79

I want to edit .vimrc file from Vim and apply them without restarting Vim.

5 Answers 5

79

Yes, just use the :so % command while editing your .vimrc.

If you want vim to auto-reload your configuration, you must add the following commands :

augroup myvimrchooks
    au!
    autocmd bufwritepost .vimrc source $MYVIMRC
augroup END

the grouping of autocommand is here to avoid "exponential" reloading if you save several times your configuration.

3
  • Note this doesn't work with gvimrc or if your file is called _vimrc or vimrc, or if you edit a file called .vimrc in another directory. See my answer to address these issues.
    – Tom Hale
    Commented Sep 4, 2016 at 2:57
  • For me it fails with errors like Function Inc already exist, add ! to replace it.
    – Hi-Angel
    Commented Sep 18, 2018 at 5:13
  • What function does the augroup serve in this context? Could this not be rewritten as simply autocmd! bufwritepost .vimrc source ~/.vimrc? Even if the group is needed for some reason, couldn't line 2 be omitted if line 3 is rewritten to start with autocmd!? Commented Apr 9, 2020 at 4:25
47

Here's a more cross-platform compatible version if you run on Mac/Windows/Linux and gvimrc:

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

The autocmd watches all potential *vimrc files and when one changes, it reloads the vimrc file followed by gvimrc if the GUI is running.

8
  • 5
    It is set when GUI Vim is started. see :help gvimrc
    – matpie
    Commented Jun 21, 2010 at 16:54
  • 3
    FANTASTIC, thanks. If you do this, avoid the brain fart I just committed ... I opened gvim, added this to my vimrc, and then tried to confirm it worked. Nope ... 20 minutes later I realize I FORGOT TO SOURCE THE FILE ONCE AFTER THE AUTOCMD WAS ADDED. Once I did that, of course, everything worked. I guess it was a little too meta for me, and I had a mental "stack overflow". Commented Dec 5, 2010 at 5:50
  • 3
    I used this, but also check if the $GVIMRC exists, because sometimes it doesn't. if has('gui_running') && filereadable($MYGVIMRC)
    – k107
    Commented Feb 12, 2014 at 0:45
  • The only thing that was strange for me at first is that it simply reload the vimrc file keeping the stuff that is already set. For example, if you have "set number" initially in your vimrc, and then you comment that line and save the document you still have the numbers in the left. But it actually has sense ... if you want to disable something you should do it manually.
    – piponazo
    Commented Nov 12, 2015 at 22:57
  • 1
    Any reason to not use au BufWritePost $MYVIMRC or au BufWritePost $MYVIMRC,$MYGVIMRC?
    – phs
    Commented Nov 30, 2016 at 18:20
35

source your vimrc file :source ~/.vimrc

1
  • 1
    on windows, just :so ~/_vimrc Commented Nov 8, 2017 at 17:15
8
" 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.

2
  • This only seems to work when the files are all open in the same instance of vim in which I'm editing $MYVIMRC. If they're in a different terminal, for example, then I still have to manually source $MYVIMRC. Is there a way to get around this?
    – Zoey Hewll
    Commented Feb 4, 2017 at 2:31
  • 1
    Not that I'm aware of - this works by catching when the current vim writes out the file. It may be possible to monitor the mtime on the file itself, but unsure of how to do that without asynchronously polling.
    – Tom Hale
    Commented Feb 4, 2017 at 4:26
1

autocmd! bufwritepost _vimrc source %

this will automatic reload all config in _vimrc file when you save

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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