0

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 ?

8
  • Not sure. I've just checked. If I open an absolute path with vim (e.g. /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 ?
    – haster8558
    Mar 16 at 9:59
  • Ok, I'll try to change the path with :lcd and maybe try to think a function to do it.
    – haster8558
    Mar 16 at 10:01
  • It's possible you have non-default settings that interfere with the behavior. Try the same steps but without your vimrc file (you can run vim with --clean flag, for instance).
    – B Layer
    Mar 16 at 10:01
  • I start to think that maybe I'm using a way too old version of vim, it doesn't recognize the --clean option and the version is : VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Dec 12 2016 09:49:04)
    – haster8558
    Mar 16 at 10:05
  • So I tried -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`).
    – haster8558
    Mar 16 at 10:16
1

Based on (my reading of) your description...

  • You edit file /foo/bar/baz.log
  • In baz.log are relative paths like quux/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.

1
  • @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 Layer
    Mar 19 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.