Getting the logical path in VIM when there's a symlink

久未见 提交于 2019-12-18 05:51:52

问题


I have the following setup:

mkdir /1
mkdir /1/2
mkdir /1/2/3
ln -s /1/2/3 /1/3

If I do cd /1/3, and then pwd, I get /1/3. If I use pwd -P, I can get /1/2/3, or pwd -L to force /1/3.

In VIM, I'm looking for a way to get the /1/3.
If I open a file in /1/3/foo.txt, and I use something like fnamemodify(bufname(winbufnr(0)), ':p:h'), it returns /1/2/3.
How can I tell it to give me the same directory that pwd would give?


回答1:


It appears you can't, other than via system('pwd -L'). According to the vim_use mailing list Vim automatically resolves symlinks nowadays.

See the text around :h E773 for rationale; if Vim went by symlinks instead of resolved filename, it'd be possible to have the same file open in two buffers under two different names, and Vim would become confused trying to figure out where the swap file should be. See also in :h version7.txt:

Unix: When editing a file through a symlink the swap file would use the name of the symlink. Now use the name of the actual file, so that editing the same file twice is detected.




回答2:


Short answer:

You may be able to use mount binding as a substitute for symlinks. See man mount.

Long answer:

I had a similar problem, as I have a short symlink to a mounted partition,

/e -> /media/iam/ext4test

I also have a symlink ~/.vimrc -> /e/configs/.vimrc.

I was running into trouble trying to pop into Netrw in the containing directory (I was landing in ~, but I couldn't see a robust way to avoid that, keeping in mind the desire to use Bookmarks, etc).

My solution was, after considering possibly changing the mount point, is that you can add mount points. So after unlink e, I used mount --bind /media/iam/ext4test /e.

Now, if I am in /e/configs/.vimrc and use :edit . (or :e. etc), it will pop me into Netrw in the containing directory.

Edit:

The command mount --bind makes transient changes. For a permanent bind mount, I add the following to /etc/fstab,

# <file system>      <mount point>  <type>  <options>  <dump>  <pass>
/media/iam/ext4test  /e             none    bind       0       0


来源:https://stackoverflow.com/questions/708700/getting-the-logical-path-in-vim-when-theres-a-symlink

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