March 14, 2025

Black Pill boards - F411 USB -- Cscope tutorial

Installing it on my Fedora system is easy: "dnf install cscope". To exit from it you type Ctrl-D.

Cscope is ancient! It began at Bell Labs prior to 2000 and is now open source. It is a curses based text UI.

At first, I turned away from Cscope because it had its own interface. I liked ctags because there was good Vim support and essentially ctags becomes just a nice Vim extension once you add a few key mappings. I now learn that Vim generally has the cscope plugin enabled and built in.

Also note that there is a man page for cscope, so type "man cscope" and gawk at all the options.

I want to follow the "Cscope with vim" tutorial, so I do as they say, download cscope_maps.vim, then do:

cd .vim
mkdir plugin
cp xxx/cscope_maps.vim plugin
Also add this to your .bashrc (and type it into your current terminal window).
export CSCOPE_EDITOR=vim
I will try using it in my "Hydra" project.
What you could do now, is this:
cd Hydra
cscope -Rb
The "b" flag says to just build the database and exit without starting the Cscope UI. You could type just "cscope -R" and it would build the database and launch the UI. Then later you just type "cscope" to lauch the UI.

Better yet, you may want to use "cscope -Rqb". The "q" flag tells cscope to also build an inverted index, which can speed things up on large source trees. Best yet is to use "cscope -Rqcb". Here the "c" flag says to not compress data.

Now to start cscope, just type cscope -- or maybe "cscope -d" where the -d says to not regenerate the database. Or better yet, setup vim as below and just start vim like you always do.

Now I have a database and Cscope works. I use the arrow keys to go up and down and try various searches. I use the tab key to go between the search results list and the search menu. When I hit return on an item in the search list, it opens that in an editor (nano by default). I want to change this to Vim, so I exit and do what I describe above to set the CSCOPE_EDITOR variable.

Things work pretty well now, but I get an error about a duplicate cscope database from the plugin. I comment out the offending line in cscope_maps.vim and now things are good.

Starting from Vim

Start vim on some file, put the cursor on an item of interest and type Ctrl-\s (the Control-\ followed by a lower case "s".) I get a menu with all the uses of the item and I type the number I want followed by a return and I go there. Control-t unwinds this (a "back" button), just like ctags.

If the search is unambiguous, vim just goes there without a silly one item menu. And you can even search on code that has been commented out.

Supposedly Ctrl-space followed by "s" should do a window split, putting the search results in a new window. This does not work for me. If it ever does work, use Ctrl-W x to manage these split windows, where "x" is one of: "w" to move between windows, hnkl to move as per usual vim motion, and "c" or "q" to close/quit the whole split window business.

Vim has quite a bit of stuff built in to support Cscope. Use ":help cscope" which takes you to if_cscop.txt and/or type ":cscope help".

Use my ctags related muscle memory

It is possible to make vim use cscope first, then fall back on ctags when I type Ctrl-] (as I have taught myself to do). There is a vim flag "cst" that controls this. Type ":help cst and :help csto" to learn about this. Note that you use :set cst? to ask for the value of a variable. I find cst set and csto set to zero, but I still get ctags behavior from Ctrl-]. Maybe this is OK (having different keys for cscope and ctags).

More

I suspect there are newer and better vim plugins and things to be learned in that arena. I want this to be a tool, not yet another research project, so I won't dig deeply for now.
Feedback? Questions? Drop me a line!

Tom's Computer Info / tom@mmto.org