3

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.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Lerp
  • 2,957
  • 3
  • 24
  • 43

4 Answers4

2

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 %
innaM
  • 47,505
  • 4
  • 67
  • 87
  • Hmm, that's essentially what mine is. Perhaps it could be something to do with the vimrc file in my repo being called `vimrc` instead of `.vimrc` (the leading .)? – Lerp May 13 '13 at 09:05
  • 1
    Indeed. `bufwritepost` might be looking at the real file's name. – innaM May 13 '13 at 09:27
  • Oh, yes: `:help autocmd-patterns` – innaM May 13 '13 at 09:29
  • I worked around this by changing the pattern after BufWritePost to *vimrc, so it matched both the dotted and undotted format. I am not too worried about it catching others. – C. Warren Dale Nov 25 '16 at 18:21
1

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.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • You don't like symlinks? And vim doesn't like symlinks either? I can't help it, but that last one is a ridiculous statement. – innaM May 13 '13 at 08:23
  • You are right, Vim likes symlinks but only if they are absolute. – romainl May 13 '13 at 11:22
1

I solved that problem that way that all my configuration I am keeping in dotfiles folder https://github.com/lis2/dotfiles

Then I have small and simple ruby script which I running when I am changing something in configuration

#!/usr/bin/env ruby
require "fileutils"

config_hash = { "tmux.conf" => ".tmux.conf", "vimrc" => ".vimrc", "vim" => ".vim", "gitconfig" => ".gitconfig", "gitignore" => ".gitignore"}
config_hash.each do |k,v|
  FileUtils.rm_rf(File.dirname(__FILE__) + "/../#{v}")
  FileUtils.ln_s(File.dirname(__FILE__) + "/#{k}", File.dirname(__FILE__) + "/../#{v}")
end

I recommend you to built same configuration. On all computers (private/work) I just clone my repo, run symlink.rb and my simple environment is ready for work.

cheers!

lis2
  • 4,234
  • 1
  • 17
  • 9
0

I have both .vimrc and .gvimrc symlinked to a git repo like you are describing. I'm running MacVim macOS.

I found a great answer to the question progressively-slower-reloading-time-of-vimrc.

I modified it a bit to reload both vimrc and gvimrc when any of them changes. I have been using it for months now without issues.

Here it is. You need just this and just in .vimrc. You don't have to add anything to .gvimrc.

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Autoreload vimrc
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
augroup configsGroup
    autocmd!
    " Make changes effective after saving .vimrc. Beware that autocommands are
    " duplicated if .vimrc gets sourced again, unless they are wrapped in an
    " augroup and the autocommands are cleared first using 'autocmd!'
    autocmd! bufwritepost *\<vimrc\> call OnSavingConfigs()
    autocmd! bufwritepost *\<gvimrc\> call OnSavingConfigs()
augroup END

" Avoid infinite loops
if !exists("*OnSavingConfigs")
    function! OnSavingConfigs()
        " Clear previous mappings, they don't go away automatically when
        " sourcing vimrc.
        mapclear
        source $MYGVIMRC
        source $MYVIMRC
        redraw
        echo "Reloaded " . $MYVIMRC . " and " . $MYGVIMRC . "."
    endfunction
endif
Sebastian Ärleryd
  • 1,774
  • 14
  • 19