ctags — Create tag files for ex, more, and vi

Format

ctags [–aBFwx] [–f tagfile] sourcefile …

Description

ctags creates a file named tags in the current directory. It summarizes the locations of various objects in the C source files named on the command line. All files with a .c or .h suffix are treated as C source files.

For C source code, ctags summarizes function, macro and typedef definitions. See tags — Format of the tags file for a description of the format of the tags file.

The tags file is used by ex, more, and vi to support the tag command. The tag command can be used to edit the file containing a name in the tags file.

For ex and vi, the command is:
: tag name
For more, the command is:
:tname

After these commands are run, the tags file is searched for name. If it is found, the file associated in the tags file with that name is loaded and the line containing the name is made the current line.

Options

–a
Appends output to the existing tags file rather than overwriting the file.
–B
Produces a tags file that searches backward from the current position to find the pattern matching the tag.
–F
Searches for tag patterns in the forward direction. This is the default.
–f
Generates a file named tagfile rather than the default tags file.
–w
Suppresses warning messages.
–x
Produces a report on the standard output. The report gives the definition name, the line number of where it appears in the file, the name of the file in which it appears, and the text of that line. ctags arranges this output in columns and sorts it in order by tag name according to the current locale's collation sequence. This option does not produce a tags file.

Localization

ctags uses the following localization environment variables:
  • LANG
  • LC_ALL
  • LC_COLLECT
  • LC_CTYPE
  • LC_MESSAGES
  • LC_TIME

See Localization for more information.

Files

ctags uses the following file:
tags
Output tags file

Usage notes

  1. It can be difficult to recognize a function definition in C source code. Because ctags does not know which C preprocessor symbols are defined, there may be some misplaced function definition information if sections of code within #if...#endif are not complete blocks.
  2. ctags invokes the sort internally.
  3. ctags makes special provision for the main() function, which may occur in several C source files. The tags file contains an entry for the first main() routine found. For all occurrences of main(), including the first, the tags file contains an entry for Mname, where name is the name of the input source file, with the .c suffix and any leading pathname components removed. For example, a tags file created for a C source code file named foo.c would contain an entry for Mfoo, which represents the main() routine in foo.c).
  4. ctags uses sort to sort the file by tag name, according to the POSIX locale's collation sequence.

Exit values

0
Successful completion
1
Failure due to any of the following:
  • Unknown command-line option
  • Cannot create the output file
  • Cannot open the output file
  • One of the input files was unrecognized

Portability

POSIX.2, X/Open Portability Guide, 4.2BSD and higher.

This utility only understands characters from the POSIX locale.

The –B, –F, and –w options are extensions to the POSIX and XPG standards.

Related information

more, sort, vi

See the tags file format description in tags — Format of the tags file.