Skip navigation.
Home

Using Cscope in Vim

Generate a cscope database

$ cscope -b -R

For Linux kernel and some other packages which have built-in cscope target in Makefile:

$ make cscope

Configure Vim to locate a cscope database:

:cs add cscope.out

My cscope settings in ~/.vimrc:

"cscope settings
if has("cscope") && filereadable("/usr/bin/cscope")
   set csprg=/usr/bin/cscope
   set csto=0
   set cst
   set nocsverb
   " add any database in current directory
   if filereadable("cscope.out")
      cs add cscope.out
   elseif filereadable("../cscope.out")
      cs add ../cscope.out ..
   elseif filereadable("../../cscope.out")
      cs add ../../cscope.out ../..
   elseif filereadable("../../../cscope.out")
      cs add ../../../cscope.out ../../..
   elseif filereadable("../../../../cscope.out")
      cs add ../../../../cscope.out ../../../..
   elseif filereadable("../../../../../cscope.out")
      cs add ../../../../../cscope.out ../../../../..
   elseif filereadable("../../../../../../cscope.out")
      cs add ../../../../../../cscope.out ../../../../..
   " else add database pointed to by environment
   elseif $CSCOPE_DB != ""
      cs add $CSCOPE_DB
   endif
   set csverb
endif

" Using 'CTRL-backslash' then a search type makes the vim window
" split horizontally, with search result displayed in the new window.

nmap <C-\>s :scs find s <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>g :scs find g <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>c :scs find c <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>t :scs find t <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>e :scs find e <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>f :scs find f <C-R>=expand("<cfile>")<CR><CR>
nmap <C-\>i :scs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
nmap <C-\>d :scs find d <C-R>=expand("<cword>")<CR><CR>

Jump to a tag

  • :cs find d <tagname>
  • position the cursor over a tag name and then press:
    Ctrl-\ d
  • $ vim -t <tagname>

Come back from a tag jump

  • :pop
  • Ctrl-t

Reference

  1. Cscope Home Page.
  2. The Vim/Cscope tutorial.