5

I'm interested in using exuberant-tags (ctags) in vim, but I use Vim for all sorts of projects so I need multiple tag files and I need vim to use the correct tag file on a per project basis.

Is there an easy way to manage this?

e.g. I could store tags in files like ~/.vim/tagfiles/-var-www-mywebsite.com then if I was editing a file in /var/www/mywebsite.com/foo/bar/baz.php it could try looking for

~/.vim/tagfiles/-var-www-mywebsite.com-foo-bar
then
~/.vim/tagfiles/-var-www-mywebsite.com-foo
then
~/.vim/tagfiles/-var-www-mywebsite.com

until it finds a match? But I don't want to reinvent the wheel and spending hours with vimscript if someone's already done it, as I suspect it's a common requirement?

10

If each project is a separate git repo, one can use fugitive.vim which will automatically point tags at .git/tags, allowing easy per-project tags.

  • Wow, and I already have that installed! Never knew it did that! – artfulrobot Apr 15 '15 at 16:29
  • 4
    Might want to look at Effortless Ctags with Git. I have also heard good things about the Gutentags plugin. – Peter Rincker Apr 15 '15 at 16:49
  • Where can I find more information about that specific fugitive.vim feature? It doesn't seem to work the way I think it does. – Pieter Feb 5 '17 at 14:50
13

This line should be enough:

set tags=./tags;,tags;

It tells Vim to look for a tags file in the directory of the current file as well as in the working directory, and up, and up, and…

2

You can use my project-tags plugin for this. It requires a tiny bit more setup than the fugitive.vim approach, but it is also more of a power user tool. Once you set it up for your projects, it will automatically generate tags for you in your projects and it will automatically select the correct tags file for you. It is largely designed around avoiding false positives when the tags file is used for auto-completion or jump to. It uses the following techniques to reduce the number of false positives:

  • tags are generated per project, instead of having a global tags file
  • tags are also separated per language instead of one tags file for the entire project
  • An optional configuration option allows you to specifically exclude certain directories from being used to generate tags. If you tend to get a lot of false positives from code inside a specific directory, excluding it might improve your tags experience.
  • An optional configuration option allows you to specifically include a directory outside your project that your project tags file will also have tags from. This is useful when you reference a library that is not inside your project directory. It can also be helpful when used in combination with the exclude option. You can generate tags inside the excluded directory that include the tags from the rest of the project. That way when you are browsing / working with code inside the excluded directory, the tags will still be there and work properly.
  • Would this work with gutentags / fugitive to set a single, overall tags location to $root/.git/tags if there's a git repo, and $root/tags otherwise? – Tom Hale Aug 13 '16 at 10:59
  • @TomHale My recommendation would be just to create a .project_tags.config.vim at the root of your project directory and try out this plugin. By default it does not work as you describe because it does not need to. It places the tags files in the project root directory, thus avoiding conflicting with other plugins that look for the tags file inside the .git directory. This plugin selects the appropriate tags file for you and generates the tags for you, so you don't need it to integrate with fugitive or other plugins. – still_dreaming_1 Aug 14 '16 at 12:08
  • @TomHale While it can be configured to generate a single tags file per project, this is precisely what it is designed to avoid because this plugin provides a better experience than what is offered by only a single tags file. I recommend just letting it do it's thing and see if that works for you. If you really want to configure it to work with a single tags file, you would add something like this to your config file: call project_tags#add_language('mytagsfile', ['js', 'php', 'py']) The list should contain all the file extensions for languages you want to be included in that one tags file. – still_dreaming_1 Aug 14 '16 at 12:12
  • @TomHale If you really need the tags file to be in the .git directory you could try specifying the relative path to it like this, but that is an untested feature: call project_tags#add_language('.git/mytagsfile', ['js', 'php', 'py']) – still_dreaming_1 Aug 14 '16 at 12:25
  • @still_dreaming_1, can I use jsctags to generate ctags for js files and use normal ctags for other files? – Aaron Shen Sep 18 '16 at 0:46

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.