I have the following in my .vimrc so that it processes after saving
" source as soon as we write the vimrc file
if has("autocmd")
autocmd bufwritepost .vimrc source $MYVIMRC
endif
However, I have a more involved file, and it appears that the time to post the vimrc file after save gets longer and longer and longer to where I have to quit out of my gvim session and start over.
Is there a 'clear workspace' or a better way to resolve this issue, so that I may stay within the comforts of my single gvim session all day?
Every time the autocmd is sourced it is added to the autocmd list. So you might be sourcing your vimrc thousands of times when you save because everytime you source you add the autocmd to the list. (You can see that the autocmd is added multiple time by doing :au bufwritepost)
To fix this you just need to wrap the autocmd in a augroup and clear the group when it is loaded.
augroup NAME_OF_GROUP
autocmd!
autocmd bufwritepost .vimrc source $MYVIMRC
augroup end
The autocmd! removes all autocmds from the group.
Help pages to read :h autocmd-define and :h autocmd-groups
Maybe you encountered a phenomenon that it outlined in
:h autocommand
The help sais:
When your .vimrc file is sourced twice, the autocommands will appear twice. To avoid this, put this command in your .vimrc file, before defining autocommands:
:autocmd! " Remove ALL autocommands for the current group.If you don't want to remove all autocommands, you can instead use a variable to ensure that Vim includes the autocommands only once:
:if !exists("autocommands_loaded")
: let autocommands_loaded = 1
: au ...
:endif
I get used to grouping of my autocommands like this
augroup BWCCreateDir
autocmd!
autocmd BufWritePre * :call s:MkNonExDir(expand('<afile>'), +expand('<abuf>'))
augroup END
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With