When I have to browse large amounts of C code in a Linux terminal for an extended period of time, I use VIM with Cscope integrated into it, and this allows me to traverse symbol dependencies fairly easily. This blog entry explains how to incorporate this Cscope functionality into VIM in a Linux environment.
One nice feature of this setup described in this article is that it will allow you to use the cursor to select source code dependencies in VIM where you can type custom keystrokes to invoke powerful Cscope functionality. Not only that, it will allow you to invoke Cscope symbols from within any directory in your source code tree. That is because the Cscope database will be built from source-code file path-names referenced from your root directory instead of the standard present directory if you built the Cscope file without the necessary ‘trickery’.
OS program requirements
Cscope must be installed on your system. If you don’t have it, then install it by typing in the Terminal “yum install Cscope” if running Fedora or “sudo apt-get install cscope” if you are running Ubuntu.
Note: You must have at least VIM 6.x for the Cscope integration to work correctly.
Setting it up
You must have cscope_maps.vim. After installing Cscope, this file should be located in ‘$HOME/.vim/plugin’. If this directory does not exist, then create it.
Step 1)
Add the .vim plugin directory to your $PATH.
Add these lines to the end of $HOME/.bash_profile\r\n
PATH=$PATH:$HOME/.vim/plugin
EDITOR="vim"
Add the following code to the bottom of your $HOME/.bashrc file. This is the function and alias which builds the Cscope database.
function build_cscope_db_func() {
local PROJDIR=$PWD
cd /
find $PROJDIR -name \\''*.c\\'' -o -name \\''*.h\\'' > $PROJDIR/cscope.files
cd $PROJDIR
cscope -Rbkq
}
alias csbuild=\\''build_cscope_db_func\\''
Step 3)
In the parent directory of your project directories, create a script file for each project which you will be looking at. For example, I created a file called ‘MyApp_env.cscope’. Inside this file, create the environment variable to point to the Cscope database that you will be working on. See below. You could make this an alias if you wanted.
CSCOPE_DB=$HOME/ProjDir/cscope.out
export CSCOPE_DB
Using Cscope in Vim
Step 1)
Build the Cscope database. This has to be done each time a change is made to code, if desired.
Cd to your project directory and then type ‘csbuild’. This creates several files with Cscope.out being the only one you will need.
Step 2)
Go to the directory where you created the script file with the CSCOPE_DB env variable (MyApp_env.cscope) and then run it.
Step 3)
Open Vim and start using the new Cscope functionality and keycodes
There are many commands to start using cscope/vim, but here are a few. To get started, open a .c file and highlight a function call with the mouse. Type CTRL- and then ‘s’. This will search for the symbol and open the search results in an new horizontal pane in Vim. Two space-bars before the ‘s’ will open it vertically.
To browse from one window to another, type Ctrl-w and then arrow key to the window that you want to go, basic Vim stuff.
Other functions (s=find uses of symbol, g=find global def of symbol, c=find all calls to a func, f=open filename under cursor). There are more functions described in the cscope_maps.vim file.


