Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

symlink

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?

like image 609
Mikeage Avatar asked Apr 02 '09 07:04

Mikeage


People also ask

How do I soft link a directory in Linux?

Use the -s option to create a soft (symbolic) link. The -f option will force the command to overwrite a file that already exists. Source is the file or directory being linked to. Destination is the location to save the link – if this is left blank, the symlink is stored in the current working directory.

How do I find symbolic links in Linux?

Many Linux file managers offer the ability to create symbolic links graphically. If yours does, you can generally do this by right-clicking a folder or file and selecting “Copy”, and then right-clicking inside another folder and selecting “Make Link”, “Paste as Link”, or a similarly named option.


2 Answers

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.

like image 199
Brian Carper Avatar answered Oct 21 '22 01:10

Brian Carper


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
like image 33
Brady Trainor Avatar answered Oct 21 '22 03:10

Brady Trainor