如何在vim中扩展符号链接到文件名的完整路径?

sve*_*vec 11 vim

我正在使用Mac和MacVim 7.3.

我有一个符号链接〜/ some/symlink,它是一个真实文件〜/ some/actual_file的链接.

当我"vim~/some/symlink"时,它会在vim中显示文件,其中vim的状态行名称为〜/ some/symlink,这是有道理的.

如何从vim中获取"真实"文件〜/ some/actual_file的完整路径?我希望vim状态行说〜/ some/actual_file而不是〜/ some/symlink.

我期望vim函数resolve()可以工作,给出它的帮助描述(粘贴在下面),但是resolve("〜/ some/symlink")返回〜/ some/symlink,所以没有帮助.

我错过了什么?谢谢!

resolve({filename})                 *resolve()* *E655*
        On MS-Windows, when {filename} is a shortcut (a .lnk file),
        returns the path the shortcut points to in a simplified form.
        On Unix, repeat resolving symbolic links in all path
        components of {filename} and return the simplified result.
        To cope with link cycles, resolving of symbolic links is
        stopped after 100 iterations.
        On other systems, return the simplified {filename}.
Run Code Online (Sandbox Code Playgroud)

Lau*_*ves 24

您的问题是"〜",它实际上不是文件名的一部分,而是您的主目录的简写.你可以使用expand()然后resolve().例如:

:echo resolve(expand("~/some/symlink"))
Run Code Online (Sandbox Code Playgroud)

expand()还会扩展环境变量之类的东西(例如:$HOME,$VIMRUNTIME).

  • @svec如果你不想`expand()`扩展环境变量并进行通配,请使用`fnamemodify('〜/ some/symlink',':p')`. (3认同)