You've probably used hypertext links to browse the Web, skipping easily between pages with simple keystrokes or mouse clicks. With etags you can browse your source programs and documentation just as easily. In this article I'll describe tag files and the etags and ctags commands that generate them. I'll also look at how less, Emacs and vi use tag files to make your editing easier and more productive.
By default, etags takes a collection of source files and generates an index of the definitions of all the global symbols of interest. For C and C++ programs, this index includes global and member functions, classes, structures, enums, typedefs and #defines. For (La)TeX documents, it indexes chapters, sections, subsections, figures, equations etc. etags also includes built-in support for assembler, Fortran, lex, Lisp, Pascal, Sheme and yac. etags --help will give a complete list of languages supported by your version.
Try running etags *.c *.h in one of your source directories. After it finishes processing, you'll find that it has produced a file called TAGS. This is a simple text file, with a block of entries for each file parsed. Each block contains a line for each definition in the file, with the text of the definition and the line number and absolute character position on which it was found.
How do you use this? Well, the simplest way is from within Emacs. You start editing as normal in the source directory. When you come across a call to one of your functions, and want to see how it was defined, position the cursor on the call, and enter the Emacs command M-. (press the esc key followed by a period, or hold alt and period down at the same time). Emacs will display the name of the function in the mini-buffer. If you press return to confirm, Emacs will automatically open the file containing the definition and position the cursor at its start. Suppose while looking through that function, you are puzzled by a declaration which uses a typedef name. Move the cursor to it, press M-H. again, and up pops the typedef. If you can't find the a #define you created, use M-., then type the macro name in the minibuffer. Emacs looks it up in the tags table and takes you straight to it.
Sometimes you'll have more than one tag containing the same text, and M-. will take you to the wrong place. Simply keep pressing C-u M-., and Emacs will move through all possible tag matches. If you're finger-tied like me, you may find M-0 M-. a simpler way to enter a prefix for the find-tag command. Entering a negative prefix (C-u -M.) takes you back to a previous definition.
That's not all you can do with the TAGS file. Imagine that when you were typing in some source you decided to give your functions long, descriptive names. It seemed like a good idea at the time but now the glow is wearing off as you're typing a call to that function for the nth time. Again, tags can help. Type the first few characters of the name, then press M-tab. Either the full function name will appear, or a window will pop up displaying a list of possible completions. (This might not work for XEmacs users, and you may have to re-bind M-tab to tag-complete-symbol.)
You can also use tag completion in the minibuffer whenever a command prompts for a tag—type in the first few characters, press tab, and Emacs will do the rest.
If you're an XEmacs user, the M-?i command displays a single line summary of a function or typedef in the modeline—really useful if you've forgotten the calling sequence!
Tags are also a great way to perform search and replace operations across all your source files. The tags-search command prompts you for a regular expression, then displays the first match in any of the files in your TAGS table. You can move on to successive matches using M-, (that's meta-comma). The tags-query-replace performs a egex-replace across all the files in the TAGS table. You can stop it at any point (with escape or C-g) and later resume it with M-,.
Finally, the tags-apropos lists in a separate window all tags matching the regular expression that you enter—a great way to scan for stuff in a hurry.
In the same way that etags works with Emacs, the ctags program generates tag files that can be used with vi, view and less. The basic operation of the command is the same, but it generates a file called tags (in lower case). Once it's finished, you can go straight from the command line to a particular function in either vi or less using vi -tname or less -tname. You don't even have to give a file name!
If you're editing with vi, you can move to a tag using the :tag command.
|Natalie Rusk's Scratch Coding Cards (No Starch Press)||Feb 17, 2017|
|Own Your DNS Data||Feb 16, 2017|
|IGEL Universal Desktop Converter||Feb 15, 2017|
|Simple Server Hardening||Feb 14, 2017|
|Server Technology's HDOT Alt-Phase Switched POPS PDU||Feb 13, 2017|
|Bash Shell Script: Building a Better March Madness Bracket||Feb 09, 2017|
- Own Your DNS Data
- Simple Server Hardening
- Understanding Firewalld in Multi-Zone Configurations
- Teradici's Cloud Access Platform: "Plug & Play" Cloud for the Enterprise
- From vs. to + for Microsoft and Linux
- The Weather Outside Is Frightful (Or Is It?)
- IGEL Universal Desktop Converter
- Bash Shell Script: Building a Better March Madness Bracket
- Natalie Rusk's Scratch Coding Cards (No Starch Press)
- Linux Makes Wi-Fi Happen in New York City