ArticleslgStudy

computer science

Ctags

Ctags is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Ctags rather than just read about it. In short: 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.

Key takeaways

  • Ctags belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Ctags to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Ctags from memory before moving on to harder problems.

Reference excerpt

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 ctags 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:

Variants

Original The original ctags 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:

{tagname}\t{tagfile}\t{tagaddress}

The fields are:

{tagname} – Identifier not containing white space \t – Exactly one tab (\x09) character, although many versions of vi can handle any amount of white space {tagfile} – The name of the file where {tagname} is defined, relative to the current directory {tagaddress} – 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 {tagname} 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 {tagaddress} are the same as for the original ctags format. Optional, additional fields (indicated by square brackets) include:

;" – semicolon + double quote: Ends the {tagaddress} in a way that looks like the start of a comment to vi or ex {tagfield} – 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 ctags variants, etags and ctags, which are built from the same source code. etags generates a tag table file for Emacs, and ctags creates a tags file for vi. Some options (e.g. --language) are accepted by both ctags and etags. Options which only make sense when generating a vi-style tags file are not accepted (and are therefore reported as errors) by etags. Similarly ctags 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 (the first two bytes make up a magic number):

\x0c {src_file},{size_of_tag_definition_data_in_bytes}

Note, \x## represents the byte in hexadecimal ##. Every line ends with a line feed (LF, \n = \x0A). The header is followed by tag definitions, one definition per line, with the format:

{tag_definition_text}\x7f{tagname}\x01{line_number},{byte_offset}

{tagname}\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 etags 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.

Hasktags Creates a ctags-compatible tag file for Haskell source files. It includes support for creating Emacs etags files.

jsctags 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 test.c 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

See also GNU GLOBAL

References

External links

ctags: create a tags file – Shell and Utilities Reference, The Single UNIX Specification, Version 5 from The Open Group Universal Ctags homepage Exuberant Ctags homepage Ctags on VMS source code for Emacs vtags.el module

Worked examples

Example 1 — a first encounter with Ctags

Start with the simplest possible case. Write down what Ctags claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Ctags before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Ctags ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Ctags

In research
Ctags appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Ctags in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Ctags is common in secondary-school and first-year university syllabi. It links to neighbouring topics Code comprehension tools, Code navigation tools, Free computer programming tools, so understanding it makes those chapters shorter.
In everyday life
Look for Ctags outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Ctags” →

How to study Ctags in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Ctags means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Ctags out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Ctags in simple terms?

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 th…

Why does Ctags matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Ctags?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Ctags.

Tags

  • Code comprehension tools
  • Code navigation tools
  • Free computer programming tools
  • Software using the BSD license
  • Standard Unix programs
  • Unix SUS2008 utilities
  • Unix programming tools

Keep exploring