0% found this document useful (0 votes)
16 views3 pages

Ctags Notes

Uploaded by

Bhima Game
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Ctags Notes

Uploaded by

Bhima Game
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Notes for ctags

Background
Imagine a big project having 100 .c files, 50 .h files, spread across 20 source/include directories.
Given a function, how do you remember which file implements it?
Given a function, how do you find all places from which it is called?
In short, how do you navigate in the code?

Do you use grep, find, and complex Linux commands each time for this?

There are two main alternatives to manual search - ctags and cscope (There are many other
tools too)

Here we discuss ctags.

ctags is basically an index data file keeping mapping between each symbol and the place where
it is defined in the source code directory structure.

When you load this index data in vi, vi can allow you to browse conveniently.

Installing ctags
$ sudo apt install universal-ctags

Generating the index data file


This command generates an index file named ‘tags’ with all symbols in code recursively
downwards from directory “.” (current dir). You can give another directory path instead of “.” too

$ ctags -R .
Loading the index data in vi
$ vi file1.c # relevant fille name
In vi run below command
:set tags=<path-of-tag-file>
In above command path-of-tag-file can be absolute or relative path of tags file

e.g. you can go to linux-prog-tools/ctags/project/main/src, run “vi main.c”, and then run below
command
:set tags=../../tags

Navigating
Assume you have opened ‘main.c’ as mentioned above, and loaded the tags.

You can bring the cursor to the function call of “mod1_func”, and press Ctrl-].
You will notice vi opens the file where mod1_func is defined.

Now you can position the cursor on mod2_func call. Press Ctrl-]. You reach the definition of
mod2_func.

Function call stack as of now: main() -> mod1_func() -> mod2_func().

Press Ctrl-t. You will return backwards to the calling place i.e. you reach mod1_func()
Function call stack as of now: main() -> mod1_func()

Press Ctrl-t. You will return backwards to the calling place i.e. you reach main()
Function call stack as of now: main()

There are many more commands and shortcuts in vi using ctags.


e.g. you can give vi command
:ts mod4_func
Above command will jump to mod4_func() definition

There are many more commands which we may not cover.

For further study, see references or search Google/Youtube


References
1. https://courses.cs.washington.edu/courses/cse451/10au/tutorials/tutorial_ctags.html

You might also like