Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

ctags

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?

like image 213
x-x Avatar asked Dec 12 '22 10:12

x-x


1 Answers

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.

like image 72
romainl Avatar answered Jan 05 '23 01:01

romainl