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.
- The Tiny Internet Project, Part I
- Machine Learning with Python
- SUSECON 2016: Where Technology Reigns Supreme
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Free Today: September Issue of Linux Journal (Retail value: $5.99)
- Bitcoin on Amazon! Sort of...
- Securing the Programmer
- Android Browser Security--What You Haven't Been Told
- The Many Paths to a Solution
Pick up any e-commerce web or mobile app today, and you’ll be holding a mashup of interconnected applications and services from a variety of different providers. For instance, when you connect to Amazon’s e-commerce app, cookies, tags and pixels that are monitored by solutions like Exact Target, BazaarVoice, Bing, Shopzilla, Liveramp and Google Tag Manager track every action you take. You’re presented with special offers and coupons based on your viewing and buying patterns. If you find something you want for your birthday, a third party manages your wish list, which you can share through multiple social- media outlets or email to a friend. When you select something to buy, you find yourself presented with similar items as kind suggestions. And when you finally check out, you’re offered the ability to pay with promo codes, gifts cards, PayPal or a variety of credit cards.Get the Guide