How can I run ctags in a large code base?
Asked Answered
C

2

5

Assuming I have a code directory structure as follows:

/top  
   /second  
       /core  
           a.pl  
           b.pl  
           c.pl  
       /common  
           d.pl  
           e.pl  
       /util  
           f.pl  
           g.pl  
           h.pl    

Where should I run the ctags so that I can jump to function definitions via vi?

For example I had:

/dir
   /perl  
      a.pl  

and I run in dir the command ctags -R perl but in a.pl I could not jump to a function definition that existed in the same file.
If I did ctags -R . inside the perl directory it worked.

So I can not understand the pattern. Should I run ctags in core, common,util? What if my code base is huge? Would I really need to run it in each directory?

Celina answered 19/5, 2013 at 15:14 Comment(4)
Which ctags are you running? If you are running Exuberant Ctags, they have an FAQ about exactly this topic.Becharm
Since you use ctags in Vim, I recommend you not to deal with ctags manually, but try plugin Indexer (goo.gl/lNrB5) which painlessly generates tags for the whole project(s) and keeps tags up-to-date. You can also check my answer to this question goo.gl/dMavX for a bit more details.Maggy
@msw:I don't know.Those installed in cygwin.How do I check this?Celina
@Celina on my system ctags --version says "Exuberant Ctags...". Based on the Cygwin manifest it appears likely that that's what you've got too.Becharm
S
6

Normally you would use just one tags file located in the root directory of your project.

For example, to tag the whole of Vim's own source code, located in ~/src/vim-7.3, you navigate to that directory and generate the tags file there.

$ cd ~/src/vim-7.3
$ ctags -R

That's all.

You can do the same from inside Vim, of course. Start Vim and then type:

:cd ~/src/vim-7.3
:!ctags -R

Check that you have the correct 'tags' setting. Default is ./tags,tags, meaning Vim will look for a tags file in the current directory.

Make sure to read :h 'tags' in its entirety. You can instruct Vim to look for tags files deep in the directory tree by giving patterns containing wildcards.

One more thing: For this approach to work, Vim's current working directory must remain at the root at all times. If you have some option like 'autochdir' set or constantly switch directories yourself, then the tags obviously won't be found.

Statutable answered 19/5, 2013 at 15:20 Comment(9)
When I type :h 'tags' I get: E433: No tags file E149: Sorry, no help for 'tags' Press ENTER or type command to continue Celina
@Celina That sounds like you have a serious problem with your runtime files. Does :help work at all?Statutable
If I type :help I get: E433: No tags file E149: Sorry, no help for help.txt Press ENTER or type command to continue Celina
Also set syntax on does not work.In case it means something.Celina
@Celina I'm sorry but that means you haven't installed Vim correctly. Can you reinstall? No need to bother with ctags until your Vim is up and running.Statutable
I am not sure I understand your latest update.How do I know that Vim's current working directory is at the root at all timesCelina
@Celina In my example, :pwd should output /home/glts/src/vim-7.3 at all times. That's the root directory of the project and that's were the tags file is.Statutable
let us continue this discussion in chatStatutable
:If you have time could you please check out my other question since you also worked on cygwin recently :) superuser.com/questions/597293/…Celina
O
9

Your tags file should be generated in the first common ancestor of your code (that would be second, in your case) with $ ctags -R ..

I'd suggest you add this line to your ~/.vimrc in order to make Vim always find your tags file, no matter where you are in your project and no matter what the "current directory" is:

set tags=./tags;/,tags;/

It works like the one in @glts's answer with an interesting twist: because of the ;/ part, Vim looks up and up until / for a tags file. Supposing you are editing g.pl, Vim will correctly use your tags file located in second.

:h tags
:h ctags
Obstetrician answered 19/5, 2013 at 15:59 Comment(2)
Nice guess with the ,/ but it doesn't work like that in Vim. There's no walking up the directory tree, unfortunately. And you can actually use ctags -R to mean ctags -R .!Statutable
It's ;/ not ,/ (the / is not required but more explicit so I keep it here) and yes it works like that. See :h file-searching, specifically the "upward search" part.Obstetrician
S
6

Normally you would use just one tags file located in the root directory of your project.

For example, to tag the whole of Vim's own source code, located in ~/src/vim-7.3, you navigate to that directory and generate the tags file there.

$ cd ~/src/vim-7.3
$ ctags -R

That's all.

You can do the same from inside Vim, of course. Start Vim and then type:

:cd ~/src/vim-7.3
:!ctags -R

Check that you have the correct 'tags' setting. Default is ./tags,tags, meaning Vim will look for a tags file in the current directory.

Make sure to read :h 'tags' in its entirety. You can instruct Vim to look for tags files deep in the directory tree by giving patterns containing wildcards.

One more thing: For this approach to work, Vim's current working directory must remain at the root at all times. If you have some option like 'autochdir' set or constantly switch directories yourself, then the tags obviously won't be found.

Statutable answered 19/5, 2013 at 15:20 Comment(9)
When I type :h 'tags' I get: E433: No tags file E149: Sorry, no help for 'tags' Press ENTER or type command to continue Celina
@Celina That sounds like you have a serious problem with your runtime files. Does :help work at all?Statutable
If I type :help I get: E433: No tags file E149: Sorry, no help for help.txt Press ENTER or type command to continue Celina
Also set syntax on does not work.In case it means something.Celina
@Celina I'm sorry but that means you haven't installed Vim correctly. Can you reinstall? No need to bother with ctags until your Vim is up and running.Statutable
I am not sure I understand your latest update.How do I know that Vim's current working directory is at the root at all timesCelina
@Celina In my example, :pwd should output /home/glts/src/vim-7.3 at all times. That's the root directory of the project and that's were the tags file is.Statutable
let us continue this discussion in chatStatutable
:If you have time could you please check out my other question since you also worked on cygwin recently :) superuser.com/questions/597293/…Celina

© 2022 - 2024 — McMap. All rights reserved.