600

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

5 Answers 5

750

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
7
  • 2
    Have a look at stackoverflow.com/questions/803464/…
    – mrucci
    Apr 17, 2010 at 14:27
  • 49
    and :so ~/.vimrc should work too on unix
    – Yab
    May 8, 2011 at 18:18
  • 3
    :so ~/_vimrc works on windows. Of course, I keep my _vimrc in my windows home directory, or else this won't work. To see what Vim considers your home directory, use :echo expand("~"). Nov 30, 2012 at 14:10
  • 6
    Note that if keyboard mappings were already in place, then they will not erased even if they are deleted from the new vimrc
    – Phani
    Oct 2, 2014 at 6:58
  • Or :so ~/.vim/gvimrc or wherever you put it of course
    – NeilG
    Aug 20, 2019 at 1:47
82

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.

2
  • 2
    How performant is this method? Sep 19, 2017 at 18:37
  • 1
    I find that use this kind of command sometimes hangs my nvim for a few seconds or longer.
    – jdhao
    Sep 29, 2018 at 8:29
15

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.

1
  • where will be the echom message be displayed?
    – mtk
    May 31, 2022 at 10:08
7

An alternative to remembering/typing :so $MYVIMRC is these mappings. It lets you quickly edit vimrc (ev) or source vimrc (sv).

" Quickly open/reload vim
nnoremap <leader>ev :split $MYVIMRC<CR>  
nnoremap <leader>sv :source $MYVIMRC<CR>     
0

I have a slightly different version of ReloadVimrcFunction

" Reloads vimrc after saving it but keep cursor position
if !exists('*ReloadVimrcFunction')
    function! ReloadVimrcFunction()
        let save_cursor = getcurpos()
        source $MYVIMRC | windo redraw
        call setpos('.', save_cursor)
        echom "Reloaded $MYVIMRC"
    endfunction
endif
noremap <silent> <Leader>v :drop $MYVIMRC<cr>
command! -nargs=0 ReloadVimrc :call ReloadVimrcFunction()
nnoremap <silent> <C-s> :call ReloadVimrcFunction()<CR>

In order to avoid loading the function twice we set:

if !exists('*ReloadVimrcFunction')

We also save the cursor position because it would be anoying to have our cursor positioning changing every time we reload $MYVIMRC.

I also have an augroup to trigger this function each time I save $MYVIMRC

augroup Reload
    autocmd!
    autocmd BufWritePost $MYVIMRC call ReloadVimrcFunction()
augroup END

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .