windows下很多人都使用source insight 編寫(xiě)和查看代碼。linux下可以使用VIM,剛開(kāi)始會(huì )覺(jué)得VIM像windows下的記事本,而如果使用得當,它并不比source insight 遜色。
在這里,我會(huì )盡我所能細致地講清楚如何把vim變成source insight, 然而你仍然需要積極地思考,并且必須自己去摸索一些東西。
為了避免過(guò)于羅嗦,我把基礎的部分放在后面,如果你越看越覺(jué)得太簡(jiǎn)單了,那么本文并不適合你;如果看完前面的仍有疑問(wèn)或者看不懂前面說(shuō)的是什么東西,不用擔心,后面會(huì )有一些必備的知識介紹。
一、用好系統自帶軟件ctags
大部分的unix系統都有ctags軟件,它能跟vim很好地合作。
用途:
生成c語(yǔ)言的標簽文件,實(shí)現相關(guān)c文件之間的跳轉。
用法:
1.生成標簽文件
在當前目錄下(運行$提示符后面的命令):
$ctags -R .
-R表示recursive,遞歸,為當前目錄及其子目錄中的c文件生成標簽文件。最后一個(gè).表示在當前目錄。
運行完當前目錄會(huì )多一個(gè)文件tags,就是c標簽的索引文件。
2.跳轉
1)用vim打開(kāi)一個(gè)已經(jīng)建過(guò)標簽的c文件
2)ctrl+] 找到光標所在位置的標簽定義的地方
3)ctrl+t 回到跳轉之前的標簽處
注意:此時(shí)運行vim,必須在"tags"文件所在的目錄下運行。否則,運行它會(huì )找不到"tags"文件,而需要在vim中用":set tags="命令設定"tags"文件的路徑。對于一個(gè)稍微大點(diǎn)的項目,你可能在任何一個(gè)目錄下打開(kāi)vim,然而在每個(gè)目錄下都生成一個(gè)tags文件并不 是個(gè)好主意,那么如何解決呢?方法是在.vimrc中增加一行:
set tags=tags;/
這是告訴vim在當前目錄找不到tags文件時(shí)請到上層目錄查找。
二、需要額外安裝的腳本:
1、taglist
下載地址http://www.vim.org/scripts/script.php?script_id=273
若你下載時(shí)地址已改變,請到 www.vim.org 找到正確的地址,這很簡(jiǎn)單。
用途:
打開(kāi)后,可以顯示源碼的整體架構,方便地進(jìn)行跳轉。(用慣source insight的人一定勾起某些回憶了^_^)
用法:
下載插件并安裝,使用時(shí)在vim中輸入命令
:Tlist
即可打開(kāi)/關(guān)閉taglist窗口。
一個(gè)簡(jiǎn)單的方法是設定快捷鍵,在.vimrc中增加一行:
nnoremap:TlistToggle
這樣在vim中按F8就可以打開(kāi)/關(guān)閉taglist了。
更多相關(guān)配置請看后面關(guān)于.vimrc的介紹。
三、基礎知識探討
約定:為了方便和準確,我們約定本文中"$"標示符后的命令為在終端下運行,而":"后的命令為在vim中運行。
VIM的配置文件一般放在用戶(hù)主文件夾下,也就是非root狀態(tài)時(shí)在終端運行
$cd ~/
會(huì )到的那個(gè)目錄,文件名為.vimrc。
看不到?有兩個(gè)可能:
1、文件名前面有一個(gè)點(diǎn),表示是隱藏文件,ls查看時(shí)需加-a選項。
$ls -a
2、你還沒(méi)有建.vimrc文件,自己創(chuàng )建一個(gè)就行,先建個(gè)空的吧,以后可以不斷往里面填東西。
$touch .vimrc
主文件夾下還有一個(gè).vim文件夾,沒(méi)有請自己mkdir
$mkdir ~/.vim
在.vim文件夾下,再建兩個(gè)子文件夾:plugin和doc
$mkdir ~/.vim/plugin
$mkdir ~/.vim/doc
plugin文件夾下放插件,doc文件夾下放相應的help文檔。
去下一個(gè)taglist吧(我們應該把它叫作腳本還是插件呢?它是放在plugin文件夾下的,那么應該是插件;而在vim.org,它是作為scripts存在,那么應當是腳本。),我們當作例子來(lái)請解。
下載的是一個(gè)zip包,把它放在 ~/.vim 目錄下,然后
$unzip filename.zip
它已經(jīng)自動(dòng)把taglist.vim和taglist.txt分別放到plugin、doc文件夾下了。
這時(shí)重新啟動(dòng)vim
$vim
運行
:Tlist
發(fā)現旁邊多了一欄沒(méi)有?如果你打開(kāi)的是c文件,并且已經(jīng)生成了tags文件,那么里面應當會(huì )顯示一些有用的信息。
這個(gè)時(shí)候,taglist的help文檔已經(jīng)在 ~/.vim/doc 目錄下了,但是你在vim下敲
:help Tlist
卻沒(méi)有任何反應,那是因為vim還沒(méi)有獲取幫助文檔里面的tag,解決方法是在vim下
:helptags ~/.vim/doc
現在,你再
:help Tlist
看看有沒(méi)有反應?
關(guān)于.vimrc
我自己的.vimrc也在不斷地完善中,完善的過(guò)程中得益于從網(wǎng)絡(luò )上獲取的很多知識,感謝提供信息的朋友,也是他們促使我寫(xiě)下這篇東西。我把自己.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:copen
nmap:cclose
聯(lián)系客服