How to expand a symbolic link to the full path of a filename in vim?

爷,独闯天下 提交于 2020-12-29 05:35:49

问题


I'm using a Mac and MacVim 7.3.

I have a symbolic link ~/some/symlink which is a link to a real file ~/some/actual_file.

When I "vim ~/some/symlink" it brings up the file in vim with vim's status line saying the name is ~/some/symlink, which makes sense.

How do I get the full path of the "real" file ~/some/actual_file from within vim? I want the vim status line to say ~/some/actual_file instead of ~/some/symlink.

I expected that the vim function resolve() would work, given its help description (pasted below), but resolve("~/some/symlink") returns ~/some/symlink, so that's no help.

What am I missing? Thanks!

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}.

回答1:


Your problem is the "~", which isn't really part of the filename, but instead a shorthand for your home directory. You can use expand() and then resolve(). eg:

:echo resolve(expand("~/some/symlink"))

expand() will also expand things like environment variables (eg: $HOME, $VIMRUNTIME).



来源:https://stackoverflow.com/questions/5794611/how-to-expand-a-symbolic-link-to-the-full-path-of-a-filename-in-vim

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!