使用 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 目錄, 若無該目錄,可以先建立。
例如:
[root@gd86 local]# mkdir -p ~/.vim/
[root@gd86 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
-------------------------------------------------------------------------------