Reputation: 7515
For each of my C++ projects I keep a tag file in the root of the source tree. How can I have Vim automatically load the tag file for that directory when I change to it?
e.g. My directory layout looks like this:
~/dev/project1/tags
~/dev/project2/tags
I start Vim (from GUI) and the present working directory defaults to $HOME. I type :cd ~/dev/project/
and Vim should load ~/dev/project/tags
. Tomorrow I might want to work on project2, so I expect Vim to load the tags for that project when I cd
to it.
How can this be done?
Upvotes: 4
Views: 8333
Reputation: 196596
Just add the following line to your ~/.vimrc
:
set tags=./tags;,tags;
It means "look for a tags
file in the directory of the current file, then upward until /
and in the working directory, then upward until /
".
In other words, no matter where you are in your project, Vim will pick up the right tags
for the project.
:help tags
is a good read, it would have saved you the hassle of posting a question here.
Note: the magic part of that line is the ;
of "upward search". You can give Vim an upper limit like this: ;$HOME
.
Upvotes: 15