Creating tags using etags and find…


etags is a command that allows you to generate emacs tags for your own code. Simply go to the directory where your source files are located and run the command:

etags filenames

The problem I’m having with this is, etags cannot build a TAGS file across multiple directories. I’ve tried like hell to see if etags can recursively look into my directory tree, but I can’t find anything online.

I did find a solution for my predicament, but it is far too complicated for my liking. It involves using the find command, which for some reason, is totally unlike any other unix command. So, lets see a quick find tutorial for our purposes.

find consists of four sections (for our purposes), which are

find path search-text action

The path is the starting directory for the recursive search; search-text is the file to find, and the criteria for search, i.e. search by name, or size or a few other crazy options; and action is what you want to do with the file. I only went so far as to see how to display files on console, which is done by the action -print. For a full find tutorial, Google it.

Thus an example find usage is:

find . -name “*.txt” -print

The double quotes around *.txt is something to do with the shell trying to expand wildcards, I don’t get it and I don’t have the patience to find out.

Getting back to the business at hand, we need to give etags a list of all the C and header files we want to tag. To do this, enter your directory tree, and type the command:

find . -name “*.[chCH]” -print | etags –

The pipe re-directs the output of the find command to etags, which proceeds to create the TAG file, and voilà we’re done.

An Aside:
Firstly, I was surprised that etags wouldn’t build TAGS recursively. Code of even moderate complexity is usually written in different directories. While the makers might have thought that users would be smart enough to use pipes, being user-friendly wouldn’t hurt.

Secondly, this was the first time ls let me down. ls -R lists all directories and their contents recursively, but asking it to display files of only one type won’t work.
For example, ls -R *.c will not print all the c files in the sub-directories. Due to this, I had to learn the find command, which frankly, is too complicated and difficult to grasp.

There is always the possibility that I missed a trick here, and I would be glad if someone would point it out. Also, if anyone has an easier way for creating TAGS, please let me know. It would be greatly appreciated.



4 Responses to “Creating tags using etags and find…”

  1. KedarS Says:

    That is because the Unix Philosophy is to build utilities (building boxes) that do one thing and do it well.

    Once you have those, you would be amazed at the kind of stuff that can be built with these.

  2. Don Pedro the Viking Says:

    Brilliant. Just the trick I was looking for. I was also surprised by the fact that etags did not work recursively. I think etags often works as a part of a make framework which executes etags in each subdirectory.

  3. John Gunderman Says:

    I tend to use exuberant-ctags instead of etags, because it supports a greater range of languages than etags and also supports recursive tag generation.

  4. narayana Says:

    Thanks a lot. This is an excellent and well written information which I was exactly looking for

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: