Tuesday, March 25, 2008

My ctags lightning talk

Last week at the Raleigh.rb we had an evening of lightning talks. I presented
a handy utility called exuberant ctags for indexing source code and showed
how nicely it plays with Ruby and Vim. Here's a recap of my 5 minute lightning
talk (along with a few points I failed to mention).

# From the top level of your project
ctags --recurse

Once it finishes there will be a file called 'tags' in your working directory.
In my case vim loads tag files automatically if it finds them in the same
directory from which it was launched. If for some reason your version does not
do this for Ruby files you can type the following in command mode:

set tags=[path to your tags file]

To make the change permanent add the following to your vimrc:

# Look for a tags file in vim's working directory.
au BufRead,BufNewFile *.rb set tags=tags

In both cases a comma separated list is accepted. This is nice if you are
working on multiple projects.

ctags works with most real text editors though I'm only experienced with the vim
integration. Basically, once vim has loaded the tags file you can place
the cursor on top of any word and press cntrl - ] to jump to the
declaration. In some cases users will notice their terminal also has that
specific key combination bound. Under gnome-terminal I have to press cntl-shift - ] to
access my tags. In the case where more than one tag matches a selection
mode will be displayed. Usually it's fairly obvious which one you want.

Also worth mentioning is vim's tagstack. It works pretty much as expected. You push down as far as you
want and then use cntrl - t to pop back.


Obviously this sort of indexing faces challenges when working with a language
as dynamic as Ruby. Things that trip it up are method aliases and any fancy

ctags vs rtags

There is a Ruby ctags alternative called rtags. In my opinion ctags works better:
  1. ctags is several orders of magnitude faster that rtags. On my laptop a 100,000 SLOC project is indexed in less than one second. rtags takes about a minute to do the same.
  2. ctags supports dozens of languages. Run ctags --list-languages to see them all.
  3. rtags has trouble parsing files on most Ruby projects I work on. This is what led me to switch back to ctags in the first place.
  4. rtags claims to be more aware of Ruby syntax though in practice I find it indexes many things I just don't care about.

No comments:


my google reader feed