How to use Tags or Ctags to Browse Code with Vim

This is a short tutorial about using ctags (or exuberant ctags) to work with the vim editor to give you a great code browser. Vi and it’s successor Vim have a built-in feature to allow you to jump from a use of a function call to the definition of that function call. It’s a good way to learn about a library of code: load up some example code, and then read it, jumping from the function calls to the definitions.

To use tags, you must first install ctags or exuberant-ctags. They are in the repositories. To run it, go to the root of your source tree and type:

ctags -R *

That recursively scans the code and produces a database of function definitions, known as tags. It outputs a single file called “tags” into the directory.

Then, in your ~/.vimrc, add:

tags=tags;../tags;../../tags;../../../tags

That line causes vim to search for the file named tags in the current directory and a few levels of parent directory.

To read code using tags, move the cursor to a function call. Press Ctrl-]. That takes you to the definition, and also pushes your current location onto the “tag stack”.

To back out of the definition, press Ctrl-o.

That’s most of what you need to know.