How do I automatically load a tag file from a directory when changing to that directory?

StackOverflow https://stackoverflow.com/questions/19330843

  •  30-06-2022
  •  | 
  •  

Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

Have a try about easytag, there is an option 'g:easytags_dynamic_files' can handle it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top