When I switch buffers in Vim (using :bn and :bp), I want it to automatically set the working directory, BUT not to be the directory of the opened file. I want Vim to search recursively upwards for a file called “tags”, and when it finds that file, set the working directory to the directory with the “tags” file.
Example:
:e ~/programming/projects/foobar/src/js/scripts.js
As you can see, “foobar” is kind of the “project root”. Let’s assume the “tags” file is in the foobar directory. Now Vim should look in “js”, but there’s no tags file. Then it should look in “src”, no tags file there. Then it should look in “foobar” and find the “tags” file, then do:
:cd ~/programming/projects/foobar/
Is there a simple way to do this? 🙂
If your whole point is to get to the correct “tags”-file then this could be done easier:
The
tagsoption accepts a comma (or space) delimited list of entries. In my example I have the following entries:./tagsthis means it should look first for atags-file in the current directorytags;$HOME/programmingthis means look for atags-file from the current directory up to$HOME/programming(that’s what the semicolon does, see file-searching). If you don’t specify the “stop”-directory using the semicolon then it searches up to the root directory/$HOME/programming/your/global/tagslastly this is a tags file referred to by absolute file nameMy example is probably overkill for your purpose from your description you only need this:
But if you really need to change the working directory then you could add something like this (change
lcdtocdif you have to) to your .vimrc: