I really like my vim to change its working dir to the file I'm currently working on. For this I use in my vimrc:
autocmd BufEnter * if bufname("") !~ "^\[A-Za-z0-9\]*://" | lcd %:p:h | endif
What I also like is using
gF(*) in vim to open files under the cursor in a new buffer.
This combination, however, induces a problem in my preferred workflow:
- Let's say I want to find all occurrences of "foo" in a project, so with my shell I
cdto the folder
grep -Ern 'foo' * | vim -. For the sake of the argument the output, which is opened in vim is:
a/file1.c:42: foo b/file2.c:13: foo
Now I want to inspect the hit in
file1so I use
gFto jump into it. This action changes the working directory to
When I go back to the tab with the output of
grepthe working directory stays at
%:p:happarently cannot resolve a path for something read from stdin.
Problem: I now cannot jump into
gFsince the relative path is now wrong.
It would be nice to make vim automatically change its working directory to the location where it was invoked when switching to a buffer. For this example, this means when switching to the
grep buffer I expect my working directory to be
I would be grateful for any ideas on how to achieve this.
(*) Full disclosure: I use spf-13 vim but the basic problem should apply to vim in general.