I am editing a file which lives in a directory that is a symlink to another directory, i.e.
% ls -alh ~/www/experiments
total 0
drwxr-xr-x 3 ubuntu ubuntu 80 Dec 5 19:52 .
drwxr-xr-x 3 ubuntu ubuntu 60 Dec 8 16:25 ..
lrwxrwxrwx 1 ubuntu ubuntu 8 Dec 5 19:52 tmp -> /tmp/web
so when I am editing the file:
~/www/experiments/tmp/webpage.php
Because the parent folder of webpage.php
( tmp
) is pointing to /tmp/web
when I run the vimscript command
echo expand('%:p')
vim will echo the real file path, i.e.
/tmp/web/webpage.php
how can I make vim expand the symlinked path in vimscript? i.e. the desired output would be:
/home/ubuntu/www/experiments/tmp/webpage.php
I've looked in :h filename-modifiers
and there doesn't seem to be anything about symlinks. Also looked at functions like resolve()
- unfortunately I think resolve()
does the opposite of what I want, it produces the real file path.
getcwd()
and:pwd
also always return the full path, rather than the symlinked path that the shell'spwd
command or$PWD
variable use (In other words, it looks like Vim normalises the path on open).match()
function and anif
block to detect when the filepath expands to the/tmp
location, and if so, thensubstitute()
to "fix" the pathmount --bind /src/ /dest
, and on FreeBSDmount_nullfs /src /dest
. Other systems may have similar constructs (e.g. directory junctions in Windows). Also see: What is a bind mount? at Unix.SE.mount --bind
definitely looks interesting. I see what is a bind mount is one of Gilles' canonical answers. It usually takes me a day or two to get through those! I have finally got this working now anyway. The overall goal is that - when I am working on a webpage code file, and I want to view that files output in a browser, I simply hit a<Leader>
mapping and vim launches a new browser tab with the corresponding URL for the file.