1

I have multiple projects (folders), and each of them has its corresponding tags generated.

I found every time I launch vim it only links to my first tags file (:set tags?), I need to manually set the proper tags for each project.

Is there a way to configure vim so every time I launch a file in a specific project that tags gets set?

For example:

  • If I launch any file vim ~/ProjectA/src/main.cpp will set tags=~/ProjectA/tags
  • similary vim ~/ProjectB/include/header.h will set tags=~/ProjectB/tags

Thanks

4
  • A simple and effective way is to start vim in the right directory, or :cd there. I almost always start vim from the project root and rarely :cd away from there. – D. Ben Knoble Feb 4 at 16:56
  • :cd does change the directory, but it doesn't change where the tags is pointing to. I'm not sure if it's possible to have something in vimrc to check my current dir (root of project dir) and then set the tag to the correct tags file for the project? – JZ67 Feb 4 at 19:26
  • Sorry, I should have clarified: this should work when your tags file is in the root of the project and tags contains ./tags or tags; you can do fancier things (:h file-searching). And tagfiles() for showing what's actually used. The reason is, after changing directories, vim should be able to look in the new pwd for tags (well, ./tags looks in the directory of the current file). – D. Ben Knoble Feb 4 at 19:59
  • 1
    Welcome to Vi and Vim! – filbranden Feb 4 at 23:16
1

You can use:

set tags=./tags;

(You can also include the other entries from the default 'tags', such as just tags, which means a file with that name in the current directory, and also the uppercase variants, if those interest you as well. Use commas to separate different entries. The ; is not a separator.)

This will look for a file named tags on either the same directory as the current file, or on a directory above it. The search keeps going up until reaching the root of the tree.

See :help file-searching, in specific:

  1. Upward search:

Here you can give a directory and then search the directory tree upward for a file. You could give stop-directories to limit the upward search. The stop-directories are appended to the path (for the 'path' option) or to the filename (for the 'tags' option) with a ';'. If you want several stop-directories separate them with ';'. If you want no stop-directory ("search upward till the root directory) just use ';'.

1
  • 1
    Thanks fibranden! – JZ67 Feb 5 at 3:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.