60

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?

thameera
  • 17,434
  • 16
  • 40
  • 42

3 Answers3

58

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>
Paul Gessler
  • 696
  • 7
  • 7
22

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)

Martin Tournoij
  • 62,054
  • 25
  • 192
  • 271
johannes
  • 589
  • 3
  • 7
  • 1
    johannes: Do we need to edit the .vimrc through vim for this command to automatically source the file? – Sai Manoj Kumar Yadlapati Feb 03 '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 04 '15 at 06:09
  • 1
    @SaiManojKumarYadlapati Yes, AS said this has to be the same vom session. Bufwritepost is triggered when vom does the write – johannes Feb 04 '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 06 '16 at 04: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.

Tom Hale
  • 2,681
  • 14
  • 32