Ctags
Template:Short description
Template:Lowercase title
Template:More citations needed
{{#invoke:Infobox|infobox}}Template:Template other {{#invoke:Check for unknown parameters|check|unknown=Template:Main other|preview=Page using Template:Infobox software with unknown parameter "_VALUE_"|ignoreblank=y| AsOf | author | background | bodystyle | caption | collapsetext | collapsible | developer | discontinued | engine | engines | genre | included with | language | language count | language footnote | latest preview date | latest preview version | latest release date | latest release version | latest_preview_date | latest_preview_version | latest_release_date | latest_release_version | licence | license | logo | logo alt | logo caption | logo class | logo size | logo title | logo upright | logo_alt | logo_caption | logo_class | logo_size | logo_title | logo_upright | middleware | module | name | operating system | operating_system | other_names | platform | programming language | programming_language | qid | released | replaced_by | replaces | repo | screenshot | screenshot alt | screenshot class | screenshot size | screenshot title | screenshot upright | screenshot_alt | screenshot_class | screenshot_size | screenshot_upright | service_name | size | standard | title | ver layout | website }}Template:Main other
ctags is a shell command that generates an index (tag) 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 <syntaxhighlight lang="text" class="" style="" inline="1">ctags</syntaxhighlight> was introduced in BSD Unix 2.0<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref><ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> 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:
- Atom
- BBEdit 8+
- CodeLite (via built-in ctagsd language server)<ref>{{#invoke:citation/CS1|citation
|CitationClass=web }}</ref>
- Cloud9 IDE (uses it internally but does not expose it)
- CygnusEd<ref>{{#invoke:citation/CS1|citation
|CitationClass=web }}</ref>
- Emacs and XEmacs
- EmEditor Professional
- Far Manager (via Ctags Source Navigator plugin)
- Geany
- Gedit (via gedit-symbol-browser-plugin)
- JED
- jEdit (via plugins CodeBrowser, Tags, ClassBrowser, CtagsSideKick, or Jump)
- JOE
- KDevelop
- Kate
- mcedit (Midnight Commander builtin editor)
- NEdit
- Notepad++ (via OpenCTags plug-in)
- QDevelop
- TSE (via macro)
- TextMate (via CodeBrowser-PlugIn)
- UltraEdit
- TextPad
- VEDIT
- vi (and derivatives such as Elvis, Nvi, Vim, vile, etc.)
- Visual Studio Code<ref>Workspace Symbols, Python for Visual Studio Code Docs</ref>
- Xedit (X11)
Variants
Original
The original <syntaxhighlight lang="text" class="" style="" inline="1">ctags</syntaxhighlight> 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.<ref name=exu-fmt>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> A tags file, normally named "tags", consists of lines formatted as:
{tagname}\t{tagfile}\t{tagaddress}
The fields are:
- Template:Mono} Template:Ndash Identifier; not containing white space
- Template:Mono Template:Ndash Exactly one tab (Template:Mono) character, although many versions of vi can handle any amount of white space
- Template:Mono} Template:Ndash The name of the file where Template:Mono} is defined, relative to the current directory
- Template:Mono} Template:Ndash An ex mode command that will take the editor to the location of the tag. To protect against execution of arbitrary commands (for example, commands that modify files), POSIX implementations of vi only allow a search command or a line number here.
The lines are sorted on Template:Mono} 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:
{tagname}\t{tagfile}\t{tagaddress}[;"\t{tagfield...}]
The fields up to and including Template:Mono} are the same as for the original ctags format. Optional, additional fields (indicated by square brackets) include:
- Template:Mono Template:Ndash semicolon + double quote: Ends the Template:Mono} in a way that looks like the start of a comment to vi or ex
- Template:Mono} Template:Ndash 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.<ref name=exu-fmt/>
etags
GNU Emacs comes with two <syntaxhighlight lang="text" class="" style="" inline="1">ctags</syntaxhighlight> variants, <syntaxhighlight lang="text" class="" style="" inline="1">etags</syntaxhighlight> and <syntaxhighlight lang="text" class="" style="" inline="1">ctags</syntaxhighlight>, which are built from the same source code. <syntaxhighlight lang="text" class="" style="" inline="1">etags</syntaxhighlight> generates a tag table file for Emacs, and <syntaxhighlight lang="text" class="" style="" inline="1">ctags</syntaxhighlight> creates a tags file for vi. Some options (e.g. <syntaxhighlight lang="text" class="" style="" inline="1"> --language</syntaxhighlight>) are accepted by both <syntaxhighlight lang="text" class="" style="" inline="1">ctags</syntaxhighlight> and <syntaxhighlight lang="text" class="" style="" inline="1">etags</syntaxhighlight>. Options which only make sense when generating a vi-style tags file are not accepted (and are therefore reported as errors) by <syntaxhighlight lang="text" class="" style="" inline="1">etags</syntaxhighlight>. Similarly <syntaxhighlight lang="text" class="" style="" inline="1">ctags</syntaxhighlight> does not accept options which are pertinent only to generating a tag table file for Emacs.<ref>Template:Man</ref>
For Emacs, the tags file is normally named "TAGS". The file consists of multiple sections Template:Endash 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 (the first two bytes make up a magic number):
\x0c
{src_file},{size_of_tag_definition_data_in_bytes}
Note, Template:Mono represents the byte in hexadecimal Template:Mono. Every line ends with a line feed (LF, Template:Mono = Template:Mono).
The header is followed by tag definitions, one definition per line, with the format:
{tag_definition_text}\x7f{tagname}\x01{line_number},{byte_offset}
Template:Mono 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,<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> was initially distributed with Vim, but became a separate project upon the release of Vim 6. It includes support for Emacs and <syntaxhighlight lang="text" class="" style="" inline="1">etags</syntaxhighlight> compatibility.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref><ref>Template:Man</ref>
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.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>
Creates a ctags-compatible tag file for Haskell source files.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> It includes support for creating Emacs etags files.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>
A ctags-compatible solution specialized for JavaScript using the CommonJS packaging system.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> It outperforms Exuberant Ctags for JavaScript code, finding more tags than the latter.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>
Example
Given a single line Template:Mono source code:
#define CCC(x)
The ctags tag file looks like:
CCC( test.c 1
The etags tag file (TAGS) looks like this:
\x0c test.c,21 #define CCC(\x7fCCC\x011,0