I want to edit .vimrc
file from Vim and apply them without restarting Vim.
5 Answers
Yes, just use the :so %
command while editing your .vimrc.
If you want vim to auto-reload your configuration, you must add the following commands :
augroup myvimrchooks
au!
autocmd bufwritepost .vimrc source $MYVIMRC
augroup END
the grouping of autocommand is here to avoid "exponential" reloading if you save several times your configuration.
-
Note this doesn't work with
gvimrc
or if your file is called_vimrc
orvimrc
, or if you edit a file called.vimrc
in another directory. See my answer to address these issues.– Tom HaleCommented Sep 4, 2016 at 2:57 -
For me it fails with errors like
Function Inc already exist, add ! to replace it
.– Hi-AngelCommented Sep 18, 2018 at 5:13 -
What function does the augroup serve in this context? Could this not be rewritten as simply
autocmd! bufwritepost .vimrc source ~/.vimrc
? Even if the group is needed for some reason, couldn't line 2 be omitted if line 3 is rewritten to start withautocmd!
? Commented Apr 9, 2020 at 4:25
Here's a more cross-platform compatible version if you run on Mac/Windows/Linux and gvimrc
:
augroup myvimrc
au!
au BufWritePost .vimrc,_vimrc,vimrc,.gvimrc,_gvimrc,gvimrc so $MYVIMRC | if has('gui_running') | so $MYGVIMRC | endif
augroup END
The autocmd watches all potential *vimrc
files and when one changes, it reloads the vimrc
file followed by gvimrc
if the GUI is running.
-
5
-
3FANTASTIC, thanks. If you do this, avoid the brain fart I just committed ... I opened gvim, added this to my vimrc, and then tried to confirm it worked. Nope ... 20 minutes later I realize I FORGOT TO SOURCE THE FILE ONCE AFTER THE AUTOCMD WAS ADDED. Once I did that, of course, everything worked. I guess it was a little too meta for me, and I had a mental "stack overflow". Commented Dec 5, 2010 at 5:50
-
3I used this, but also check if the
$GVIMRC
exists, because sometimes it doesn't.if has('gui_running') && filereadable($MYGVIMRC)
– k107Commented Feb 12, 2014 at 0:45 -
The only thing that was strange for me at first is that it simply reload the vimrc file keeping the stuff that is already set. For example, if you have "set number" initially in your vimrc, and then you comment that line and save the document you still have the numbers in the left. But it actually has sense ... if you want to disable something you should do it manually.– piponazoCommented Nov 12, 2015 at 22:57
-
1Any reason to not use
au BufWritePost $MYVIMRC
orau BufWritePost $MYVIMRC,$MYGVIMRC
?– phsCommented Nov 30, 2016 at 18:20
source your vimrc file :source ~/.vimrc
-
1
" Quickly edit/reload this configuration file
nnoremap gev :e $MYVIMRC<CR>
nnoremap gsv :so $MYVIMRC<CR>
To automatically reload upon save, add the following to your $MYVIMRC
:
if has ('autocmd') " Remain compatible with earlier versions
augroup vimrc " Source vim configuration upon save
autocmd! BufWritePost $MYVIMRC source % | echom "Reloaded " . $MYVIMRC | redraw
autocmd! BufWritePost $MYGVIMRC if has('gui_running') | so % | echom "Reloaded " . $MYGVIMRC | endif | redraw
augroup END
endif " has autocmd
and then for the last time, type:
:so %
The next time you save your vimrc
, it will be automatically reloaded.
Features:
- Tells the user what has happened (also logging to
:messages
) - Handles various names for the configuration files
- Ensures that it wil only match the actual configuration file (ignores copies in other directories, or a
fugitive://
diff) - Won't generate an error if using
vim-tiny
Of course, the automatic reload will only happen if you edit your vimrc
in vim.
-
This only seems to work when the files are all open in the same instance of
vim
in which I'm editing$MYVIMRC
. If they're in a different terminal, for example, then I still have to manually source$MYVIMRC
. Is there a way to get around this? Commented Feb 4, 2017 at 2:31 -
1Not that I'm aware of - this works by catching when the current
vim
writes out the file. It may be possible to monitor the mtime on the file itself, but unsure of how to do that without asynchronously polling.– Tom HaleCommented Feb 4, 2017 at 4:26
autocmd! bufwritepost _vimrc source %
this will automatic reload all config in _vimrc file when you save