十個必用的 Vim Plugin
http://www.openfoundry.org/en/tech-article/2244
建立~/.vimrc
(系統預設也有一個/etc/vimrc)
vimrc配置如下(為簡單起見,采取注釋的方式直接說明):1
if(has("win32") || has("win95") || has("win64") || has("win16")) "判定目前作業系統類型
let g:iswindows=1
else
let g:iswindows=0
endif
set nocompatible "不要vim模仿vi模式,建議設定,否則會有很多不相容的問題
syntax on"打開高亮
if has("autocmd")
filetype plugin indent on "根據檔案進行縮進
augroup vimrcEx
au!
autocmd FileType text setlocal textwidth=78
autocmd BufReadPost *
/ if line("'/"") > 1 && line("'/"") <= line("$") |
/ exe "normal! g`/"" |
/ endif
augroup END
else
"智能縮進,相應的有cindent,官方說autoindent可以支援各種檔案的縮進,但是效果會比隻支援C/C++的cindent效果會差一點,但筆者并沒有看出來
set autoindent " always set autoindenting on
endif " has("autocmd")
set tabstop=4 "讓一個tab等于4個空格
set vb t_vb=
set nowrap "不自動換行
set hlsearch "高亮顯示結果
set incsearch "在輸入要搜尋的文字時,vim會實時比對
set backspace=indent,eol,start whichwrap+=<,>,[,] "允許倒退鍵的使用
if(g:iswindows==1) "允許滑鼠的使用
"防止linux終端下無法拷貝
if has('mouse')
set mouse=a
endif
au GUIEnter * simalt ~x
endif
"字型的設定
set guifont=Bitstream_Vera_Sans_Mono:h9:cANSI "記住空格用下劃線代替哦
set gfw=幼圓:h10:cGB2312
OK,有了上面的基本設定,一個好用的vim就配置好了,但是如果想讓它完美支援各種源代碼檔案,想讓它友善的注釋,友善的進行代碼提示,友善的添加使用者資訊,那還差的很遠哦,
将這些插件(*.vim)拷貝到~/.vim/plugin目錄(如果沒有這個目錄,自己建就可以)就可以使用。不過我将插件拷貝到~/.vim/plugin目錄後仍然無法使用這些插件。後來發現是這些插件檔案的權限有問題。我用的是自己的賬戶登陸,而那些檔案的owner是root。導緻運作Gvim後無法打開那些插件。解決辦法,用root登陸,滑鼠選中插件單擊右鍵-->propertes-->permission 把那些插件的owner改成正确的賬戶。
下面一個個的講解配置:
首先确認安裝了ctags和cscope,并且确認這兩個可執行程式所在的目錄已經放進環境變量裡面。(必須做,否則後邊都無法操作)
在vimrc中配置如下:1
map <F12> :call Do_CsTag()<CR>
nmap <[email protected]>s :cs find s <C-R>=expand("<cword>")<CR><CR>:copen<CR>
nmap <[email protected]>g :cs find g <C-R>=expand("<cword>")<CR><CR>
nmap <[email protected]>c :cs find c <C-R>=expand("<cword>")<CR><CR>:copen<CR>
nmap <[email protected]>t :cs find t <C-R>=expand("<cword>")<CR><CR>:copen<CR>
nmap <[email protected]>e :cs find e <C-R>=expand("<cword>")<CR><CR>:copen<CR>
nmap <[email protected]>f :cs find f <C-R>=expand("<cfile>")<CR><CR>:copen<CR>
nmap <[email protected]>i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>:copen<CR>
nmap <[email protected]>d :cs find d <C-R>=expand("<cword>")<CR><CR>:copen<CR>
function Do_CsTag()
let dir = getcwd()
if filereadable("tags")
if(g:iswindows==1)
let tagsdeleted=delete(dir."//"."tags")
else
let tagsdeleted=delete("./"."tags")
endif
if(tagsdeleted!=0)
echohl WarningMsg | echo "Fail to do tags! I cannot delete the tags" | echohl None
return
endif
endif
if has("cscope")
silent! execute "cs kill -1"
endif
if filereadable("cscope.files")
if(g:iswindows==1)
let csfilesdeleted=delete(dir."//"."cscope.files")
else
let csfilesdeleted=delete("./"."cscope.files")
endif
if(csfilesdeleted!=0)
echohl WarningMsg | echo "Fail to do cscope! I cannot delete the cscope.files" | echohl None
return
endif
endif
if filereadable("cscope.out")
if(g:iswindows==1)
let csoutdeleted=delete(dir."//"."cscope.out")
else
let csoutdeleted=delete("./"."cscope.out")
endif
if(csoutdeleted!=0)
echohl WarningMsg | echo "Fail to do cscope! I cannot delete the cscope.out" | echohl None
return
endif
endif
if(executable('ctags'))
"silent! execute "!ctags -R --c-types=+p --fields=+S *"
silent! execute "!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q ."
endif
if(executable('cscope') && has("cscope") )
if(g:iswindows!=1)
silent! execute "!find . -name '*.h' -o -name '*.c' -o -name '*.cpp' -o -name '*.java' -o -name '*.cs' > cscope.files"
else
silent! execute "!dir /s/b *.c,*.cpp,*.h,*.java,*.cs >> cscope.files"
endif
silent! execute "!cscope -b"
execute "normal :"
if filereadable("cscope.out")
execute "cs add cscope.out"
endif
endif
endfunction
1)taglist.vim
把taglist.vim放在plugin目錄下後,在vimrc中添加如下的配置:1
"進行Tlist的設定
"TlistUpdate可以更新tags
map <F3> :silent! Tlist<CR> "按下F3就可以呼出了
let Tlist_Ctags_Cmd='ctags' "因為我們放在環境變量裡,是以可以直接執行
let Tlist_Use_Right_Window=1 "讓視窗顯示在右邊,0的話就是顯示在左邊
let Tlist_Show_One_File=0 "讓taglist可以同時展示多個檔案的函數清單,如果想隻有1個,設定為1
let Tlist_File_Fold_Auto_Close=1 "非目前檔案,函數清單折疊隐藏
let Tlist_Exit_OnlyWindow=1 "當taglist是最後一個分割視窗時,自動推出vim
"是否一直處理tags.1:處理;0:不處理
let Tlist_Process_File_Always=0 "不是一直實時更新tags,因為沒有必要
let Tlist_Inc_Winwidth=0
showfunc.vim 下載下傳showfunc.vim
截圖如下:
這個插件實作了按下fd,就可以提示目前光标所在函數的定義,另外還實作了當輸入'('時,會自動比對tags,展示函數的定義原型。
隻需要把這個插件放到plugin下就行,不需要配置。
4)NERD_commenter.vim 注釋插件 下載下傳插件
也是程式員非常實用的一款插件,支援各種語言的補全,隻要你能想到的,放心他絕對注釋的了,而且還支援單行注釋,批量注釋,等各種指令映射,在這裡,我把最常用的鍵映射在vimrc中配置了一下。1
2
3
4 "對NERD_commenter的設定
let NERDShutUp=1
"支援單行和多行的選擇,//格式
map <c-h> ,c<space>
這樣的話,在光标所在行上,按下一次ctrl+h是注釋,再按下一次是取消注釋。
而其内建的指令,cm是多行注釋,類似C++的,,cu是取消注釋。
5)DoxygenToolkit.vim 由注釋生成文檔,并且能夠快速生成函數标準注釋 下載下傳位址
這個插件使得vim能夠生成标準的函數注釋,如圖:
map fg : Dox<cr>
let g:DoxygenToolkit_authorName="dantezhu"
let g:DoxygenToolkit_licenseTag="My own license/<enter>"
let g:DoxygenToolkit_undocTag="DOXIGEN_SKIP_BLOCK"
let g:DoxygenToolkit_briefTag_pre = "@brief/t"
let g:DoxygenToolkit_paramTag_pre = "@param/t"
let g:DoxygenToolkit_returnTag = "@return/t"
let g:DoxygenToolkit_briefTag_funcName = "no"
let g:DoxygenToolkit_maxFunctionProtoLines = 30
讀者可以需要将 DoxygenToolkit_authorName設定成為自己的名字,OK,這樣标準格式的代碼注釋就出來啦。
vim的代碼自動補全
2007-08-04 20:58突然發現vim有代碼自動補全功能,很是強大,基本和IDE的功能差不多了。
要實作代碼自動補全功能,我們需要一些插件,我使用的插件有:
javacomplete.vim
htmlcomplete.vim
csscomplete.vim
xmlcomplete.vim
pythoncomplete.vim
rubycomplete.vim
javascriptcomplete.vim
實際上在$VIMRUNTIME/autoload下已經有不少這樣的插件了,我們隻需要下載下傳沒有的就行了。
把這些插件放到$VIMRUNTIME/autoload後,還需要配置下~/.vim/vimrc,加上:
autocmd FileType ruby,eruby set omnifunc=rubycomplete#Complete
autocmd FileType python set omnifunc=pythoncomplete#Complete
autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS
autocmd FileType html set omnifunc=htmlcomplete#CompleteTags
autocmd FileType css set omnifunc=csscomplete#CompleteCSS
autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags
autocmd FileType java set omnifunc=javacomplete#Complete
于是我們在編輯相應的檔案的時候,按下C-X C-O就能喚出自動補全的視窗了