使用 ctags 和 taglist 閱讀C和C++源碼
(1) ctags 使用
(i) 生成 tags 檔案
進入源碼根目錄,例如 /local/zkl/Datacollector,執行指令 “ ctags -R * ” 将在該目錄下生成整個源碼的 tags 檔案.
(ii) 使用vim編輯源碼時加載 tags 檔案
tags檔案加載後,就可以在 vim 中使用相關指令檢視相關源碼了,例如當光标在函數調用處時,使用 ctrl+] 指令進入函數定義處,退回來使用 ctrl+T 組合鍵.
要想使用 vim 時加載 tags,有兩種方法:
a) 設定vim配置檔案 ~/.vimrc ,加入“ set tags=tags ”這樣一行(注意不是 set tags=./tags)。
這表示當執行vim 指令時會加載目前目錄下(執行vim指令時使用者所在的目錄)的tags檔案.例如在源碼根目錄下執行 vim include/parapi.h 就會加載根目錄下的 tags 檔案
b) 在用vim打開檔案後,執行 :set tags=tags (注意不是 set tags=./tags),這表示加載目前目錄下的 tags 檔案。
(2) taglist插件的使用
使用taglist插件可以檢視源碼檔案的函數,變量等資訊,對于閱讀源碼很有幫助.
插件下載下傳位址: http://www.vim.org/scripts/script.php?script_id=273
解壓後得到
plugin/taglist.vim
doc/taglist.txt
插件安裝上面網頁講的很詳細,這裡做一下簡單介紹:
複制plugin和doc兩個目錄到 $HOME/.vim or $HOME/vimfiles or $VIM/vimfiles 目錄, 若無該目錄,可以先建立。
例如:
[[email protected] local]# mkdir -p ~/.vim/
[[email protected] local]# cp taglist_45/* ~/.vim/ -r
注意目錄結構是: $HOME/.vim/plugin/taglist.vim
進入 $HOME/.vim/doc 目錄,啟動vim 并執行指令": helptags ." ,這會處理 taglist 的幫助檔案
重新開機 vim,可以使用指令 " :TlistToggle " 列出目前源碼檔案的代碼,使用 ":help taglist " 檢視更多用法。
-------------------------------------------------------------------------------
taglist視窗使用F1指令可以打開幫助菜單,主要如下:
" <enter> : Jump to tag definition
" o : Jump to tag definition in new indow
" p : Preview the tag definition
" <space> : Display tag prototype
" u : Update tag list
" s : Select sort field
" d : Remove file from taglist
" x : Zoom-out/Zoom-in taglist window 放大taglist視窗
" + : Open a fold 打開一個折疊項
" - : Close a fold 折疊一個項目
" * : Open all folds 打開所有
" = : Close all folds 折疊所有
" [[ : Move to the start of previous file
" ]] : Move to the start of next file
" q : Close the taglist window 關閉視窗
" <F1> : Remove help text
-------------------------------------------------------------------------------
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZwpmL0QEWNNDOwQDNyIDMzEzXzIzMyETMvwFOvwFNwETMwIzLcRnbl1GajFGd0F2LcRXZu5ibkN3YukGavw1LcpDc0RHaiojIsJye.jpg)