March 14, 2025

Black Pill boards - F411 USB -- Cscope and vim

Cscope and vim seem to work together, but not quite as I would like (or expect). So I decided to dig deeper. See the end of this page for the setup I arrived at.

I am running vim 9.1 on Fedora 41. Typing "vim --version gives plenty of information, including "+cscope".

Time to start reading.

The built in stuff is all accessed via :cscope or just :cs

:cs find
Here is a partial list of types.

s - find this C symbol
g - find this definition
d - find functions called by this function
c - find functions calling this function
t - find this text string
e - find this egrep pattern
This works, but it is awkward having to type (or copy/paste) the name to be searched for.

The :cstag command searches both the ctags database and the csource database. The order is determined by the setting of "csto"

It works like this:

:cst is the same as :cs find g

Variables

Some of these must be set in the .vimrc file and will have no effect is set within vim once it is running.
:set cst
:set nocst
This is a variable (long form cscopetag) with the same short name as the command above (just to confuse you).
:set csto=0
:set csto=1
This specifies the order for the :cstag command.
When set to zero, the cscope database(s) get looked at first.
When set to one, the ctags database(s) get looked at first.

You use the "add" command to tell vim about a cscope database. So far I find that I never need to do this, vim just finds one either in the current directory or a parent directory.

:cs add cscope.out
:cs show
The "show" command will tell you if vim has found your database.

My final setup

I am pretty happy with the following setup:

I modified (mostly trimmed down) the recommended plugin file I ended up with the following in .vim/plugin/cscope_maps.vim

if has("cscope")
    set cscopetag
    set csto=1
    set ttimeoutlen=600
    nmap  :cs find c =expand("")
    if $CSCOPE_DB != ""
        cs add $CSCOPE_DB
    endif
endif
I set csto to one so it uses ctags first to find things under the cursor when I press Ctrl-] (like it always did when I just used ctags).

I decided I didn't want Ctrl-\ to allow access to all the csource options, but just the "c" option (to find all the places that call the thing under the cursor). So my nmap does only that, and I get it with one keystroke, just like Ctrl-] to find definitions.
If I want all the csource stuff, I'll run csource outside of vim.

I often launch vim within a project somewhere, and it won't find the cscope.out database up in some parent directory. I found a fellow who published his fix for that and put it into my .vimrc.

This code looks like this:
function! LoadCscope()
  let db = findfile("cscope.out", ".;")
  if (!empty(db))
    let path = strpart(db, 0, match(db, "/cscope.out$"))
    set nocscopeverbose " suppress 'duplicate connection' error
    exe "cs add " . db . " " . path
    set cscopeverbose
  " else add the database pointed to by environment variable
  elseif $CSCOPE_DB != ""
    cs add $CSCOPE_DB
  endif
endfunction
au BufEnter /* call LoadCscope()

Feedback? Questions? Drop me a line!

Tom's Computer Info / tom@mmto.org