Exuberant ctags make it possible to jump to the definition of a class, method, variable and any other language object in vim. Tool is able to generate an index file (a.k.a tag file) for one of 41 supported programming languages. Index can be used by editors like vim to quickly find related keyword.
Installation shouldn’t be hard on any Linux based operating system. In Ubuntu exuberant ctags are in the default package repository. To install it you can use Ubuntu Software Center or open a terminal and write:
sudo aptitude install exuberant-ctags
Generating an index file
Before taking advantage of ctags in vim you need to generate an index file. ctags command is made for that (it’s an alias for ctags-exuberant).
To generate an index first change the working directory to the main directory of your project. After that tell ctags to recursively index the current directory:
cd ~/workspace/projects/sympal ctags -R --languages=php .
Note: Read 'man ctags' for more options.
Note: Long time ago Matthew Weier O'Phinney wrote a blog post about exuberant ctags with PHP in vim. He used several options which look like no longer needed. I think ctags support for PHP has been improved over the time.
Basic usage boils down to the two commands:
- ctrl-] takes you to the declaration of a keyword your cursor is currently over. Jump is made so it doesn’t matter in which file it is defined. Keyword is put on the tag stack.
- ctrl-t takes you a step back in the tag stack.
Notice that you can precede the command with a number to invoke it several times. It’s useful when you have several definitions of the same tag and you don’t want to jump to the first one (2ctrl-]). From time to time you’ll also need to jump back several tags (4ctrl-t).
You can make that vim directly opens a chosen keyword with -t option:
vim -t sfGuardUser
Some of the tag stack commands:
- pop jumps backward in the tag stack
- tag jumps forward in the tag stack
- tags displays a tag stack
Some of the tag match list commands:
- tselelect lists the tags that match its argument or keyword under the cursor
- g] works like the ctrl-] but lets you to choose the tag occurrence
- :tnext, :tprevious, :tfirst and :tlast jump through the tag occurrences
Some of the search commands:
- [i and ]i display the first line containing the keyword under and after the cursor
- [I and ]I display all lines containing the keyword under and after the cursor
- [ ctrl-i and ] ctr-i jumps to the first line that contains the keyword under and after the cursor
Note: Write :help tags-and-searches in vim to get total overview of features offered by tags.