At work I deal with a large code base. Taking into consideration that I'm often still familiarizing myself with the code, you can imagine that it isn't easy finding things. An average day working in the code used to involve me trying to determine what a bit-field flag means, which involves tracking down a #define somewhere in the code. Worse yet are the various functions I've seen around and now need to understand what they're doing, leaving me to find where they're doing it.

Now at first I was running commands like egrep -R '^some_function_name' * or egrep -R '^#define \+FLAG' *. This usually worked, but it meant I would then have to then open the file up in my vim session. This took away a lot of time, and I knew that there was a better way.

ctags is a program that will go through code and determine where things are defined and write a tag file. ctags can generate tag files for a host of programming languages, and the best part? Vim knows how to deal with them.

  1. Generate a ctags file. find src/directory -name '*.c' -o -name '*.h' | ctags -f - tags
  2. Run vim from the directory with the generated tags file.
  3. See something you don't recognize? Move the cursor over it and CTRL-] will jump you to the definition. CTRL-O will jump you back.
  4. If vim says something about this being tag 1 of 3, you can jump to other definitions by adding a count to you tag command. 1 CTRL-] jumps to the first definition (or the current definition if you're over one). 2 CTRL-] jumps to the second, and so on.
  5. Tried of typing in long paths, but know the function you want to work on? Start vim in the directory without a file name, the just run :tag function_name and you're there.

I'm currently using this for C and Perl, and I'm not sure how I've lived without it for so long. Even though I know the code a lot better than when I started, navigating by tags is so much faster than opening a file and leaves me more time to remember more important things like which argument is the source and destination of a strcpy, it will always be worth using.

Posted: Feb 24, 2008 | Tags: programming tips

Comments are closed.


42 | django | python | oggify | OSCON | OSCON07 | osx | utosc | mythtv | security | reading | music | gaming | programming | tips | shell | vim | pyobjc | cocoa | iphone | blog | git


<< Feb >>
Mo Tu We Th Fr Sa Su
28 29 30 31 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 1 2


This space reserved for a search box

A Django site. Hosted on a Slicehost Slice