DEV Community

Serhat Teker
Serhat Teker

Posted on • Originally published at tech.serhatteker.com on

Auto Source vimrc When Saved

If you find yourself always executing :so after every vimrc change, put this in your .vimrc.

autocmd! bufwritepost .vimrc source %
Enter fullscreen mode Exit fullscreen mode

This causes Vim to automatically source and apply your changes to your vimrc when the buffer is saved.

Improvements

One

It will be better option to use variable, since your .vimrc file may be different: For instance if you use NeoVim your rc file is init.vim.

" Automatically source vimrc on save.
autocmd! bufwritepost $MYVIMRC source $MYVIMRC
Enter fullscreen mode Exit fullscreen mode

Two

I want to see a confirmation from Vim after my file sourced to ensure everything is OK. In order to make this happen you can use below snippet:

autocmd! BufWritePost $MYVIMRC source $MYVIMRC | echom "Reloaded $NVIMRC"
Enter fullscreen mode Exit fullscreen mode

All done!

Top comments (0)