Not long ago we discussed ctags here, and how it can quickly let you navigate your code, helping you find definitions of variables and functions. You'll have taken notice that sometimes you want to navigate in the reverse: where is this function called, who includes this file, etc. Cscope builds a database of such information (and more). We can include it with ctags and have very impressive code searching.

If you want a detailed introduction, there is a great cscope and vim tutorial available. I'll just give you a quick run down of using it.

  1. At the base of your C or C++ source tree run cscope -R -b -q

  2. Add this to your .vimrc:

    if has("cscope")
      set cst
      set csto=1
      if filereadable("cscope.out")
        cs add cscope out
  3. Start Vim in the same directory as the generated cscope.out file. (If not, just do :cs add <cscope.out file>).

  4. To find where a function is called do :cs find c <function>. To find where a symbol is defined do :cs find s <symbol. You'll notice that if there are multiple matches a menu pops up to let you pick. This is the case for using CTRL-] on symbols as well.

  5. Make some handy mappings:

    nmap <C-\>s :cs find s <C-R>=expand("<cword>")<CR><CR>
    nmap <C-\>c :cs find c <C-R>=expand("<cword>")<CR><CR>

Cscope can do many other searches. See the tutorial or just type :he cscope in Vim.

Posted: Apr 18, 2008 | Tags: programming tips vim

Comments are closed.


42 | django | python | oggify | OSCON | OSCON07 | osx | utosc | mythtv | security | reading | music | gaming | programming | tips | shell | vim | pyobjc | cocoa | iphone | blog | git


<< Apr >>
Mo Tu We Th Fr Sa Su
31 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 1 2 3 4


This space reserved for a search box

A Django site. Hosted on a Slicehost Slice