I am attempting to write a vim function that I would like to be loaded automatically when vim starts, thus I am putting it in my vimrc (but maybe that is wrong?). When called during normal editing, it should write a modified version of the current buffer's contents to another file, and then return me to my original file to continue editing it. My version of vim is 8.1.5126.
So far I've attempted the following:
function! Write_new_file() " Hold the original and new file names in variables. let l:fn_no_ext = expand('%:p:r') let l:fn_ext = expand('%:e') let l:original_name = l:fn_no_ext.'.'.l:fn_ext let l:new_name = l:fn_no_ext.'_x.'.l:fn_ext " Save the state of the current buffer to the original file. write " Assign a new file name to the current buffer. exe 'file '.l:new_name " Make all the modifications to the current buffer. call Many_Modifications() " Save the modified buffer to the new file. write! " Return to editing the original file. exe 'edit! '.l:original_name endfunction The problem:
If this function is defined in a script other than my vimrc, it works as expected. However, if the function is defined in my vimrc, the edit command at the very end causes error E127. The output looks like this:
Error detected while processing /home/edward/.vim/vimrc: line 111: E127: Cannot redefine function Write_new_file: It is in use Press ENTER or type command to continue Based on the fact that, when I comment out the edit in the function, the error disappears (of course, then I don't get the behavior I want), I am guessing that the edit is the source of the problem.
Based on the fact that the error message says that the error was detected while processing my vimrc, I am guessing that something about executing the edit statement makes vim re-source my vimrc, and since my vimrc contains the definition of Write_new_file(), the error is triggered.
Of course, these are just guesses. BTW, this behavior happens regardless of whether the --noplugins flag is present or not when vim is started.
Is there a way to use the edit command in a function defined in my vimrc that does not throw an error, or alternatively is there a different way to achieve my goal that does not involve manually sourcing a script once during each session?
For anyone who is wondering what my use case is: I am writing text that has to contain many instances of different long sequences of characters. To ease the typing, for each such long sequence, I've come up with a unique shorter one that I type instead. Obviously I have to replace my sequences with the expected ones, before the files are usable. The process of creating any such file is iterative, ie: write, check validity, keep writing. Therefore I need a script that creates a new file with the appropriate replacements and returns me to where I left off in my text.
I suppose I could accomplish my goal with sed, however, I've already invested considerable effort writing the replacement scripts in vimscript, and I'd hate to have to re-implement. Also, I have a secondary goal to become more proficient in the use of vim.
:editor another command is sourcing your vimrc, we're missing code that would cause that. See How to debug my vimrc.write, no need forexec 'w'. Usingexecuteis for interpolation of expressions into commands and controlling where|delimits command end.:help abbreviations.