When I do a change in my .vimrc
, I usually exit Vim and open it again. Is there an easier way to reload the new .vimrc
changes?
3 Answers
Run:
:source $MYVIMRC
inside Vim to reload the vimrc
file. Or, a shorter version:
:so $MYVIMRC
as mentioned in a comment by kenorb.
You may also find it useful to map either of these forms to a key. For example:
nnoremap <Leader>r :source $MYVIMRC<CR>
-
2
-
@kenorb: I've added this possibility; thanks for mentioning it! Commented Feb 21, 2015 at 4:34
-
-
3Save two more keystrokes with
:so %
.%
is set to the current file name of the buffer you edit, in this case$MYVIMRC
Commented Aug 24, 2015 at 21:30 -
2By far the easiest way to do this is to automatically reload the vimrc upon save - check the other answers.– Tom HaleCommented Aug 6, 2016 at 8:25
If you just want to reload the file once in a while
:source $MYVIMRC
as Paul wrote is correct. If you end up changing your vimrc often, you could add something like this to your vimrc file:
autocmd BufWritePost .vimrc,_vimrc source $MYVIMRC
This will reload the file when you write it (from within that vim session)
-
1johannes: Do we need to edit the .vimrc through vim for this command to automatically source the file? Commented Feb 3, 2015 at 18:25
-
5note a vimrc file can also be called other things: for example vimrc (if it's in ~/.vim). Commented Feb 4, 2015 at 6:09
-
1@SaiManojKumarYadlapati Yes, AS said this has to be the same vom session. Bufwritepost is triggered when vom does the write– johannesCommented Feb 4, 2015 at 11:01
-
Problems with this: (1) doesn't work with
.vim/vimrc
, (2) will create duplicateautocmd
s so each subsequent save will be slower as file will be reloaded multiple times, (3) doesn't behave with vim-tiny which doesn't have autocmd and is still the default on some linux distributions. See my answer to address these issues.– Tom HaleCommented Aug 6, 2016 at 4:14
" 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.