Ctags Notes
Ctags Notes
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)
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
$ 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.
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()