Saturday, December 27, 2008

Vim: ctags

ctags is a tool that figures out where various functions, classes, etc. are defined. Using ctags, you can use a hot key to jump to the definition of the symbol under the cursor.

To get started, install exuberant-ctags. In Ubuntu, this is just "apt-get install exuberant-ctags". Now, from within Vim:
:cd project_root
:!ctags -R .
:set tags=tags
To jump to the definition of the symbol under the cursor, use cntl-]. To get back to where you were, use cntl-o.

There's also a taglist plugin for Vim. Once you install that, you can use ":TlistToggle" to open up a window on the left that shows all the things defined in your open files. I have that mapped to "T" by putting the following in my .vimrc: "map T :TlistToggle<CR>".

Thanks to Benjamin Sergeant for helping me get started with ctags.

2 comments:

Erich said...

My problem with ctags is this: with Python (and possibly other languages) it's a pain in the butt to get to the proper definition sometimes. Particularly with duck typing and/or inheritance. If I'm trying to find the definition for the method Goose.fly, I could end up there, or at Duck.fly or Parrot.fly. Vim certainly doesn't make it easy to go to the NEXT .fly method if the first one is incorrect. I have yet to find a good workaround for this.

Shannon -jj Behrens said...

That's a good point. Certainly with inheritance and duck-typing, you might need to examine all of the implementations.

I just looked at "help tags". There's some useful stuff in the "Tag match list" section.