Make vim follow symlinks when opening files from command line

5k Views Asked by At

I'm a huge vim lover, but I can't find a way to get vim to follow symlinks when opening files.

As an example, all the dotfiles in my home dirs are symlinked to within the .zprezto directory:

.vimrc -> ~/.zprezto/runcoms/vimrc
.zshrc -> ~/.zprezto/runcoms/zshrc

I'm keeping my fork of .zprezto in a private git repo, which is then used to keep all my Mac/Linux machines and servers in sync. Whenever I'm editing any of these files in vim, none of the plugins I'm using for git management work properly, because the symlink I'm accessing when calling vim ~/.zshrc is outside the git repo. Is there any way of forcing vim to follow the link and open up the actual file when I open it from the command line, so that the buffer is then in the git repo?

Tried this:

function vim() {
  local ISLINK=`readlink $1`
  /usr/local/bin/vim ${ISLINK:-$1}
}

but it didn't work as well as I'd hoped as it limits me to one file with no options. I'd like to know if there's a more sensible way of doing this before I go about write a massive wrapper function that can take all edge cases into account.

2

There are 2 best solutions below

1
On BEST ANSWER

So it doesn't look like there's anything built into vim to allow this. I had a play with the wrapper function and it turned out to be a little easier than I thought. Here's the final result:

function vim() {
  args=()
  for i in $@; do
    if [[ -h $i ]]; then
      args+=`readlink $i`
    else
      args+=$i
    fi
  done

  /usr/local/bin/vim -p "${args[@]}"
}

Just add to your .zshrc (or config file for your favorite shell) to use it.

0
On

There's a Vim plugin called vim-symlink that allows you to automatically follow the symlinks when you open a file.