Tuesday, January 19, 2010

Using ctags with Vim

I have been using vim for all my coding related tasks for a year now. I heard of ctags a while ago but I never tried using it. But recently I got tired of moving between files manually -looking for functions and other stuff- so I decided to give ctags a try. I never expected for it to be so simple. Just install ctags:
sudo apt-get install ctags
Generate tags file. To do this go to the main directory of your source tree. There just issue the command:
ctags -R
this will recursively traverse the source directory and generate a tags file containing information about all symbols -functions and variable- in the source files.
Using Vim with ctags is straight forward. Vim has builtin support for ctags so now you just need to issue vim command from the directory where you generated the tags file using ctags.
vim src/asdf/test.c
Now in the file move a cursor to some function or variable and press Ctrl-]. This will take you to the definition of that function or variable. To go back to where you came from press Ctrl-T, this will take you back to where your were before pressing Ctrl-]

If you have more than one tags files or you dont want to always use vim from one directory then add following line to your .vimrc file in your home directory:
set tags=tags;/

No comments: