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:
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.
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.