1. 程式人生 > >vim + ctags + taglist + cscope + cppcomplete + global 使用初探

vim + ctags + taglist + cscope + cppcomplete + global 使用初探

各個工具的主要功能:
vim:這個是Linux下功能十分強大的編輯器,就不說了。
ctags:生成tag檔案的命令,這個是其它工具的基礎。
taglist:需要ctags的支撐,直接在左邊列出函式列表,全域性引數列表。
cppcomplete:需要ctags的支撐,可以補全型別或者函式名。
cscope:比較強大,可以對函式以及部分型別定義進行跳轉,其實cscope是用來替代ctags功能的。
global:貌似和cscope,taglist功能一樣,據說對於大型的工程表現不錯。

各個工具的安裝:
1)vim,ctags,cscope,global,在系統安裝時安裝,或者使用yum安裝。
2)下載vim的cscope外掛,並放到相應目錄下,如~/.vim/plugin/

2)taglist,cppcomplete是Vim的外掛,從下面地址獲得,然後放到vim相應目錄下即可。

各個工具的使用:
1)ctags的使用
在終端執行如下命令,將生成檔案tags:
ctags *.cpp
要生成cppcomplete需要的tags檔案的話,需要執行下面的命令:
ctags -n -f cppcomplete.tags --fields=+ai --C++-types=+p * -L cscope.files
注意,我在最後使用了引數"-L cscope.files" 這僅僅是借用拉cscope生成的檔案索引來幫助ctags去查詢相關工程檔案生成tags。

2)使用taglist
taglist的功能是即時生成當前檔案的函式列表和全域性變數列表,便於索引。
在vim中命令模式下使用:
Tlist 開啟或者關閉當前檔案的索引;
TlistSync 立即在開啟的索引視窗中定位當前的游標所在位置屬於哪個函式或者結構定義中。

3)使用cppcomplete
使用很簡單,就是在你需要補全型別成員時,按F8鍵就可以啦。
The default key mapping to complete the code are:

Alt+l in insert mode will try to find the possible completions and display them in a popup menu. Also normal completions to the names in cppcomplete.tags.
Alt+j in insert mode will show the popup menu with the last results. Selecting one of the items will paste the text.
F8/F9 will work in a similar way as Ctrl+N, Ctrl+P in unextended vim so the script can be used without the popup menu.
F5 in insert mode will lookup the class and display it in a preview window

The key mapping are only tested under Windows and linux and they will not work on all platforms. Changing the mappings is easy.

4)使用cscope
cscope的tag生成最簡單的方法是:
在你的開發工程的最上層目錄執行cscope-indexer,它會遍歷下面的所有目錄,生成兩個檔案,一個是cscope.files,這個檔案記錄需要生成tags的檔名,可以手工修改,另一個是cscope格式的tags檔案cscope.out。
完成後,就可以使用Ctrl-]跳轉查詢型別定義了。

5)使用global
在工程的最上層目錄執行命令:
gtags
等待它完成所有需要的global自己格式的tags的生成。生成完後,可以用如下命令查詢相關型別引用和關聯關係:

Gtags xxxx 查詢xxxx的定義
Gtags -r xxxx 查詢xxxx的引用
Gtags -s xxxx 查詢xxxx出現的地方
有時它無法正常分析的型別 可以試試用帶-s 的引數的方式找找型別定義。