48

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?

44

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
    Shortcut: :so $MYVIMRC. – kenorb Feb 20 '15 at 22:43
  • @kenorb: I've added this possibility; thanks for mentioning it! – Paul Gessler Feb 21 '15 at 4:34
  • :so $m<Tab> ← extra-short-cut – Aaron Thoma May 31 '15 at 21:46
  • 3
    Save two more keystrokes with :so %. % is set to the current file name of the buffer you edit, in this case $MYVIMRC – cbaumhardt Aug 24 '15 at 21:30
  • 1
    By far the easiest way to do this is to automatically reload the vimrc upon save - check the other answers. – Tom Hale Aug 6 '16 at 8:25
20

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)

  • 1
    johannes: Do we need to edit the .vimrc through vim for this command to automatically source the file? – Sai Manoj Kumar Yadlapati Feb 3 '15 at 18:25
  • 5
    note a vimrc file can also be called other things: for example vimrc (if it's in ~/.vim). – user50136 Feb 4 '15 at 6:09
  • 1
    @SaiManojKumarYadlapati Yes, AS said this has to be the same vom session. Bufwritepost is triggered when vom does the write – johannes Feb 4 '15 at 11:01
  • Problems with this: (1) doesn't work with .vim/vimrc, (2) will create duplicate autocmds 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 Hale Aug 6 '16 at 4:14
2
" 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.