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



Question:

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?


Solution: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.


Solution: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  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »