Thursday, October 22, 2009

Cscope with vim

today I came to know the best way of using cscope with vim(I mean through ex editor commands).

SHORTCUT:
Ctrl-b will browse through history of cscope searches that u performed earlier at the cscope prompt.

first set the default editor to vim
export EDITOR=vim
  • Find this definition - :cs f g
  • Find functions called by this function - :cs f d
  • Find functions calling this function - :cs f c
  • Find this text string - :cs f t
  • Find this file - :cs f f
  • Find files #including this file - :cs f i
  • Find this C symbol - :cs f s
  • Find this egrep pattern - :cs f e
if you see problem in building the cscope database or facing problem like "E567: no cscope connections" then use:

:cs add cscope.out
as described under ":help cscope-howtouse"

Is the screen getting unresponsive/blocked/can't reattach, then try setting following command next time:
:nonblock on (or) -- default is 1 second.
:nonblock 5 

To browse the code through commands instead of typing ":cs f x",
download the following file:
http://cscope.sourceforge.net/cscope_maps.vim
copy it to:
~/.vim/plugin directory (assuming u are using >=vim6) and follow the following command options and I can tell you it's worth trying and with this options cscope became best browsing tool for me:
  • To jump to a result type the results number (+ enter)
  • Use tags commands to return after a jump to a result: ctrl-t
    To return to same spot as departure, use ctrl-o
  • To use "tags" navigation to search for words under the cursor (ctrl-\ or ctrl-])
  • CTRL-\ s/g/c/f..etc for key board shortcuts
  • "CTRL-spacebar s". This time, your Vim window will split in two horizontally , and the Cscope search result will be put in the new window. [if you've never used multiple Vim windows before: move between windows via 'CTRL-W w' (or CTRL-W arrow key, or CTRL-W h/j/k/l for left/up/down/right), close a window via 'CTRL-W c' (or good old ':q'), make the current window the only one via 'CTRL-W o', split a window into two via 'CTRL-W s' (or 'CTRL-W v' for a vertical split), open a file in a new window via ':spl[it] filename']
  • "CTRL-spacebar CTRL-spacebar s" (just hold down the CTRL key and tap the spacebar twice). If you have trouble hitting the keys fast enough for this to work, go into the cscope_maps.vim script and change Vim's timeout settings as described in the comments This time your Vim window will be split vertically

To get the help from vim :help cscope or :cs help
Command to see the runtimepath of vim :set runtimepath

Building cscope database on readonly workspace

Create cross reference file with readonly directories or files by giving complete path:
1. cscope -R -s dir,dir,dir -f cross-reference.file --- if this doesn't work, try the following:
     Go to one of the directories for which you need to build database, because -s option seems to take including the current directory to build database. Here lets think you are in dirpath1
    cscope -R -s  dirpath2/* dirpath3/*  -f cross-reference.file
2. cscope -R /complete-path/file1.c /complete-path/file.h -f cross-reference.file
3. find `pwd` -name [*.ch] | sort > file
cscope -R -i file -f cross-reference.file

To just start with cross-reference file
cscope -d -f cross-reference.file

To display last n elements of the path name:
cscope -pn 



To set the cscope tags:
set cst


>>>>>>
set csprg=/router/bin/cscope



References:
http://vimdoc.sourceforge.net/htmldoc/if_cscop.html
http://www.swaroopch.com/notes/Vim_en:Programmers_Editor
http://vim.dindinx.net/orig/html/if_cscop.txt.php
http://www.uic.edu/depts/accc/software/unixgeneral/vi101.html
http://vim.sourceforge.net/scripts/script.php?script_id=213
http://www.vim.org/scripts/script.php?script_id=1638
http://www.vim.org/scripts/script.php?script_id=2502
http://www.yolinux.com/TUTORIALS/LinuxTutorialAdvanced_vi.html
http://cscope.sourceforge.net/cscope_vim_tutorial.html
http://uw714doc.sco.com/en/SDK_cdebug/B_CmdLnOpts.html
http://unix.stackexchange.com/questions/46568/gnu-screen-freezes-trying-to-reattach

1 comment: