vim & cscope

vim

vim what an editor! I am yet to find someone who used this editor but is neutral. Either folks love it or hate it. I think that is sign of a truly remarkable product, in sense it connects to core- evoking emotional reaction. It was not long ago i was in vim WTF camp. But had to spend a few weeks on linux and re-discovered vim. After about a month of usage vim is one of my preferred editors for code browsing- even on windows. :) Learning and getting used to vim took some time- especailly it was learning not to fight normal and insert mode. One my left little finger developed flesh memory of hitting esc key every now and then- life became much better with vim.

The other thing that makes vim awesome is plugin support and number of plugins available. I have been using ctags for quite some time- but started using cscope recently and love it. For mid sized projects it is quite fast and snappy and the code database makes all sorts of queries possible. Now that i found that it integrates well with vim. This post is about getting cscope hooked up in vim.

cscope

As I said I discovered cscope a few weeks ago, ya i was living under a rock. For the efficieny with which it makes source browsing possible I don’t think now i can live without it. It is one of the plugins that integrates so well with vim. Other similar plugin is ctags but cscope is it’s superset thus makes sense to go with it. Steps to set up cscope with vim

$ cscope -bqk
$ cd ~\.vim\plugins
$ wget http://cscope.sourceforge.net/cscope_maps.vim

We are ensuring that cscope.out has been generated already in your project directory, cscope -bqk command does that. Then go to vim plugins directory, create one if it does not exists, and wget the cscope_maps.vim there. Once you have this start vim go to your vimrc, which should be located at ~\.vimrc and add following lines:

if has('cscope')
    set cscopetag cscopeverbose

    if has('quickfix')
        set cscopequickfix=s-,c-,d-,i-,t-,e-
    endif

    cnoreabbrev csa cs add
    cnoreabbrev csf cs find
    cnoreabbrev csk cs kill
    cnoreabbrev csr cs reset
    cnoreabbrev css cs show
    cnoreabbrev csh cs help

    command -nargs=0 Cscope cs add $VIMSRC/src/cscope.out $VIMSRC/src
endif

restart vim and you are all set now. For demo i have git repository checked out and i would like to surf some code. Once i add cscope database vim will use that for symbol navigation. The way to add symbol database, assuming it is named cscope.out, in vim is to

:cscope add cscope.out

After this once you move cursor over a function you would like to see it definition just press Ctrl+] and voila it will be opened for you. To get back to previous location press Ctrl+T See this link and this one for further details and the cscopes_map.vim file has wealth of information about key mappings.