Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim auto-source vimrc on save with symlink

Tags:

vim

I have a symlink which points my .vimrc to the one from my repo.

Vim loads that just fine, but I can't get it to auto-source upon it being changed.

I have the typical:

if has("autocmd")
    autocmd! BufWritePost .vimrc source $MYVIMRC
endif

Which works if the vimrc is not symlinked.

like image 912
Lerp Avatar asked May 01 '26 23:05

Lerp


2 Answers

I have a similar setup where ~/.vimrc is just a symlink to a git repository. The following autocommand works for me:

autocmd! bufwritepost .vimrc source %
like image 185
innaM Avatar answered May 03 '26 16:05

innaM


I don't like symlinks in general and Vim doesn't really like them either.

The layout I use is probably similar to yours:

~/.vimrc
~/.vim/vimrc

with a big difference: ~/.vimrc is a real file, not a symlink, and it contains only one line:

runtime vimrc

that executes my real ~/.vim/vimrc. Because it is a Vim command and it doesn't use a file path, that line can be the same on every system.

Because $MYVIMRC points to a real file, :so $MYVIMRC always works.

like image 32
romainl Avatar answered May 03 '26 17:05

romainl