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?

2 Answers 2

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).

3
  • No, it doesn't work. Strange. Version 4 is from here: vi.stackexchange.com/q/21848
    – user90726
    Sep 11, 2021 at 17:57
  • if you do :source $MYVIMRC would it reload it?
    – Maxim Kim
    Sep 11, 2021 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, 2021 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.

2
  • No, it's not my intent, but thanks for the explanation regarding asterisk. The downvotes are not from me.
    – user90726
    Sep 11, 2021 at 19:55
  • Haha, I was surprised that none of your original attempts worked, so I figured I give an answer. Sep 11, 2021 at 21:28

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.