天天看點

[轉]vim ctags使用方法

windows下很多人都使用source insight 編寫和檢視代碼。linux下可以使用VIM,剛開始會覺得VIM像windows下的記事本,而如果使用得當,它并不比source insight 遜色。

    在這裡,我會盡我所能細緻地講清楚如何把vim變成source insight, 然而你仍然需要積極地思考,并且必須自己去摸索一些東西。

    為了避免過于羅嗦,我把基礎的部分放在後面,如果你越看越覺得太簡單了,那麼本文并不适合你;如果看完前面的仍有疑問或者看不懂前面說的是什麼東西,不用擔心,後面會有一些必備的知識介紹。

一、用好系統自帶軟體ctags

大部分的unix系統都有ctags軟體,它能跟vim很好地合作。

用途:

    生成c語言的标簽檔案,實作相關c檔案之間的跳轉。

用法:

    1.生成标簽檔案

        在目前目錄下(運作$提示符後面的指令): 

        $ctags -R .

      -R表示recursive,遞歸,為目前目錄及其子目錄中的c檔案生成标簽檔案。最後一個.表示在目前目錄。

        運作完目前目錄會多一個檔案tags,就是c标簽的索引檔案。

    2.跳轉

        1)用vim打開一個已經建過标簽的c檔案    

        2)ctrl+] 找到光标所在位置的标簽定義的地方

        3)ctrl+t 回到跳轉之前的标簽處

    注意:此時運作vim,必須在"tags"檔案所在的目錄下運作。否則,運作它會找不到"tags"檔案,而需要在vim中用":set tags="指令設定"tags"檔案的路徑。對于一個稍微大點的項目,你可能在任何一個目錄下打開vim,然而在每個目錄下都生成一個tags檔案并不 是個好主意,那麼如何解決呢?方法是在.vimrc中增加一行:

        set tags=tags;/

    這是告訴vim在目前目錄找不到tags檔案時請到上層目錄查找。

二、需要額外安裝的腳本:

1、taglist

下載下傳位址http://www.vim.org/scripts/script.php?script_id=273

若你下載下傳時位址已改變,請到 www.vim.org 找到正确的位址,這很簡單。

    打開後,可以顯示源碼的整體架構,友善地進行跳轉。(用慣source insight的人一定勾起某些回憶了^_^)

    下載下傳插件并安裝,使用時在vim中輸入指令

        :Tlist

    即可打開/關閉taglist視窗。

    一個簡單的方法是設定快捷鍵,在.vimrc中增加一行:

        nnoremap <silent> <F8> :TlistToggle<CR>

    這樣在vim中按F8就可以打開/關閉taglist了。

    更多相關配置請看後面關于.vimrc的介紹。

三、基礎知識探讨

    約定:為了友善和準确,我們約定本文中"$"标示符後的指令為在終端下運作,而":"後的指令為在vim中運作。

VIM的配置檔案一般放在使用者主檔案夾下,也就是非root狀态時在終端運作

        $cd ~/

    會到的那個目錄,檔案名為.vimrc。

    看不到?有兩個可能:

        1、檔案名前面有一個點,表示是隐藏檔案,ls檢視時需加-a選項。

            $ls -a

        2、你還沒有建.vimrc檔案,自己建立一個就行,先建個空的吧,以後可以不斷往裡面填東西。

            $touch .vimrc

    主檔案夾下還有一個.vim檔案夾,沒有請自己mkdir

        $mkdir ~/.vim

    在.vim檔案夾下,再建兩個子檔案夾:plugin和doc

        $mkdir ~/.vim/plugin

        $mkdir ~/.vim/doc

    plugin檔案夾下放插件,doc檔案夾下放相應的help文檔。

    去下一個taglist吧(我們應該把它叫作腳本還是插件呢?它是放在plugin檔案夾下的,那麼應該是插件;而在vim.org,它是作為scripts存在,那麼應當是腳本。),我們當作例子來請解。

    下載下傳的是一個zip包,把它放在 ~/.vim 目錄下,然後

        $unzip filename.zip

    它已經自動把taglist.vim和taglist.txt分别放到plugin、doc檔案夾下了。

    這時重新啟動vim

        $vim

    運作

    發現旁邊多了一欄沒有?如果你打開的是c檔案,并且已經生成了tags檔案,那麼裡面應當會顯示一些有用的資訊。

    這個時候,taglist的help文檔已經在 ~/.vim/doc 目錄下了,但是你在vim下敲

        :help Tlist

    卻沒有任何反應,那是因為vim還沒有擷取幫助文檔裡面的tag,解決方法是在vim下

        :helptags ~/.vim/doc

    現在,你再

    看看有沒有反應?

    關于.vimrc

    我自己的.vimrc也在不斷地完善中,完善的過程中得益于從網絡上擷取的很多知識,感謝提供資訊的朋友,也是他們促使我寫下這篇東西。我把自己.vimrc的一部分貼在下面,你可以把這些根據需要加到你的.vimrc裡面去。

".vimrc

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" General

"For ctags, then it can find the 'tags' file even not in current directory

set tags=tags;/

"Get out of VI's compatible mode..

set nocompatible

"Sets how many lines of history VIM har to remember

set history=400

"Set to auto read when a file is changed from the outside

set autoread

"Have the mouse enabled all the time:

"when you need to copy from vim, maybe you have to ':set mouse=' first

set mouse=a

"""""""""""""""""""""""""""""""""""""

" Colors and Fonts

"Enable syntax highlight

syntax enable

"set colorscheme

colorscheme elflord

"endif

" VIM userinterface

"Set 7 lines to the curors away from the border- when moving vertical..

set so=7

"Turn on WiLd menu

set wildmenu

"Always show current position

set ruler

"The commandbar is 2 high

set cmdheight=2

"Show line number

set nu

"Set backspace

set backspace=eol,start,indent

"Bbackspace and cursor keys wrap to

set whichwrap+=<,>,h,l

"show matching bracets

set showmatch

"How many tenths of a second to blink

set mat=2

"Highlight search things

set hlsearch

"imediately show the search result

set is

" Folding

"Enable folding, I find it very useful

set nofen

set fdl=0

" Text options

set expandtab

set shiftwidth=2

set ambiwidth=double

set smarttab

"Set Tab=4 spaces

set ts=4

set lbr

set tw=500

set selection=inclusive

   """"""""""""""""""""""""""""""

   " Indent

   "Auto indent

   set ai

   "Set auto indent width = 4 spaces

   set sw=4

   "Smart indet

   set si

   "C-style indenting

   set cindent "usage: select codes, press '=' key, the codes will autoindenting

   "Wrap lines

   set wrap

"Encoding settings

if has("multi_byte")

    " Set fileencoding priority

    if getfsize(expand("%")) > 0

        set fileencodings=ucs-bom,utf-8,cp936,big5,euc-jp,euc-kr,latin1

    else

        set fileencodings=cp936,big5,euc-jp,euc-kr,latin1

    endif

    " CJK environment detection and corresponding setting

    if v:lang =~ "^zh_CN"

        " Use cp936 to support GBK, euc-cn == gb2312

        set encoding=cp936

        set termencoding=cp936

        set fileencoding=cp936

    elseif v:lang =~ "^zh_TW"

        " cp950, big5 or euc-tw

        " Are they equal to each other?

        set encoding=big5

        set termencoding=big5

        set fileencoding=big5

    elseif v:lang =~ "^ko"

        " Copied from someone's dotfile, untested

        set encoding=euc-kr

        set termencoding=euc-kr

        set fileencoding=euc-kr

    elseif v:lang =~ "^ja_JP"

        " Copied from someone's dotfile, unteste

        set encoding=euc-jp

        set termencoding=euc-jp

        set fileencoding=euc-jp

    " Detect UTF-8 locale, and replace CJK setting if needed

    if v:lang =~ "utf8$" || v:lang =~ "UTF-8$"

        set encoding=utf-8

        set termencoding=utf-8

        set fileencoding=utf-8

else

    echoerr "Sorry, this version of (g)vim was not compiled with multi_byte"

endif

"plugins

" Tlist

if &diff

let Tlist_Auto_Open=0 "don't auto pen when compare two files

let Tlist_Auto_Open=1 "auto pen Tlist when open a file

"set taglist window in right, delete the following line if you don't like

let Tlist_Use_Right_Window=1

let Tlist_Auto_Update=1 

let Tlist_File_Fold_Auto_Close=1

"auto close Tlist when exiting file.

let Tlist_Exit_OnlyWindow = 1 

nmap <F7> :copen<CR>

nmap <F6> :cclose<CR>

本文轉自feisky部落格園部落格,原文連結:http://www.cnblogs.com/feisky/archive/2012/02/07/2341932.html,如需轉載請自行聯系原作者