简体   繁体   English

从命令行打开文件时使 vim 遵循符号链接

[英]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.我是一个巨大的 vim 爱好者,但我找不到让 vim 在打开文件时遵循符号链接的方法。

As an example, all the dotfiles in my home dirs are symlinked to within the .zprezto directory:例如,我的主目录中的所有点文件都被符号链接到 .zprezto 目录中:

.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.我将 .zprezto 的分支保存在一个私有 git 存储库中,然后用于使我所有的 Mac/Linux 机器和服务器保持同步。 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.每当我在 vim 中编辑这些文件中的任何一个时,我用于 git 管理的插件都无法正常工作,因为我在调用vim ~/.zshrc时访问的符号链接在 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?当我从命令行打开文件时,是否有任何方法可以强制 vim 跟随链接并打开实际文件,以便缓冲区位于 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.所以看起来vim中没有内置任何东西来允许这样做。 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.只需添加到您的.zshrc (或您最喜欢的 shell 的配置文件)中即可使用它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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