Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top