Originally published by Robert Beisert at fortcollinsprogram.robert-beisert.com

CTags: Lightning Fast Project Editing Made Easy

What’s the fastest way to get from a function call back to its definition during the editing process? If you’re using something like Notepad or an IDE, the answer is usually to search for the file containing the function, open it, and search your way down to the definition. Unfortunately, if you want to step into a definition inside THAT function, you get to start the process all over.

Or you could use the vi editor and install ctags.

How it Works

Ctags is a script designed to read through code and generate a file of tags linked to each object and function. If you run the script recursively, you get a tag file containing the references of everything under the current directory. This means that all the work of finding files and line numbers has already been done.

The VI editor has a bunch of functionality related to tag files. For instance, if you want to go from one instance of a function to its declaration, you can use ^] (control-]) to jump directly into that function’s definition. You can the do whatever editing you want (or keep stepping through it with ^]), save, and return to the previous level with ^t. This lets you quickly jump across directories and files to get to whatever you want, then quickly get back to where you were.

Or, if you don’t know where a function is (but you know its name), you can use vi -t to jump directly to that definition. For example, if you’re looking for a function named “Panda_eats_apples(struct panda, int apples)”, you just type:

vi -t Panda_eats_apples

…and you’re there. Easy as that. The same applies to structures: it’ll take you to the definition whether it’s in a header or code file.

There’s just one caveat to all this: ctags can only recurse downward, not upward. This means that if you might want to bounce between directories, you need to run ctags in the root directory and open files from that directory. Suppose you have a project whose root directory looks like this:





You couldn’t navigate into Pears\ and get your tags: Pears\ won’t have a tag file, and if you run ctags again inside Pears\ the result will only contain tags for things inside of Pears\. If you needed to bounce into Apples\, you would have to get to a file in Pears\ this way:

vi Pears\<filename>

If you do it this way, you can quickly jump around the entire project without grief.

It’s a powerful tool that makes me easily 1000% more efficient. Install ctags now and get to work.

photo by: