80

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

Vaibhav Jain
  • 33,887
  • 46
  • 110
  • 163
0xdeadbeef
  • 4,090
  • 8
  • 33
  • 37

5 Answers5

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.

thiagowfx
  • 4,832
  • 6
  • 37
  • 51
Raoul Supercopter
  • 5,076
  • 1
  • 34
  • 37
  • 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 Sep 04 '16 at 02:57
  • For me it fails with errors like `Function Inc already exist, add ! to replace it`. – Hi-Angel Sep 18 '18 at 05: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!`? – John Karahalis Apr 09 '20 at 04: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.

matpie
  • 17,033
  • 9
  • 61
  • 82
  • 5
    It is set when GUI Vim is started. see `:help gvimrc` – matpie Jun 21 '10 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". – Charlie Flowers Dec 05 '10 at 05:50
  • 3
    I used this, but also check if the `$GVIMRC` exists, because sometimes it doesn't. `if has('gui_running') && filereadable($MYGVIMRC)` – k107 Feb 12 '14 at 00: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 Nov 12 '15 at 22:57
  • 1
    Any reason to not use `au BufWritePost $MYVIMRC` or `au BufWritePost $MYVIMRC,$MYGVIMRC`? – phs Nov 30 '16 at 18:20
  • @phs this seems to work fine. It's definitely cleaner, and more extensible – Zoey Hewll Feb 04 '17 at 02:11
  • This tripped me up at first: it's my understanding that this will re-source $MYVIMRC in whatever buffer was editing a configuration file, but not in any other buffers. For example, if I'm editing .vimrc in one split and editing a totally unrelated file in another split, and then save .vimrc, the changes to .vimrc will affect the split that just saved .vimrc, but will not affect the other split. Is that not correct? – John Karahalis Apr 11 '20 at 19:27
  • @matpie Please add `init.vim` for NeoVim users. – Refael Sheinker Jun 25 '21 at 15:26
35

source your vimrc file :source ~/.vimrc

shingara
  • 46,608
  • 11
  • 99
  • 105
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.

Tom Hale
  • 40,825
  • 36
  • 187
  • 242
  • 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 Feb 04 '17 at 02: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 Feb 04 '17 at 04:26
1

autocmd! bufwritepost _vimrc source %

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

James
  • 11
  • 1