Emacs etags: a quick introduction

Published on 2010-11-23
Tagged: emacs

Yesterday, I finally got around to learning how to use etags in Emacs. I can't believe I waited this long to learn this feature. etags lets you quickly locate a definition by its name anywhere in your code base.

Before you can use etags, you have to build a database called a tags table. This is done by running the etags command on every source file in your code base.

find . -name '*.c' -exec etags -a {} \;

etags will write to a file called TAGS in the current directory. The -a option indicates that it should append extra symbols to the file, rather than replacing it. You can also use -u to update the symbols for a file, but this is slower than -a if you're processing your whole code base again.

Once the TAGS file is built, you can load it with M-x visit-tags-table. This allows you to have different databases for different projects. To look up a definition, just type M-., then enter the name of the symbol. By default, Emacs will look up the symbol the point is currently over.

Although this definition look-up is pretty great, it isn't nearly as good as modern IDEs. It would be nice if you could load a TAGS file based on the current directory by searching parent directories. It would also be great if the TAGS file could be automatically updated when a file is changed, maybe using a hook in Emacs or some inotify-based mechanism in Linux.