Is there a way to push out a vimrc change to all open windows and instances of vim without having to write/change any buffers or reopen vim? I'd like vim to be able to behave like a modern day IDE for this as I change my vimrc a lot and am trying to learn more vim scripting.
2 Answers
I use the following autocmd
which reloads my .vimrc
whenever I've finished editing it and close it.
augroup reloadvimrc
autocmd!
" auto source whenever vimrc fully closed
" nested because (as of vim 8.0 patch 1-823) needed to do ColorScheme cmds
" autocmd BufWinLeave $MYVIMRC nested source $MYVIMRC
autocmd BufWinLeave $HOME/.vimrc nested source $HOME/.vimrc
augroup end
Further, I use the following mapping to make it easy to edit my .vimrc
" \V open ~/.vimrc
" nmap <leader>V :-tabf $MYVIMRC<CR>
" bc neovim
nnoremap <leader>V :-tabf $HOME/.vimrc<CR>
vnoremap <leader>V :-tabf $HOME/.vimrc<CR>
In general, this is going to handle most of the cases you're worried about. My understanding is you can never truly accomplish a full, clean reload without restarting Vim because of items like VimEnter
and statefulness.
If your vimrc is reloadable, you can simply do :source $MYVIMRC
(I have that bound to a command :Reload
, which is bound to a keystroke <leader>r
). In practice, this means avoiding messing with local settings, using augroups
, and not unconditionally calling colorscheme
.
Other tips:
- If you are editing an ftplugin or syntax file, you can usually do
:edit
to force-reload those in a file you're in (but note that any mappings and settings that were there may stick around) - If you are working on a file in the
plugin
directory, you can use:runtime
—or, better,:Runtime
from tpope's scriptease plugin—to load the file.
Causing all open instances of vim to do this would be tricky, though. If you could send signals to vim, it might be possible, but I couldn't find a way to make it respond to, e.g., USR1
.