1. 程式人生 > 實用技巧 >「雜項」VIM 配置

「雜項」VIM 配置

轉載自 https://www.cnblogs.com/ma6174/archive/2011/12/10/2283393.html

Features

  1. F5 可以直接編譯並執行 CC++java 程式碼以及執行 shell 指令碼,按 F8 可進行 CC++ 程式碼的除錯。

  2. 自動插入檔案頭 ,新建 CC++ 原始檔時自動插入表頭:包括檔名、作者、聯絡方式、建立時間等,讀者可根據需求自行更改。

  3. 對映 Ctrl + A 為全選並複製快捷鍵,方便複製程式碼。

  4. F2 可以直接消除程式碼中的空行。

  5. F3 可列出當前目錄檔案,開啟樹狀檔案目錄。

  6. 支援滑鼠選擇、方向鍵移動。

  7. 程式碼高亮,自動縮排,顯示行號,顯示狀態行。

  8. Ctrl + P 可自動補全。

  9. []{}()""' ' 等都自動補全。

  10. 其他功能讀者可以研究以下檔案。

Install Easily!

wget http://files.cnblogs.com/ma6174/vimrc.zip

unzip -f vimrc.zip -d ~/

Code

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"顯示相關
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"setshortmess=atI"啟動的時候不顯示那個援助烏干達兒童的提示
"winpos55"設定視窗位置
"setlines=40columns=155"設定視窗大小
"setnu"顯示行號
setgo="不要圖形按鈕
"colorasmanian2"設定背景主題
setguifont=Courier_New:h10:cANSI"設定字型
"syntaxon"語法高亮
autocmdInsertLeave*senocul"用淺色高亮當前行
autocmdInsertEnter*secul"用淺色高亮當前行
"setruler"顯示標尺
setshowcmd"輸入的命令顯示出來,看的清楚些
"setcmdheight=1"命令列(在狀態行下)的高度,設定為1
"setwhichwrap+=<,>,h,l"允許backspace和游標鍵跨越行邊界(不建議)
"setscrolloff=3"游標移動到buffer的頂部和底部時保持3行距離
setnovisualbell"不要閃爍(不明白)
setstatusline=%F%m%r%h%w\[FORMAT=%{&ff}]\[TYPE=%Y]\[POS=%l,%v][%p%%]\%{strftime(\"%d/%m/%y\-\%H:%M\")}"狀態行顯示的內容
setlaststatus=1"啟動顯示狀態行(1),總是顯示狀態行(2)
setfoldenable"允許摺疊
setfoldmethod=manual"手動摺疊
"setbackground=dark"背景使用黑色
setnocompatible"去掉討厭的有關vi一致性模式,避免以前版本的一些bug和侷限
"顯示中文幫助
ifversion>=603
sethelplang=cn
setencoding=utf-8
endif
"設定配色方案
"colorschememurphy
"字型
"if(has("gui_running"))
"setguifont=Bitstream\Vera\Sans\Mono\10
"endif


setfencs=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936
settermencoding=utf-8
setencoding=utf-8
setfileencodings=ucs-bom,utf-8,cp936
setfileencoding=utf-8



"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"""""新檔案標題""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"新建.c,.h,.sh,.java檔案,自動插入檔案頭
autocmdBufNewFile*.cpp,*.[ch],*.sh,*.javaexec":callSetTitle()"
""定義函式SetTitle,自動插入檔案頭
funcSetTitle()
"如果檔案型別為.sh檔案
if&filetype=='sh'
callsetline(1,"\#########################################################################")
callappend(line("."),"\#FileName:".expand("%"))
callappend(line(".")+1,"\#Author:ma6174")
callappend(line(".")+2,"\#mail:[email protected]")
callappend(line(".")+3,"\#CreatedTime:".strftime("%c"))
callappend(line(".")+4,"\#########################################################################")
callappend(line(".")+5,"\#!/bin/bash")
callappend(line(".")+6,"")
else
callsetline(1,"/*************************************************************************")
callappend(line("."),">FileName:".expand("%"))
callappend(line(".")+1,">Author:ma6174")
callappend(line(".")+2,">Mail:[email protected]")
callappend(line(".")+3,">CreatedTime:".strftime("%c"))
callappend(line(".")+4,"************************************************************************/")
callappend(line(".")+5,"")
endif
if&filetype=='cpp'
callappend(line(".")+6,"#include<iostream>")
callappend(line(".")+7,"usingnamespacestd;")
callappend(line(".")+8,"")
endif
if&filetype=='c'
callappend(line(".")+6,"#include<stdio.h>")
callappend(line(".")+7,"")
endif
"新建檔案後,自動定位到檔案末尾
autocmdBufNewFile*normalG
endfunc
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"鍵盤命令
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

nmap<leader>w:w!<cr>
nmap<leader>f:find<cr>

"對映全選+複製ctrl+a
map<C-A>ggVGY
map!<C-A><Esc>ggVGY
map<F12>gg=G
"選中狀態下Ctrl+c複製
vmap<C-c>"+y
"去空行
nnoremap<F2>:g/^\s*$/d<CR>
"比較檔案
nnoremap<C-F2>:vertdiffsplit
"新建標籤
map<M-F2>:tabnew<CR>
"列出當前目錄檔案
map<F3>:tabnew.<CR>
"開啟樹狀檔案目錄
map<C-F3>\be
"C,C++按F5編譯執行
map<F5>:callCompileRunGcc()<CR>
func!CompileRunGcc()
exec"w"
if&filetype=='c'
exec"!g++%-o%<"
exec"!./%<"
elseif&filetype=='cpp'
exec"!g++%-o%<"
exec"!./%<"
elseif&filetype=='java'
exec"!javac%"
exec"!java%<"
elseif&filetype=='sh'
:!./%
endif
endfunc
"C,C++的除錯
map<F8>:callRungdb()<CR>
func!Rungdb()
exec"w"
exec"!g++%-g-o%<"
exec"!gdb./%<"
endfunc
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""實用設定
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"設定當檔案被改動時自動載入
setautoread
"quickfix模式
autocmdFileTypec,cppmap<buffer><leader><space>:w<cr>:make<cr>
"程式碼補全
setcompleteopt=preview,menu
"允許外掛
filetypepluginon
"共享剪貼簿
setclipboard+=unnamed
"從不備份
setnobackup
"make執行
:setmakeprg=g++\-Wall\\%
"自動儲存
setautowrite
setruler"開啟狀態列標尺
setcursorline"突出顯示當前行
setmagic"設定魔術
setguioptions-=T"隱藏工具欄
setguioptions-=m"隱藏選單欄
"setstatusline=\%<%F[%1*%M%*%n%R%H]%=\%y\%0(%{&fileformat}\%{&encoding}\%c:%l/%L%)\
"設定在狀態行顯示的資訊
setfoldcolumn=0
setfoldmethod=indent
setfoldlevel=3
setfoldenable"開始摺疊
"不要使用vi的鍵盤模式,而是vim自己的
setnocompatible
"語法高亮
setsyntax=on
"去掉輸入錯誤的提示聲音
setnoeb
"在處理未儲存或只讀檔案的時候,彈出確認
setconfirm
"自動縮排
setautoindent
setcindent
"Tab鍵的寬度
settabstop=4
"統一縮排為4
setsofttabstop=4
setshiftwidth=4
"不要用空格代替製表符
setnoexpandtab
"在行和段開始處使用製表符
setsmarttab
"顯示行號
setnumber
"歷史記錄數
sethistory=1000
"禁止生成臨時檔案
setnobackup
setnoswapfile
"搜尋忽略大小寫
setignorecase
"搜尋逐字元高亮
sethlsearch
setincsearch
"行內替換
setgdefault
"編碼設定
setenc=utf-8
setfencs=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936
"語言設定
setlangmenu=zh_CN.UTF-8
sethelplang=cn
"我的狀態行顯示的內容(包括檔案型別和解碼)
"setstatusline=%F%m%r%h%w\[FORMAT=%{&ff}]\[TYPE=%Y]\[POS=%l,%v][%p%%]\%{strftime(\"%d/%m/%y\-\%H:%M\")}
"setstatusline=[%F]%y%r%m%*%=[Line:%l/%L,Column:%c][%p%%]
"總是顯示狀態行
setlaststatus=2
"命令列(在狀態行下)的高度,預設為1,這裡是2
setcmdheight=2
"偵測檔案型別
filetypeon
"載入檔案型別外掛
filetypepluginon
"為特定檔案型別載入相關縮排檔案
filetypeindenton
"儲存全域性變數
setviminfo+=!
"帶有如下符號的單詞不要被換行分割
setiskeyword+=_,$,@,%,#,-
"字元間插入的畫素行數目
setlinespace=0
"增強模式中的命令列自動完成操作
setwildmenu
"使回格鍵(backspace)正常處理indent,eol,start等
setbackspace=2
"允許backspace和游標鍵跨越行邊界
setwhichwrap+=<,>,h,l
"可以在buffer的任何地方使用滑鼠(類似office中在工作區雙擊滑鼠定位)
setmouse=a
setselection=exclusive
setselectmode=mouse,key
"通過使用:commands命令,告訴我們檔案的哪一行被改變過
setreport=0
"在被分割的視窗間顯示空白,便於閱讀
setfillchars=vert:\,stl:\,stlnc:\
"高亮顯示匹配的括號
setshowmatch
"匹配括號高亮的時間(單位是十分之一秒)
setmatchtime=1
"游標移動到buffer的頂部和底部時保持3行距離
setscrolloff=3
"為C程式提供自動縮排
setsmartindent
"高亮顯示普通txt檔案(需要txt.vim指令碼)
auBufRead,BufNewFile*setfiletypetxt
"自動補全
:inoremap(()<ESC>i
:inoremap)<c-r>=ClosePair(')')<CR>
:inoremap{{<CR>}<ESC>O
:inoremap}<c-r>=ClosePair('}')<CR>
:inoremap[[]<ESC>i
:inoremap]<c-r>=ClosePair(']')<CR>
:inoremap"""<ESC>i
:inoremap'''<ESC>i
function!ClosePair(char)
ifgetline('.')[col('.')-1]==a:char
return"\<Right>"
else
returna:char
endif
endfunction
filetypepluginindenton
"開啟檔案型別檢測,加了這句才可以用智慧補全
setcompleteopt=longest,menu
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"CTags的設定
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
letTlist_Sort_Type="name""按照名稱排序
letTlist_Use_Right_Window=1"在右側顯示視窗
letTlist_Compart_Format=1"壓縮方式
letTlist_Exist_OnlyWindow=1"如果只有一個buffer,kill視窗也kill掉buffer
letTlist_File_Fold_Auto_Close=0"不要關閉其他檔案的tags
letTlist_Enable_Fold_Column=0"不要顯示摺疊樹
autocmdFileTypejavasettags+=D:\tools\java\tags
"autocmdFileTypeh,cpp,cc,csettags+=D:\tools\cpp\tags
"letTlist_Show_One_File=1"不同時顯示多個檔案的tag,只顯示當前檔案的
"設定tags
settags=tags
"setautochdir

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"其他東東
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"預設開啟Taglist
letTlist_Auto_Open=1
""""""""""""""""""""""""""""""
"Taglist(ctags)
""""""""""""""""""""""""""""""""
letTlist_Ctags_Cmd='/usr/bin/ctags'
letTlist_Show_One_File=1"不同時顯示多個檔案的tag,只顯示當前檔案的
letTlist_Exit_OnlyWindow=1"如果taglist視窗是最後一個視窗,則退出vim
letTlist_Use_Right_Window=1"在右側視窗中顯示taglist視窗
"minibufexpl外掛的一般設定
letg:miniBufExplMapWindowNavVim=1
letg:miniBufExplMapWindowNavArrows=1
letg:miniBufExplMapCTabSwitchBufs=1
letg:miniBufExplModSelTarget=1