简体   繁体   中英

Make vim follow symlinks when opening files from command line

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.

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.

有一个名为 vim-symlink 的 Vim插件,它允许您在打开文件时自动跟踪符号链接。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM