Ctags


ctags is a shell command that generates an index file of programmatic identifiers parsed from source code files of various programming languages to aid with code comprehension. The tags are often used by a source-code editor to lookup the definition of an identifier while the user develops the source code. Alternatively, the command supports an output format that is a human-readable cross reference.
The original was introduced in BSD Unix 2.0 and was written by Ken Arnold, with Fortran support by Jim Kleckner and Pascal support by Bill Joy. It is part of the initial release of Single Unix Specification and XPG4 of 1992.

Editor support

Tag index files are supported by many editors, including:

Original

The original command is generally not supported today but its functionality is still commonly available today via programs that are similar and possibly compatible. In particular, the original tag data format is still commonly used.
The original format, often used with vi and its clones as well as by the modern descendent programs Exuberant and Universal Ctags. A tags file, normally named "tags", consists of lines formatted as:
\t\t
The fields are:
  • Identifier not containing white space
  • Exactly one tab character, although many versions of vi can handle any amount of white space
  • The name of the file where is defined, relative to the current directory
  • An ex mode command that will take the editor to the location of the tag. To protect against execution of arbitrary commands, POSIX implementations of vi only allow a search command or a line number here.
The lines are sorted on which allows for fast searching.

Extended Ctags

The format used by Vim's Exuberant Ctags and Universal Ctags. These programs can generate an original ctags file format or an extended format that attempts to retain backward compatibility.
A file consists of lines formatted as:
\t\t
The fields up to and including are the same as for the original ctags format. Optional, additional fields include:
  • semicolon + double quote: Ends the in a way that looks like the start of a comment to vi or ex
  • extension fields: tab separated "key:value" pairs for more information
This format is compatible with non-POSIX vi as the additional data is interpreted as a comment. POSIX implementations of vi must be changed to support it, however.

etags

GNU Emacs comes with two variants, and, which are built from the same source code. generates a tag table file for Emacs, and creates a tags file for vi. Some options are accepted by both and. Options which only make sense when generating a vi-style tags file are not accepted by. Similarly does not accept options which are pertinent only to generating a tag table file for Emacs.
For Emacs, the tags file is normally named "TAGS". The file consists of multiple sections one section per input source file. Sections are plain-text with several non-printable ASCII characters used for special purposes. These characters are represented as underlined hexadecimal codes below.
A section starts with a two line header :
\x0c
,
Note, represents the byte in hexadecimal. Every line ends with a line feed.
The header is followed by tag definitions, one definition per line, with the format:
\x7f\x01,
can be omitted if the name of the tag can be deduced from the text at the tag definition.

Exuberant Ctags

Exuberant Ctags, written and maintained by Darren Hiebert until 2009, was initially distributed with Vim, but became a separate project upon the release of Vim 6. It includes support for Emacs and compatibility.
Exuberant Ctags includes support for over 40 programming languages with the ability to add support for even more using regular expressions.

Universal Ctags

Universal Ctags is a fork of Exuberant Ctags, with the objective of continuing its development. A few parsers are rewritten to better support the languages.
Creates a ctags-compatible tag file for Haskell source files. It includes support for creating Emacs etags files.
A ctags-compatible solution specialized for JavaScript using the CommonJS packaging system. It outperforms Exuberant Ctags for JavaScript code, finding more tags than the latter.

Example

Given a single line source code:
#define CCC
The ctags tag file looks like:

CCC looks like this:
\x0c
test.c,21
#define CCC(\x7fCCC\x011,0