提升程式碼閱讀效率:vim 結合 ctags 與 cscope 的使用指南 - mintrabbitplus
Site icon mintrabbitplus

提升程式碼閱讀效率:vim 結合 ctags 與 cscope 的使用指南

在實務開發中,工程師經常需要面對規模龐大、結構複雜的程式碼專案,例如:Linux Kernel、嵌入式韌體。vim 作為一套高度可客製化的文字編輯器,若搭配 ctags 與 cscope 這兩個工具,便能建構出一個快速且方便的程式碼閱讀平台。透過這樣的組合,工程師可以在純文字環境中,迅速完成「跳轉函數定義」、「查找函數參考」等關鍵操作,大幅提升閱讀與維護程式碼的效率。本文將以實際使用情境為導向,說明如何設定 vim 並結合 ctags 與 cscope,協助建立一個日常開發的程式碼閱讀流程。

一、基本安裝
sudo apt-get install ctags
sudo apt-get install cscope
二、建立 ctags 資料庫
ctags -R
三、建立 cscope 資料庫
find ./ -name "*.c" -o -name "*.h" > ./cscope.files
cscope -Rbq -i cscope.files
四、開始使用 ctags
set tags=./tags
跳至該函式或變數定義
ctrl + ]
跳回使用此函式或變數處
ctrl + t
五、開始使用 cscope
cs add cscope.out
cs find s [name]
【查詢 C 語言的符號,例如函式名稱、巨集、列舉等在程式碼中出現的位置】
cs find g [name]
【查詢函式、巨集或列舉等的定義位置】
cs find d [name]
【查詢目前函式內所呼叫的其他函式】
cs find c [name]
【查詢有哪些函式呼叫了目前的函式】
cs find t [name]
【查詢指定的字串內容】
cs find e [name]
【以 egrep 模式進行查詢,功能類似 egrep,但搜尋速度更快】
cs find f [name]
【查詢並開啟指定的檔案】
cs find i [name]
【查詢有哪些檔案包含目前的檔案】

以下將以 cs find s [name] 為範例,實際示範其操作流程。以 mtk_add_mac 這個 API 為例,若需查詢其相關資訊,可在指令模式下輸入 cs find s mtk_add_mac,即可取得對應的搜尋結果。

nnoremap fs :cs find s <C-R>=expand("<cword>")<CR><CR>
nnoremap fg :cs find g <C-R>=expand("<cword>")<CR><CR>
nnoremap fc :cs find c <C-R>=expand("<cword>")<CR><CR>
nnoremap ft :cs find t <C-R>=expand("<cword>")<CR><CR>
nnoremap fe :cs find e <C-R>=expand("<cword>")<CR><CR>
nnoremap ff :cs find f <C-R>=expand("<cfile>")<CR><CR>
nnoremap fi :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
nnoremap fd :cs find d <C-R>=expand("<cword>")<CR><CR>

以下將以 nnoremap fs :cs find s <C-R>=expand("<cword>")<CR><CR> 為範例,實際示範其操作流程。以 mtk_add_mac 這個 API 為例,若需查詢其相關資訊,先將游標移至 mtk_add_mac 上,再輸入 fs 即可取得對應的搜尋結果。

六、使用 .vimrc
" add  ctags database
set tags=./tags

" use both cscope and ctag for 'ctrl-]', ':ta', and 'vim -t'
set cscopetag
" check cscope for definition of a symbol before checking ctags: set to 1
" if you wan the reverse search order.
set csto=0

" add any cscope database in current directory
if filereadable("cscope.out")
   cs add cscope.out
" else add the database pointed to by environment variable
elseif $CSCOPE_DB != ""
    cs add $CSCOPE_DB
endif
" show msg when any other cscope db added
set cscopeverbose

" cscope/vim key mappings
nnoremap fs :cs find s <C-R>=expand("<cword>")<CR><CR>
nnoremap fg :cs find g <C-R>=expand("<cword>")<CR><CR>
nnoremap fc :cs find c <C-R>=expand("<cword>")<CR><CR>
nnoremap ft :cs find t <C-R>=expand("<cword>")<CR><CR>
nnoremap fe :cs find e <C-R>=expand("<cword>")<CR><CR>
nnoremap ff :cs find f <C-R>=expand("<cfile>")<CR><CR>
nnoremap fi :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
nnoremap fd :cs find d <C-R>=expand("<cword>")<CR><CR>

.vimrc 設定檔可由此下載

七、結語

透過本文的說明與實際操作示範,可以看出 vim 搭配 ctags 與 cscope,能在不依賴大型 IDE 的情況下,提供完整程式碼導覽能力。無論是查找函式定義、追蹤函式呼叫關係,或是快速定位符號使用位置,皆能大幅降低閱讀與理解程式碼的成本。

將相關設定整合至 .vimrc 後,更可使 vim 在啟動時即完成必要的環境建置,讓開發者能專注於程式碼本身,而非重複的操作流程。這樣的工具組合不僅輕量、穩定,也能隨著使用習慣進一步客製化,成為長期開發與維護專案的有力輔助。

期望本文能協助開發者建立一套實用的程式碼閱讀流程,並在日後的開發工作中,充分發揮 vim、ctags 與 cscope 的作用。

Exit mobile version