Emacs27
2.2.5Using Tags
As an application grows in size, it also grows in complexity. As the number of subroutines,
variables, functions and files increases, it becomes much more difficult to keep track of every
piece and to quickly find the portion of code that one needs to work on. To address this issue,
Emacs has the ability to read a file that contains a table of tags that reference various parts of an
application.
These tags are stored in a
TAGS
file that is created by the
etags
command. Once this file
is built, Emacs can read this table and use it to quickly locate a specific portion of code, regard-
less of whether it is in a currently open file or not.
From the command line the
etags
command is issued with a list of file names to be read:
$ etags *.[ch]
This command will read all files in the current directory that have a
.c
or
.h
extension
and build a tags table file in the current directory. The output file is, by default, called
TAGS
.
To build a single
TAGS
table for a project that has source code spread through any number
of subdirectories, the following command may be used:
$ find . –name \*.[ch] | xargs etags –
Just be aware if there are too many files, then this command may need to be run several
times with varying arguments in order to build a complete table. To do this, you must use the
–append option to the
etags
command as shown:
$ find . –name \*.c | xargs etags –
$ find . –name \*.h | xargs etags –-append –
Any of these commands may be issued from within Emacs itself by using the
M-!
com-
mand to issue a shell command. Simply type
ESC !
and press return.
Once you have built a
TAGS
table, it must first be read into Emacs before you can use it to
search. To accomplish this, type
M-x visit-tags-table
, specify the location of the
TAGS
file to be read in, and then the name of the
TAGS
file. The default value for the location is the
current working directory, and the default tags file name is
TAGS
”.
Once the
TAGS
file has been read in, you may issue search commands against the table.
There are several commands that can be used. The one most commonly used is
ESC .
which
searches for a tag that matches the search parameter. The default search parameter is the word at
the current pointer position. For example, if the pointer were on the character string
search_function
, the default search string that Emacs presents would be
search_function
.
If you are not sure of the name of the function that you are looking for, you can type the
first few characters of the function name and press the
TAB
key. This invokes the completion
function of Emacs and, if the function is unique, it will display that function name. If the func-
tion name is not unique, Emacs will complete as much of the function name as it can and then
prompt you for the rest. Pressing
TAB
again after Emacs has completed as much of the function
Next Page >>
<< Previous Page
Back to the Table of Contents