How to get ctags working inside vim
Asked Answered
R

5

10

I'm new to vim and wanted to get ctags integration working so I can more easily navigate a large java project.

I've pulled down the zip from source forge and extracted it but from here I'm not sure how to get it working with vim

Any help for a novice vim user would be great!

Responsive answered 4/4, 2011 at 18:29 Comment(1)
The posted answers all have their individual merits, but do note the -R option - it's much more common to invoke ctags recursively on an entire directory structure than to run it on individual files.Manizales
P
4

This is what I'm doing:

  1. ctags -n -f [OUTPUT] [SOURCE] to generate the tags (NOTE: the -n applies to me but may not be necessary for your usage)
  2. exec "set tags=" . [OUTPUT] inside of .vimrc to let vim become of aware of the tags

EDIT: I'm using

  • Exuberant Ctags 5.5.2
  • VIM 6.1

Additional info:

  • See ctags usages here
  • Tips and tricks from SO
Peeved answered 4/4, 2011 at 18:38 Comment(6)
Why do you override the default output name? (You wouldn't have to override the value of the tags option in vim if you used the default.) Why do you use -n instead of letting ctags use its normal excmd output? (The normal format is much, much more robust against changes in your source code.)Manizales
I have multiple releases that I work in and I can "change" the release that I work in. Specifying a filename allows me to automatically switch the tag file that I'm working with since it's based off of an environment variable. The -n is used since I work with C code and based on the manual that's already what's used anyways.Peeved
Have another look at the manual. -n is not default. If you don't specify it, it uses pattern, not number (except for macro definition tags). Easy enough to verify for yourself. And as the manual says, using numbers means that if you change the source code, it'll jump to the wrong line.Manizales
The default from what I read is mixed which as it states is essentially numbers for C code from what I read. Am I misinterpreting it?Peeved
Ahh, it seems that pattern is used for mixed and numbers only for C macros. You may want to read the pros/cons of each method and decide on what's best. I think I choose numbers because of duplicate entries but I can't remember for certain. For reference, using numbers the tag file is ~9.2MB otherwise it's ~13.4MB if file size is at all relevant.Peeved
Yeah, numbers do have plenty of benefits, but I find that as I change source code a lot, the numbers miss a decent amount of the time. I'm not really concerned about a few MB, just the ctags working right. I can totally understand preferring the numbers - if you rename things, they'll still work, while the patterns won't.Manizales
H
15

As nobody has given one critical function in these answers, I'll provide one more slightly superior answer.

The easiest way to use ctags with vim is by calling:

ctags -R *

from the root of your source repository. This will generate a tags file in that same directory.

In your ~/.vimrc file, add this short block:

 " ctags optimization
 set autochdir
 set tags=tags;

" denotes a comment. set autochdir tells vim that if it doesn't find a tags file in the $PWD it will look in the directory parent for the tags file, recursively. set tags=tags; tells vim that the name of your tags file will always be the same as the default tags file generated by ctags.

So long as you run ctags -R * in your root source directory the first time and occasionally to update it (if you pull new changes from others) then you'll always have a fast and intuitive ctags symbol lookup in vim.

Harrietharriett answered 27/5, 2014 at 19:51 Comment(3)
This doesn't make tags for subdirectories under it.Meaningless
Hey Zen, ctags -R * recurses through the directory tree under PWD and places all of the tags into a single file, ./tags. You only need one tags file for your whole repository.Harrietharriett
I had nothing but problems with autochdir so instead I have this setting: autocmd BufRead,BufEnter * silent! lcd %:p:h:gs/ /\\ /. This still "misses" when using -tab-remote* arguments to Vim but is good enough for 99% of my needs. (I just have to :e the files manually when this doesn't trigger.)Aniseikonia
W
7

Using exuberant ctags, I use something like this in my project's base directory (excluding the "log" directory):

ctags -R --exclude=log *
Wightman answered 5/4, 2011 at 1:5 Comment(0)
C
6

You have to run the ctags command with the source files as arguments. This will create a tags file containing all information. Then you can open a file with vim, and e.g. press Ctrl-] when on a line with a function to jump to the code of that function. If vi isn't started in the same directory as the tag file, you can set it with :set tags=<file>

Costmary answered 4/4, 2011 at 18:40 Comment(0)
P
4

This is what I'm doing:

  1. ctags -n -f [OUTPUT] [SOURCE] to generate the tags (NOTE: the -n applies to me but may not be necessary for your usage)
  2. exec "set tags=" . [OUTPUT] inside of .vimrc to let vim become of aware of the tags

EDIT: I'm using

  • Exuberant Ctags 5.5.2
  • VIM 6.1

Additional info:

  • See ctags usages here
  • Tips and tricks from SO
Peeved answered 4/4, 2011 at 18:38 Comment(6)
Why do you override the default output name? (You wouldn't have to override the value of the tags option in vim if you used the default.) Why do you use -n instead of letting ctags use its normal excmd output? (The normal format is much, much more robust against changes in your source code.)Manizales
I have multiple releases that I work in and I can "change" the release that I work in. Specifying a filename allows me to automatically switch the tag file that I'm working with since it's based off of an environment variable. The -n is used since I work with C code and based on the manual that's already what's used anyways.Peeved
Have another look at the manual. -n is not default. If you don't specify it, it uses pattern, not number (except for macro definition tags). Easy enough to verify for yourself. And as the manual says, using numbers means that if you change the source code, it'll jump to the wrong line.Manizales
The default from what I read is mixed which as it states is essentially numbers for C code from what I read. Am I misinterpreting it?Peeved
Ahh, it seems that pattern is used for mixed and numbers only for C macros. You may want to read the pros/cons of each method and decide on what's best. I think I choose numbers because of duplicate entries but I can't remember for certain. For reference, using numbers the tag file is ~9.2MB otherwise it's ~13.4MB if file size is at all relevant.Peeved
Yeah, numbers do have plenty of benefits, but I find that as I change source code a lot, the numbers miss a decent amount of the time. I'm not really concerned about a few MB, just the ctags working right. I can totally understand preferring the numbers - if you rename things, they'll still work, while the patterns won't.Manizales
S
2

look at this article: vim-easytags. i haven't tried this, but it looks quite good. manually creating and updating tags was really annoying. hope this will help. :)

Schlesinger answered 5/4, 2011 at 9:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.