Make vim follow symbolic links when opening files from command line

I'm a big vim lover, but I can't find a way to get vim to follow symbolic links when opening files.

As an example, all dot files in my home directories are linked by a symbol inside the .zprezto directory:

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

      

I save my .zprezto fork in a private git repository which is then used to sync all Mac / Linux computers and servers. Whenever I edit any of these files in vim, none of the plugins I use to control git work correctly because the symbolic link I access when called vim ~/.zshrc

is outside the git repository. Is there a way to make vim follow the link and open 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 I hoped as it restricts me to one file without any parameters. I would like to know if there is a smarter way to do this before I start writing a massive wrapper function that can take all edge cases into account.

+3


source to share


1 answer


So it doesn't seem like it's built into vim to allow this. I had a game with a wrapper function and it turned out to be a little lighter than I thought. Here's the end 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 yours .zshrc

to use it.

+1


source







All Articles