Ctags and Cscope
ctags
and cscope
are the 2 most useful tools for source code browing and
editing, especially in Vim.
ctags - generate tag files for source code
Basic usage:
-
Install ctags using pacman (on Arch)
-
Enter the root directory of the source code, and generate tags by running
ctags -R
-
In vim, locate the cursor on variable or function names, then
-
use Ctrl+] to jump to the definition (push into the tag stack)
-
use Ctrl+T to jump back (pull from the stack)
-
Generate tags for C and C++ standard library (for Arch only):
-
Setup a folder ~/.vim/ctags
-
Run in ~/.vim/ctags
pacman -Ql glibc | awk '/\/usr\/include/{print $2}' > c_headers ctags -L c_headers --c-kinds=+p --fields=+iaS --extra=+q -f c pacman -Ql gcc | awk '/\/usr\/include/{print $2}' > c++_headers ctags -L c++_headers --c++-kinds=+p --fields=+iaS --extra=+q -f c++
-
Add the following lines in .vimrc
set tags+=~/.vim/ctags/c set tags+=~/.vim/ctags/c++
cscope - interactively examine a C program
Basic usage:
-
Install cscope using pacman (on Arch)
-
Enter the root directory of the source code, and generate cscope database by running
cscope -Rb
Note.
-
For operating system or C/C++ standard library development, use
cscope -Rbk
-
For lookup accleration, use
cscope -Rbq
-
-
In vim, first run
:cs add cscope.out
Then you can use various cscope commands. To see help, run
:cs
You may also have interest in this toturial http://cscope.sourceforge.net/cscope_vim_tutorial.html