How To Get etags Working in Emacs

You’ve been using Emacs for how long and you haven’t figured this out yet?

Yup.  Up until now I’ve never really needed to actually get etags working in Emacs.  I remember trying to figure it out long ago as a newbie and giving up.  It’s initially difficult because it’s not as simple as just saying “etags my-src-dir”, though once you have a couple of UNIX tools on your utility belt though, it’s practically as simple as “etags my-src-dir”.

Just show me the code.

Although there might be other more correct ways to do this, the following works fine for PHP:

    cd ~/src-code-top
    find . -name '*.php' -print | xargs etags

Woah find and xargs?

etags (unfortunately) only works on files and not directories.  Those files also have to be arguments and not stdin.  So we need to use find to give us a list of file names and xargs to convert each filename into an argument for etags.

Explain find.

find is a swiss army knife of file search but mostly you can just memorize “find . -name ‘*.filetype‘ -print”.  find needs 3 arguments, the directory to start looking in, some condition, and some action. The “.” tells it to look starting from the current directory. The “-name” part means find matching filenames (We use the single-quotes to keep bash from expanding). Finally -print tells it to print what it found one line at a time.

Explain xargs.

xargs takes lines from stdin and supplies it as the arguments to some other program.  Just what we need for etags. Although xargs is as versitile as find we don’t need to supply it with anything, the default behavior is fine.

Ok, now how do I use it in Emacs?

Just use M-x visit-tags-file and point it to your newly minted TAGS file.  After that you can easily find out where the hell that class/function is hiding just by doing “M-.” (You can also return to the file you came from by pressing M-*).  Of course “M-.” and “M-*” work in a stack like manner so you can keep “M-.”ing to dig as deep as you need to and easy get back by pressing “M-*” and equal number of times.

One thought on “How To Get etags Working in Emacs

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>