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.
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- The Italian Army Switches to LibreOffice
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Petros Koutoupis' RapidDisk
- Linux Mint 18
- Oracle vs. Google: Round 2
- The FBI and the Mozilla Foundation Lock Horns over Known Security Hole
- Varnish Software's Varnish Massive Storage Engine
- Privacy and the New Math
- Ben Rady's Serverless Single Page Apps (The Pragmatic Programmers)
Until recently, IBM’s Power Platform was looked upon as being the system that hosted IBM’s flavor of UNIX and proprietary operating system called IBM i. These servers often are found in medium-size businesses running ERP, CRM and financials for on-premise customers. By enabling the Power platform to run the Linux OS, IBM now has positioned Power to be the platform of choice for those already running Linux that are facing scalability issues, especially customers looking at analytics, big data or cloud computing.
￼Running Linux on IBM’s Power hardware offers some obvious benefits, including improved processing speed and memory bandwidth, inherent security, and simpler deployment and management. But if you look beyond the impressive architecture, you’ll also find an open ecosystem that has given rise to a strong, innovative community, as well as an inventory of system and network management applications that really help leverage the benefits offered by running Linux on Power.Get the Guide