How do you reload your .vimrc file without restarting vim?

Can you edit your .vimrc file and reload it without having to restart Vim?


Solution 1:

If you're editing it, you can reload it with:

:so %

% stands for current file name (see :h current-file) and :so is short for :source, which reads the content of the specified file and treats it as Vim code.

In general, to re-load the currently active .vimrc, use the following (see Daily Vim):

:so $MYVIMRC

Solution 2:

Even better, you configure Vim to watch for changes in your .vimrc and automatically reload the config.

augroup myvimrc
    au!
    au BufWritePost .vimrc,_vimrc,vimrc,.gvimrc,_gvimrc,gvimrc so $MYVIMRC | if has('gui_running') | so $MYGVIMRC | endif
augroup END

Source: this answer on SO

Note: this particular method watches for the many variations of Vim config filenames so that it's compatible with GUI Vim, Windows Vim, etc.

Solution 3:

Key mappings

" Quickly edit/reload this configuration file
nnoremap gev :e $MYVIMRC<CR>
nnoremap gsv :so $MYVIMRC<CR>

Completely automated solution

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.