C projects have thousands of lines of code divided into hundreds in some cases thousands of files. In order to access various function definitions within the source code repository effectively using a VIM editor, there is a great need of source browsing tools. ctags is one of the popular source browser tools. Though it has been around for many years, even many seasoned Linux developers are unaware of its abilities and usefulness.
ctags works by looking at all the source files for a piece of code and finding the lines which contain symbol definitions – that is, it locates the places where the functions, variables and so on are defined. It has two distinct advantages over a more general approach using something like grep: firstly ctags understands the syntax of many languages, which allows it to find the symbols very reliably. Secondly, its output is a file in a format which can be used by many of today’s more powerful text editors to jump straight to required symbol definitions. Even in a huge piece of code spread out over hundreds of source files, finding a function is never more than a couple of keystrokes away.
Installing ctags package
Almost all the Linux distributions ship ctags by default. if ctags is not already installed use the following commands
On Ubuntu Linux machines
$ sudo apt-get install exuberant-ctags
Generating tags for your source tree
ctags builds what is known as a “tags file” (also sometimes called a “tags table”). This is a plain text file which contains one line per symbol in the source code. Creating a tags file involves a parse over the entire source code tree. Each source file will be opened, its language ascertained, then examined for symbols of interest. A tags file is a static table and therefore requires rebuilding as the source code changes. Exactly how frequently this needs to happen depends on the piece of code and the nature of the changes which are happening to it. Fortunately, ctags parses code and builds tags files very quickly, so a tags file rebuild is rarely an inconvenience.
$ cd proj_source $ ctags -R *
A tag is an index entry within a “tags file” of a programming language identifier. The tags indicate the locations (in terms of file/line number pairs) where the identifier is defined and referenced. Programming language identifiers include functions, structure and union names, type names, and variable names (essentially, anything that is entered into a compiler’s symbol table), as well as macros. The information from this tags file can be loaded into a suitably capable text editor. When the user wants to find the definition of a given function, class or another symbol, they just hit the right button, type in the name of the required symbol, and the text editor can jump straight to it, opening the file if necessary.
Using ctags with vim editor
We have 3 different ways to use ctags in VIM editor.
From Shell:
We can invoke directly the file or the files containing the definition for a particular function from the shell.
We can invoke directly the file or the files containing the definition for a particular function from the shell.
vim -t function_name
OR
VIM command line:
We can invoke from VIM command line (in command mode) the definition of a function.
We can invoke from VIM command line (in command mode) the definition of a function.
:set tags=~/proj_source/tags :tag function_name or :ta function_name
This will jump automatically from the current file to the file containing the function_name definition.
OR
By cursor position:
This option is more user-friendly as we use a key combination instead of giving commands.
This option is more user-friendly as we use a key combination instead of giving commands.
ctrl + ]
Place the cursor on the first character of the function name and press ctrl-]. This will jump to the file containing the definition of function_name.
The following command will jump back to the previous location.
ctrl + t
use the following command to search and Jump to a tag name found by a search.
:tag /search-string
When multiple entries exist in the tags file, such as a function declaration in a header file and a function definition (the function itself), the operator can choose by issuing this command. The user will be presented with all the references to the function and the user will be prompted to enter the number associated with the appropriate one.
:tselect function-name
Following command to Jump to next matching tag.
:tnext or :tn
Jump to previous matching tag.
:tprevious or :tp
Jump to first matching tag.
:tfirst or :tf or :trewindor:tr
Jump to last matching tag.
:tlast or :tl
To traverse forward through the tag history.
ctrl + i
To traverse back through the tag history.
ctrl + o
To display the list of tags that we have traversed in past give the following command.
:tags
Finding global identifiers
when you are editing a C program and wonder if a variable is declared as “int” or “unsigned”. A quick way to find this is with the “[I” command. Suppose the cursor is on the word “column” then type
[ + shiftkey + i
Vim will list the matching lines it can find. Not only in the current file, but also in all included files (and files included in them, etc.). The result looks like this:
structs.h 1: 29 unsigned column; /* column number */