vim Autocommands Automatically source .vimrc after saving


Add this to your $MYVIMRC:

" Source vim configuration file whenever it is saved
if has ('autocmd')          " Remain compatible with earlier versions
 augroup Reload_Vimrc       " Group name.  Always use a unique name!
    autocmd!                " Clear any preexisting autocommands from this group
    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


  • echom tells the user what has happened (and also logs to :messages).
  • $MYVIMRC and $MYGVIMRC handle platform-specific names for the configuration files,
  • and only match the actual configuration files (ignoring copies in other directories, or a fugitive:// diff)
  • has() will prevent an error if using incompatible versions, such as vim-tiny.
  • autocmd! avoids buildup of multiple identical autocommands if this file is sourced again. (It clears all commands in the named group, so the group name is important.)