關於VIM+ctags+cscope安裝與使用

安裝ctags和taglist

$ sudo apt-get install cscope
$ sudo apt-get install ctags

另外需要安裝vim的插件taglist, taglist插件是以vim腳本的形式存在的,因此只需要將其下載下來放到相應的目錄即可。
可以去VIM的官方網址,上面有安裝方法:
install details
1. Download the taglist.zip file and unzip the files to the $HOME/.vim or the $HOME/vimfiles or the $VIM/vimfiles directory. After this step, you should have the following two files (the directory structure should be preserved):

     plugin/taglist.vim - main taglist plugin file
     doc/taglist.txt    - documentation (help) file

Refer to the |add-plugin|, |add-global-plugin| and |runtimepath| Vim help pages for more details about installing Vim plugins.
2. Change to the $HOME/.vim/doc or $HOME/vimfiles/doc or $VIM/vimfiles/doc directory, start Vim and run the “:helptags .” command to process the taglist help file. Without this step, you cannot jump to the taglist help topics.
3. If the exuberant ctags utility is not present in your PATH, then set the Tlist_Ctags_Cmd variable to point to the location of the exuberant ctags utility (not to the directory) in the ~/.vimrc file.
4. If you are running a terminal/console version of Vim and the terminal doesn’t support changing the window width then set the ‘Tlist_Inc_Winwidth’ variable to 0 in the .vimrc file.
5. Restart Vim.
6. You can now use the “:TlistToggle” command to open/close the taglist window. You can use the “:help taglist” command to get more information about using the taglist plugin.
簡單解釋一下上述步驟:
(1)安裝taglist
以taglist_46.zip爲例,下載後解壓至$HOME/.vim路徑下,使用unzip命令:
-d選項表示解壓到指定目錄(默認解壓到當前目錄),解壓後可以看到有兩個文件夾:

$ unzip taglist_46.zip -d $HOME/.vim
Archive:  taglist_46.zip
  inflating: /home/zfchen/.vim/plugin/taglist.vim  
  inflating: /home/zfchen/.vim/doc/taglist.txt 

然後進入$HOME/.vim/doc目錄下,打開Vim($ vim),執行”:helptags .“命令,此步驟是將doc下的幫助文檔加入到Vim的幫助主題中,這樣我們就可以在Vim中運行“:help taglist.txt”查看taglist的幫助文檔。
(2)配置taglist
關於具體配置項的說明和快捷鍵的使用可以參考這篇blog,~/.vimrc腳本的內容可以參考另外一個鏈接,下面是我使用的腳本內容。

"設置ctags路徑
let Tlist_Ctags_Cmd = '/usr/bin/ctags'
"啓動vim後自動打開taglist窗口
let Tlist_Auto_Open = 1
"taglist爲最後一個窗口時,退出vim
let Tlist_Exit_OnlyWindow = 1
"總是使用鼠標
let mouse=a
"單擊tag跳轉
let Tlist_Use_SingleClick = 1
"--ctags setting--
" 按下F5重新生成tag文件,並更新taglist
map <F5> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR><CR> :TlistUpdate<CR>
imap <F5> <ESC>:!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR><CR> :TlistUpdate<CR>
set tags=tags
set tags+=./tags "add current directory's generated tags file
set tags+=~/my_repository/linux/tags "add new tags file

另外可以使用 Ctrl+WW組合鍵(按2次W),在標籤區和代碼區進行切換。
可以使用:Tlist命令打開或關閉tag區。

關於cscope的配置

同樣需要在~/.vimrc腳本中添加一些配置項,如下:

if has("cscope")
  set csprg=/usr/bin/cscope
  set csto=1
  set cst
  set nocsverb
  " add any database in current directory
  if filereadable("cscope.out")
      cs add cscope.out
  endif
  set csverb
endif

關於上面的cscope.out文件,可以設置爲某個指定的目錄(比如linux內核的目錄: “/home/zfchen/my_repository/linux/cscope.out”)
cscope的用法:
(1)首先需要爲你的代碼生成一個cscope數據庫
在你的項目根目錄運行下面的命令:

cscope -Rbq 

這個命令會生成三個文件:cscope.out, cscope.in.out, cscope.po.out。其中cscope.out是基本的符號索引,後兩個文件是使用”-q“選項生成的,可以加快cscope的索引速度。Cscope只在第一次解析時掃描全部文件,以後再調用cscope,它只掃描那些改動過的文件,這大大提高了cscope生成索引的速度。
這裏列出了cscope的常用選項:

-R: 在生成索引文件時,搜索子目錄樹中的代碼
-b: 只生成索引文件,不進入cscope的界面
-q: 生成cscope.in.out和cscope.po.out文件,加快cscope的索引速度
-k: 在生成索引文件時,不搜索/usr/include目錄
-i: 如果保存文件列表的文件名不是cscope.files時,需要加此選項告訴cscope到哪兒去找源文件列表。可以使用“–”,表示由標準輸入獲得文件列表
-Idir: 在-I選項指出的目錄中查找頭文件
-u: 掃描所有文件,重新生成交叉索引文件
-C: 在搜索時忽略大小寫
-Ppath: 在以相對路徑表示的文件前加上的path,這樣不用切換到數據庫文件所在的目錄也可以使用它了。

(2)在Vim中連接到指定的數據庫
打開Vim,使用cs add cscope.out命令連接指定的數據庫。快捷鍵使用——參考鏈接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章