« Online Lectures From UC Berkeley | Main | Metrics »

ctags and cscope

If you're a programmer, and you use the excellent vim editor, you really need to get into ctags (the 'exuberant-ctags' package on debian, not the normal 'ctags' one) and cscope. These are both awesome tools, but there's plenty of info on making effective use of these tools for your own builds (cscope info here, and ":help ctags" for ctags, or google around), I thought I'd share a couple of other tid-bits on how I use them which have improved my productivity.

The Alias

Pretty simple really, I throw this:

alias t='ctags -R; find . -name "*.c" -o -name "*.cc" -o -name "*.hpp" -o -name "*.hh" -o -name "*.h" -o -name "*.cpp" -o -name "*.py" -o -name "*.pl" -o -name "*.pm" | cscope -Rb -i-'

into my .bashrc, and run 't' whenever I need to update my tags and cscope files. The -i argument to cscope works around issues you'd otherwise have with the file list being too long.

System-header tags

This one is a bit more useful. Add this to your .vimrc:

set tags=./tags,./TAGS,tags,TAGS,/usr/include/tags

and then go off and run the body of that 't' alias in /usr/include (or do it from cron if you want it to stay up to date). That way when you're trying to remember which header file it is that defines, for example IPPROTO_TCP (I can never remember that one), you're not reduced to grep -r, and vim will tab-complete when you do the search, and even remind you which IPPROTO's there are. For example, I fire up my vim and type ":ts IPPR" and then press ctrl-d, I'm greeted with this:

:ts IPPR
IPPROTO_AH        IPPROTO_HOPOPTS   IPPROTO_MAX       IPPROTO_ROUTING
IPPROTO_COMP      IPPROTO_ICMP      IPPROTO_MTP       IPPROTO_RSVP
IPPROTO_DSTOPTS   IPPROTO_ICMPV6    IPPROTO_NONE      IPPROTO_SCTP
IPPROTO_EGP       IPPROTO_IDP       IPPROTO_OSPF      IPPROTO_TCP
IPPROTO_ENCAP     IPPROTO_IGMP      IPPROTO_OSPF_LSA  IPPROTO_TP
IPPROTO_ESP       IPPROTO_IP        IPPROTO_PIM       IPPROTO_UDP
IPPROTO_FRAGMENT  IPPROTO_IPIP      IPPROTO_PUP       IPPROTO_VRRP
IPPROTO_GRE       IPPROTO_IPV6      IPPROTO_RAW

I can keep typing, or press tab to complete the first one and hit enter, which then tells me this:

  # pri kind tag               file
  1 F   e    IPPROTO_AH        /usr/include/linux/in.h
               IPPROTO_AH = 51,             /* Authentication Header protocol                   */
  2 F   e    IPPROTO_AH        /usr/include/netinet/in.h
               IPPROTO_AH = 51,       /* authentication header.  */
  3 F   d    IPPROTO_AH        /usr/include/netinet/in.h

That is, I have definitions for that constant in netinet/in.h and linux/in.h -- for maximum portability you should use the netinet version, but that's a lot easier than other ways I can think of to get that info. It also works for structure definitions and function prototypes, and if you want to dive into the definition itself, hit the number on the left (I've bolded it) and press enter. Zap, you're there. ctrl-t to bounce back. I also keep a copy of the Linux kernel source code indexed with ctags and cscope for when I do kernel development, I only pull that in when I need it though, because it generates a lot of noise for userspace stuff.

Make the best use of the tools you have; sometimes they're a distraction but used well, they can be a huge productivity boost.

Got a favourite tool? mail me about it.

About

This page contains a single entry from the blog posted on June 21, 2007 12:01 AM.

The previous post in this blog was Online Lectures From UC Berkeley.

The next post in this blog is Metrics.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.35