2012年8月7日 星期二

簡單打造vi+cscope的工作環境

每個人都會有自己的使用習慣,
像我每次用別人電腦時, 發現很不順水,
原因是少了慣用的快捷指令~
為了可以快速地把個人習慣搬家, 整理後發現其實也不多.
但少了這一點點東西, 就好像少了隻手一樣, 很不順呢!

編輯 ~/.bash_profile 或 ~/.bashrc
加入這些alias, 然後重新login 或source一下
-----
alias ff='find ./ -iname "*.[ch]" -o -iname "*.cpp" |xargs grep'
alias fh='find $PWD -iname "*.h" |xargs grep'
alias fp='find $PWD -iname "*.sh" -o -iname "*.php" |xargs grep'
alias fj='find $PWD -iname "*.js" -o -iname "*.html" -o -iname "*.htm" |xargs grep'
alias fm='find $PWD -iname "makefile" | xargs grep'
alias fk='find $PWD -iname "*.mk" | xargs grep'
alias fg='find $PWD -iname "kconfig" | xargs grep'
alias fx='find $PWD -iname "*.xml" |xargs grep'
alias cs='cscope -bqR'
-----
用法,
ff , 在當前目錄及子目錄的.c及.h檔案裡搜尋
fj , 在當前目錄及子目錄的.js及.html檔案裡搜尋


編輯 ~/.vimrc, 加入以下這段,
-----
if has("cscope")
nnoremap :TlistOpen
nnoremap :wincmd p
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
" else add database pointed to by environment
elseif $CSCOPE_DB != ""
cs add $CSCOPE_DB
endif
set csverb
endif

nmap s :cs find s =expand("")
nmap g :cs find g =expand("")
nmap c :cs find c =expand("")
nmap t :cs find t =expand("")
nmap e :cs find e =expand("")
nmap f :cs find f =expand("")
nmap d :cs find d =expand("")
-----
用法,
到了某個要trace code的目錄裡, 
1. 用"cs"指令先把cscope的reference建好
2. 直接用vi打檔案
3. 把指標移到要查詢的function name 或 variable上
4. 在指令模式下, 按crtl+\然後按s 或 g 或上面所試定的快捷
5. 例如, 'g'則會直接跳到function name 或 variable的所在位於
    's'會把reference所指定function name 或 variable的地方都列出來
    看到下信息時, 輸入'2'即可切換到src/xlist.c檔案, 同時指標也會移到dlist_add所在這行


Cscope tag: dlist_add
   #   line  filename / context / line
   1     58  common/dlist.h <>
             static inline void dlist_add(struct dlist_head * new, struct dlist_head * head)
   2     78  src/xlist.c <>
             dlist_add((dlist_t *)tmp_hash, &g_xmlnode_hash[hash].hash);
   3     57  unittest/dlist_test.c <>
             dlist_add((struct dlist_head *)new_node, head);
Type number and (empty cancels):



這樣就可以很快速的來trace code, 省去對每個檔案開開關關的時間~

沒有留言: