1. 程式人生 > >【轉】vim ctag使用方法

【轉】vim ctag使用方法

記事本 menu owin 是什麽 的人 rtt script 例子 pac

我看到的出處沒有貼來源,我轉於http://www.cnblogs.com/feisky/archive/2012/02/07/2341932.html 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 運行 :Tlist 發現旁邊多了一欄沒有?如果你打開的是c文件,並且已經生成了tags文件,那麽裏面應當會顯示一些有用的信息。 這個時候,taglist的help文檔已經在 ~/.vim/doc 目錄下了,但是你在vim下敲 :help Tlist 卻沒有任何反應,那是因為vim還沒有獲取幫助文檔裏面的tag,解決方法是在vim下 :helptags ~/.vim/doc 現在,你再 :help Tlist 看看有沒有反應? 關於.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 endif " 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 endif 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 else let Tlist_Auto_Open=1 "auto pen Tlist when open a file endif "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>

【轉】vim ctag使用方法