UsingCtags

Using ctags to find functions

Finding functions in X is hard. One way to search for the actual definition of a data type is to grep the source directory and then open the file. This can take forever, especially when you don't quite know where to look for.

However, vim's support for ctags makes it easier. It is possible to create a tags file for the whole system and then just use it from within vim. That way, in vim you only have to go to the occurence of the data type, press CTRL+] and it will open the matching definition. With CTRL+T you jump back to the original file.

I created my tags file somewhere in my .vim directory.

$> mkdir .vim/tags/
$> cd .vim/tags/
$> ctags -R /usr/include/* /path/to/X/source/code

ctags will create a file "tags" in the current directory ($HOME/.vim/tags in this case). This way I got pretty much all defintions I need at the moment.

Now you need to tell vim to include this file. Add the following line to your $HOME/.vimrc.

set tags=./tags,tags,/home/username/.vim/tags/tags

On your next startup of vim, everything will be available with CTRL+]. If you use tags heavily, you will find CTRL+G helpful. It shows the name of the file in the current buffer.

A recommendation is to write a little script to update your ctags and run it as a cron job every night. Your computer will not be very responsive while recursively searching for ctags in a multi-GB directory.

Warning

This can be a hazardous setup as the ctags are absolute. If you are working on two different source trees (i.e. two releases of the same software), using CTRL+ ] will jump to the functions as defined in ctags. So you might be editing the wrong source tree.