I often have file log where there are several relative path to files. The paths are relative to the location of the filelog. My understanding is that I'm able to open the relative path if the CWD of vim contain that relative path. Is there a way to automatically compose the path for gf (goto file) to prepend the absolute path of the filelog opened to the relative path in it ?
1 Answer
Based on (my reading of) your description...
- You edit file
/foo/bar/baz.log
- In
baz.log
are relative paths likequux/this.txt
quux
is a subdirectory of/foo/bar
If I've interpreted things correctly then gf
with your cursor over quux/this.txt
should open this.txt
without any other steps.
That is, unless you've modified 'path'
. It should contain .
(dot meaning current directory). This setting influences how gf
works.
-
@haster8558 Though the topic of 'current file vs current working directory' didn't end up having any bearing on things you may be interested in this: Set working directory to the current file . (The reason I thought this was the default behavior is that I completely forgot that I have
autocmd BufEnter * if expand("%:p:h") !~ '^/tmp' | silent! lcd %:p:h | endif
in my vimrc. It's been in there for years.)– B LayerMar 19, 2021 at 23:03
/home/user1/path1/file.log
) while i'm in a completely different path (e.g./etc/path2/path3
), when I do :pwd i get/etc/path2/path3
, and not/home/user1/path1/
. All the file in file.log are relative to/home/user1/path1/
and if I try to open vim doesn't find them. Is there something wrong in my vim settings ?:lcd
and maybe try to think a function to do it.--clean
flag, for instance).--clean
option and the version is :VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Dec 12 2016 09:49:04)
-u NONE
and:pwd
is still returning the path where I executed vim and not the path where the file I opened is. I loaded a newer version of vim (8.2) and I still have the same behavior. If I do a:pwd
after:lcd
to the path I want, I can see the right value returned by:pwd
set (the one set by :lcd`).