VI and Ctags Makes Browsing Code Easier

Vim/GVim has great features to make it easy to read C (and PHP and Perl) code. They treat your code like a hypertext or web page, where you can jump between files easily.

First is the "go to file" feature. If you move the cursor onto a file name, and type gf, you'll be taken to that file. To return to the previous file, press Control-o. The file opens are kept on a stack, so you can drill down into file, and climb back out. This is useful for reading code with "includes".

gfgo to ffile under the cursor, and push the current file onto the file stack
Control-oPop the file from the file stack and return to it.
Second is the "tags" feature. If you have a body of code written in C, you can use the "ctags" command to generate an index of function definitions. This is stored in a file named "tags" in the root directory.

On Debian or Ubuntu, to install ctags, do: sudo apt-get install exuberant-ctags

When you move the cursor onto a word, and press Control-], it looks up the word in tags, and opens up the file with the definition, and puts the cursor at the definition. The tags are kept on a stack, and you can back out of the file by pressing Control-t. This is useful if you're reading a header file or some code, and want to know what a function does.

Sometimes, a tag will have multiple definitions. You usually discover this when you use C-] and go to a useless line. You can find an alternative tag definition by typing :ts and picking the correct tag definition.

Alternatively, you can type g] and get a list of the tags defined for the word under the cursor. Use this if you're aware that the tag has multiple definitions.

Control-]Follow tag. Looks for the word under the cursor in the tags database, and open the file where the tag is defined. Pushes current file onto the tag stack.
Control-tPop file from tag stack and open it.
:tsShow and select tag definitions for the word on the top of the tag stack. Use this if you follow a tag and the definition seems wrong.
g]Show and select tag definitions for the word under the cursor.
The exuberant-ctags package (on Debian) also known as etags, is a ctags that works for PHP, Perl, Java and a lot of other languages.

When you install exuberant-ctags it will install two commands ctags and etags. Calling it at ctags will produce a "tags" file in the classic format, which is used by vim. Calling it as etags will produce a "TAGS" file using a newer binary format used by emacs. So, use "ctags".

Here are some sample commands:

ctags -R
Recursively build the tags file from all the sources starting with the current directory.
ctags -R *
Run this in the Drupal root directory to build the tags file. Drupal's code uses the .inc suffix on files in the includes directory.

Generally, I'll create a script to refresh the tags file, and make sure ALL the files I'm using are included in the database. As the project grows, I re-run the script to rebuild the db. Simple.

Here's a script to rebuild the tags file for Drupal:

#! /bin/bash
cd ~/Src/drupal-6.19/
ctags -R *