Using ctags in Vim

Using ctags and Vim is a lethal combination, especially when doing bigger projects.

Using them gives you the following features:

  • Impressive class/function/method search
  • Ability to go to the definition of a function with one click (even in dynamic languages like Python)
  • Better overview by a module browser (where you can see classes and members)

Getting started with Taglist

Tag list opens a left window where you can browse the current file. It gives a nice overview. Here is an example (click for a bigger picture):

taglist_small

First download ctags. On Debian/Ubuntu it would be:

sudo apt-get install exuberant-ctags

After this, install the taglist.vim plugin, you can get it here.

Now you need to configure taglist.vim, this can be done like this:

let Tlist_Ctags_Cmd = "/usr/bin/ctags"
let Tlist_WinWidth = 50
map <F4> :TlistToggle<cr>

Pressing F4 will toggle the taglist window on and off. Try it out, it's really useful :)

For more options look in taglist.txt documentation, it comes when downloading taglist.vim.

Search and destroy using tags

I have one mapping in vimrc:

map <F8> :!/usr/bin/ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR>

This builds tags libs for the current working directory (it's super fast).

Once you have build tags, you can browse them using builtin functions. Here are some examples:

  • :tag getUser => Jump to getUser method
  • :tn (or tnext) => go to next search result
  • :tp (or tprev) => to to previous search result
  • :ts (or tselect) => List the current tags
  • => Go back to last tag location
  • +Left click => Go to definition of a method

If you want full power out of this I recommend reading following documentation:

  • http://vimdoc.sourceforge.net/htmldoc/tagsrch.html (official documentation)
  • http://www.vim.org/tips/tip.php?tip_id=94 (a vim tip)

Happy vim hacking.

15. Jul 2008 Code · Tips · VIM Editor
© Amir Salihefendic