LinuxCommandLibrary

ctags

Create tag files for source code

TLDR

Generate tags for a single file and output them to a file named "tags" in the current directory, overwriting the file if it exists

$ ctags [path/to/file]
copy

Generate tags for all files in the current directory and output them to a specific file, overwriting the file if it exists
$ ctags -f [path/to/file] *
copy

Generate tags for all files in the current directory and all subdirectories
$ ctags --recurse
copy

Generate tags for a single file and output them with start line number and end line number in JSON format
$ ctags --fields=+ne --output-format=json [path/to/file]
copy

SYNOPSIS

ctags [-VabBdfFghI:L:NopRstuWwx] [-e] [-f tagsfile] [-G] [-n] [--exclude=glob] [--languages[=LANG]] [files...]

PARAMETERS

-a
    Append to existing tags file

-b
    Make tagsfile a backup before writing it

-B
    Use backward scanning (default forward)

-d
    Include macro definitions as tags

-e
    Emacs-compatible tags file format

-f tagsfile
    Use specified file for tags (default: tags, -f - for stdout)

-F
    Use forward scanning

-G
    Ignore files matched by ignore patterns

-h symbols
    Limit tag nesting by symbol types

-I file
    File with suppression patterns

-L file
    Read file list from file

-n
    Line numbers instead of search patterns

-N
    Do not use NUL trick for faster searches

-o
    Alias for -f

-p
    Use relative path for source files

-R
    Recursive scan of directories

-t
    Include typedefs

-u
    Update existing tags file

-V
    Display version and exit

-v
    Long-form output

-w
    Suppress warnings

-W file
    Read suppression list from file

-x
    xref output format (like v but more columns)

--exclude=glob
    Exclude files matching glob

--languages[=LANG]
    Process only specified languages

DESCRIPTION

Ctags is a command-line utility that parses source code files and generates a tags file, an index of language objects such as functions, classes, variables, and macros. This enables fast navigation in editors like Vim, Emacs, and IDEs by allowing jumps to definitions via commands like Ctrl-] in Vim or M-. in Emacs.

Originally designed for C programs, modern implementations support dozens of languages including C++, Java, Python, JavaScript, Ruby, and more, with language-specific parsers identifying scopes, access levels, and inheritance. Users run ctags on directories or files to build the index, which is typically named tags and stored in the project root.

Key benefits include efficient browsing of large codebases without full indexing overhead. Options allow recursive scanning (-R ), excluding patterns, custom fields, and output formats like JSON. Regenerating the tags file after code changes keeps navigation accurate. Universal Ctags, the current standard, extends the original with plugin support and improved parsers.

CAVEATS

Tags files grow large in big projects; regenerate after code changes. Parser accuracy varies by language. Not all syntaxes fully supported.

EXAMPLES

ctags -R .
Generate tags recursively in current directory.

ctags -R --exclude='*.js' --languages=C,C++ src/
Tags for C/C++ only, excluding JS files.

EDITOR USAGE

Vim: set tags=./tags,tags; then :tag func or Ctrl-].
Emacs: visit-tags-table then M-. on symbol.

HISTORY

Created in 1975 by Ken Arnold for early vi editor. Enhanced by Exuberant Ctags (1995-2015) by Darren Hiebert, adding multi-language support. Forked as Universal Ctags (2015+) with ongoing development for modern languages and extensibility.

SEE ALSO

etags(1), gtags(1), vim(1), emacs(1)

Copied to clipboard