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

2

8

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?

Monday answered 2/4, 2009 at 7:42 Comment(2)
You might want to edit in the "why" of what you're trying to do, because vim's behavior seems correct to me.Lefebvre
it's a complicated build system (clearcase based) where the main sources are in something /code/comp/subcomp/src but the working tree is in something like /tree/tree1/build/comp.Monday
C
9

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.

Carmel answered 2/4, 2009 at 18:26 Comment(3)
Ok, followup question. If I run vim using "vim 1/3/foo.txt", how can I use system('pwd -L')? If I change to the directory using cd %:p:h I've already jumped into the symlink /1/2/3/Monday
Yeah if you :cd into the directory, I don't know if there's anything you can do at that point. You can do something horrible like system('cd "' . expand('%:h') . '"; pwd -L') before :cd'ing Vim itself but not sure what you're using this for, it may not be enough.Carmel
I understand the reason, but I have set noswapfile, and I prefer to get the logical path of a file instead of its physical path.Conyers
M
1

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
Mouthful answered 17/7, 2014 at 1:23 Comment(2)
In general, this can be a helpful alternative, but it (a) requires root permissions and (b) is not suitable for many different mappings. In the case of the original question, this was a build system on a shared server which might host fifty or even 500 source trees, so it wouldn't be relevant.Monday
I was being lazy, I should've asked a new question and answer, but your question at least had some of the search terms I was using. Since part of my answer was here, that is, Vim does not play nice with how I expect it to behave, I tacked my workaround onto this page. Here's a joke: have you tried Emacs? It may do what you're expecting with symlinks. (Admittedly I am not qualified to consider hostings etc.) JK I'm sure you're using Vim for many reasons.Mouthful

© 2022 - 2024 — McMap. All rights reserved.