0

I'm trying to make Vim reload its vimrc when I save a file. I have found different solutions, but none of them works for me for some reason.

Vim version: 8.2.2824, OS: Windows 7.

My vimrc:

" I start Vim, type foo, save a buffer as test.txt,
" change colorcolumn to 20, type bar in test.txt, and save it again.
" The colorcolumn doesn't move.
set colorcolumn=5

" version 1 " autocmd! BufWritePost $MYVIMRC execute "normal! :source $MYVIMRC"

" version 2 " autocmd! BufWritePost $MYVIMRC source $MYVIMRC

" version 3 " autocmd! BufWritePost $MYVIMRC source %

" version 4 " augroup reload_vimrc " autocmd! " autocmd BufWritePost *.vim,vimrc,$MYVIMRC ++once source $MYVIMRC | e " augroup END

" There is nothing else.

What is wrong here?

user90726
  • 123
  • 3
  • 13

2 Answers2

3

Following works for me:

augroup reload_vimrc | au!
    au BufWritePost $MYVIMRC source $MYVIMRC
augroup END

" version 1 " autocmd! BufWritePost $MYVIMRC execute "normal! :source $MYVIMRC"

This is a bit awkward way to source $MYVIMRC

" version 2 " autocmd! BufWritePost $MYVIMRC source $MYVIMRC

This should work (and actually works for me).

" version 3 " autocmd! BufWritePost $MYVIMRC source %

This should also work (and works for me)

" version 4 " augroup reload_vimrc " autocmd! " autocmd BufWritePost *.vim,vimrc,$MYVIMRC ++once source $MYVIMRC | e " augroup END

This should not work properly (it will source once your vimrc if you save any .vim file or your vimrc).

Maxim Kim
  • 13,376
  • 2
  • 18
  • 46
  • No, it doesn't work. Strange. Version 4 is from here: https://vi.stackexchange.com/q/21848 – user90726 Sep 11 '21 at 17:57
  • if you do :source $MYVIMRC would it reload it? – Maxim Kim Sep 11 '21 at 17:59
  • 1
    Hey, if I edit _vimrc with the same instance of Vim (and not with external editor, Sublime Text), it works. It seems I just messed the whole workflow. So now I accept the answer, thanks. – user90726 Sep 11 '21 at 18:01
0

Do you want $MYVIMRC to be sourced after saving any file?

If so, then you would need to change the filter argument in your autocmd:

autocmd! BufWritePost * source $MYVIMRC

Any filename matching the * pattern will trigger this autocmd.

Chris Heithoff
  • 1,312
  • 3
  • 12