天天看點

适合自己的vimrc (短小精悍)

"*******************************************************************************

"

" Author :Honker.y

"

" Email :[email protected]

"

" Last modified :2011-03-19 12:21

"

" Filename :.vimrc

"

" version :1.82

"

" Description : 适合自己的vim配置檔案

"

"******************************************************************************

"{​{​{基本設定

set nocompatible " 關閉 vi 相容模式

"setlocal spell spell開啟拼寫檢查

set mouse=a "讓vim支援滑鼠

syntax on " 自動文法高亮

set number " 顯示行号

set nocursorline " 不突出顯示目前行

set ruler " 打開狀态欄标尺

set shiftwidth=4 " 設定 << 和 >> 指令移動時的寬度為 4

set softtabstop=4 " 使得按倒退鍵時可以一次删掉 4 個空格

set tabstop=4 " 設定 tab 長度為 4

set nobackup " 覆寫檔案時不備份

set autochdir " 自動切換目前目錄為目前檔案所在的目錄

filetype plugin indent on " 開啟插件

set backupcopy=yes " 設定備份時的行為為覆寫

set ignorecase smartcase " 搜尋時忽略大小寫,但在有一個或以上大寫字母時仍保持對大小寫敏感

set nowrapscan " 禁止在搜尋到檔案兩端時重新搜尋

set incsearch " 輸入搜尋内容時就顯示搜尋結果

set hlsearch " 搜尋時高亮顯示被找到的文本

set noerrorbells " 關閉錯誤資訊響鈴

set novisualbell " 關閉使用可視響鈴代替呼叫

set t_vb= " 置空錯誤鈴聲的終端代碼

set showmatch " 插入括号時,短暫地跳轉到比對的對應括号

" set matchtime=2 " 短暫跳轉到比對括号的時間

set magic " 設定魔術

set hidden " 允許在有未儲存的修改時切換緩沖區,此時的修改由 vim 負責儲存

"set guioptions-=T " 隐藏工具欄

"set guioptions-=m " 隐藏菜單欄

set smartindent " 開啟新行時使用智能自動縮進

set backspace=indent,eol,start

" 不設定在插入狀态無法用倒退鍵和 Delete 鍵删除回車符

set cmdheight=1 " 設定指令行的行數為 1

set laststatus=2 " 顯示狀态欄 (預設值為 1, 無法顯示狀态欄)

"set foldenable " 開始折疊

"set foldmethod=syntax " 設定文法折疊

"set foldcolumn=0 " 設定折疊區域的寬度

"setlocal foldlevel=1 " 設定折疊層數為

autocmd Filetype c :set equalprg=indent

"如果是c檔案調用GNU indet 進行排版

"exe

"startinsert"

"啟動vim預設為插入模式

set cursorline

hi cursorline guibg=NONE gui=underline

"設定光标所在行,下劃線

" 代碼折疊設定 {​{​{

set foldenable " enable folding

set foldcolumn=2 " add a fold column

set foldmethod=marker " detect triple-{ style fold markers

set foldlevelstart=0 " start out with everything folded

set foldopen=block,hor,insert,jump,mark,percent,quickfix,search,tag,undo

" which commands trigger auto-unfold

"}}}

set cmdheight=1 " 指令行高度

set t_Co=256 " 指定配色方案是256色

colo hello " 使用 hello 調色闆,當然這是自定義的

set bsdir=buffer " 設定檔案浏覽器目錄為目前目錄

filetype plugin on " 檔案類型檢查打開

set fileencodings=cp936,gbk,gb2132,ucs-bom,utf-8

" 讓vim按照gbk,utf-8(沒有頭),utf-8的順序識别

set directory=~/.tmp "讓swap檔案儲存在~/.tmp目錄下

set noswapfile "不生成swap檔案

inoremap <C-v> <esc>:set paste<cr>mui<C-R>+<esc>mv'uV'v=:set nopaste<cr>

" 在插入模式中使用Ctrl+v粘貼全局剪貼闆内容

vnoremap <C-c> "+y

" 在Visual模式中使用Ctrl+c複制内容到全局剪貼闆

vnoremap <C-x> "+x " 在Visual模式中使用Ctrl+x剪切内容到全局剪貼闆

set comments=s1:/*,mb:*,ex0:/

"修正自動C式樣注釋功能 <2005/07/16>

set tags=./tags,./../tags,./**/tags

" 增強檢索功能

set fileformats=unix,dos " 儲存檔案格式

"set statusline=/ %<%F[%1*%M%*%n%R%H]%=/ %y/ %0(%{&fileformat}/ %{&encoding}/ %c:%l/%L%)

set statusline=%k%F[%{&ff}:%{&fenc}]%m%r%h%w/ %=[%l,%c]

"顯示檔案基本資訊

"----------------------------------------------------基本設定結束-----------------------------------------------"}}}

"{​{​{預設隐藏菜單欄和工具欄,可以通過 <F11> 切換顯示和隐藏

" @see http://liyanrui.is-programmer.com/articles/1791/gvim-menu-and-toolbar-toggle.html

set guioptions-=m

set guioptions-=T

map <silent> <F11> :if &guioptions =~# 'T' <Bar>

/set guioptions-=T <Bar>

/set guioptions-=m <bar>

/else <Bar>

/set guioptions+=T <Bar>

/set guioptions+=m <Bar>

/endif<CR>

"}}}

"{​{​{綁定一些快捷鍵

" Quick yanking to the end of the line

nmap Y y$

nnoremap j gj

nnoremap k gk

xnoremap j gj

xnoremap k gk

inoremap <C-h> <left>

inoremap <C-l> <Right>

inoremap <C-j> <C-o>gj

inoremap <C-k> <C-o>gk

nnoremap <C-j> <C-W>j

nnoremap <C-k> <C-W>k

nnoremap <C-h> <C-W>h

nnoremap <C-l> <C-W>l

inoremap " ""<Left>

inoremap ' ''<Left>

inoremap ( ()<Left>

inoremap [ []<Left>

inoremap { {}<Left>

" Yank/paste to the OS clipboard with ,y and ,p

nmap <leader>y "+y

nmap <leader>Y "+yy

nmap <leader>p "+p

nmap <leader>P "+P

"}}}

"{​{​{實作一鍵編譯單個源檔案

map <F5> :call Do_OneFileMake()<CR>

function Do_OneFileMake()

if expand("%:p:h")!=getcwd()

echohl WarningMsg | echo "Fail to make! This file is not in the current dir! Press <F7> to redirect to the dir of this file." | echohl None

return

endif

let sourcefileename=expand("%:t")

if (sourcefileename=="" || (&filetype!="cpp" && &filetype!="c"))

echohl WarningMsg | echo "Fail to make! Please select the right file!" | echohl None

return

endif

let deletedspacefilename=substitute(sourcefileename,' ','','g')

if strlen(deletedspacefilename)!=strlen(sourcefileename)

echohl WarningMsg | echo "Fail to make! Please delete the spaces in the filename!" | echohl None

return

endif

if &filetype=="c"

if g:iswindows==1

set makeprg=gcc/ -o/ %<.exe/ %

else

set makeprg=gcc/ -o/ %</ %

endif

elseif &filetype=="cpp"

if g:iswindows==1

set makeprg=g++/ -o/ %<.exe/ %

else

set makeprg=g++/ -o/ %</ %

endif

elseif &filetype=="cs"

set makeprg=csc/ //nologo/ //out:%<.exe/ %

endif

if(g:iswindows==1)

let outfilename=substitute(sourcefileename,'/(/.[^.]*/)</p>,'.exe','g')

let toexename=outfilename

else

let outfilename=substitute(sourcefileename,'/(/.[^.]*/)</p>,'','g')

let toexename=outfilename

endif

if filereadable(outfilename)

if(g:iswindows==1)

let outdeletedsuccess=delete(getcwd()."//".outfilename)

else

let outdeletedsuccess=delete("./".outfilename)

endif

if(outdeletedsuccess!=0)

set makeprg=make

echohl WarningMsg | echo "Fail to make! I cannot delete the ".outfilename | echohl None

return

endif

endif

execute "silent make"

set makeprg=make

execute "normal :"

if filereadable(outfilename)

if(g:iswindows==1)

execute "!".toexename

else

execute "!./".toexename

endif

endif

execute "copen"

endfunction

"}}}

"{​{​{進行make的設定

map <F6> :call Do_make()<CR>

map <c-F6> :silent make clean<CR>

function Do_make()

set makeprg=make

execute "silent make"

execute "copen"

endfunction

"}}}

"{​{​{讓自動補全根據使用者寫的部分來慢慢減小補全的搜尋範圍

set completeopt=longest,menuone

inoremap <expr> <cr> pumvisible() ? "/<c-y>" : "/<c-g>u/<cr>"

inoremap <expr> <c-n> pumvisible() ? "/<c-n>" : "/<c-n>/<c-r>=pumvisible() ? /"//<down>/" : /"//<cr>/""

inoremap <expr> <m-;> pumvisible() ? "/<c-n>" : "/<c-x>/<c-o>/<c-n>/<c-p>/<c-r>=pumvisible() ? /"//<down>/" : /"//<cr>/""

"}}}

"{​{​{設定 gnome-terminal 的光标

"if &term == "xterm"

" silent execute "!gconftool-2 --type string --set /apps/gnome-terminal/profiles/Default/cursor_shape block"

" autocmd! InsertEnter * silent execute "!gconftool-2 --type string --set /apps/gnome-terminal/profiles/Default/cursor_shape ibeam"

" autocmd! InsertLeave * silent execute "!gconftool-2 --type string --set /apps/gnome-terminal/profiles/Default/cursor_shape block"

" autocmd! VimLeave * silent execute "!gconftool-2 --type string --set /apps/gnome-terminal/profiles/Default/cursor_shape underline"

"endif

"}}}

"{​{​{ 顯示Tab符

set listchars=tab:/|/ ,trail:.,extends:>,precedes:<

if has("autocmd")

autocmd filetype javascript,php,python set list

endif

"}}}

"{​{​{ 設定用于GUI圖形使用者界面的字型清單。

if has ("win323")

set guifont=YaHei/ Consolas/ Hybrid:h12

endif

"}}}

"{​{​{進行版權聲明的設定

"添加或更新頭

"将鍵盤上的F4功能鍵映射為添加作者資訊的快捷鍵

map <F4> :call TitleDet()<cr>'s

function AddTitle()

call append(0,"/*******************************************************************************")

call append(1," * Author :Honker.y")

call append(2," * Email :[email protected]")

call append(3," * Last modified :".strftime("%Y-%m-%d %H:%M"))

call append(4," * Filename :".expand("%:t"))

call append(5," * version :")

call append(6," * Description :")

call append(7," * Install :")

call append(8," * Use :")

call append(9," *******************************************************************************/")

echohl WarningMsg | echo "Successful in adding the copyright." | echohl None

endf

"更新最近修改時間和檔案名

function UpdateTitle()

normal m'

execute '/# *Last modified:/s@:.*$@/=strftime(":/t%Y-%m-%d %H:%M")@'

normal ''

normal mk

execute '/# *Filename:/s@:.*$@/=":/t/t".expand("%:t")@'

execute "noh"

normal 'k

echohl WarningMsg | echo "Successful in updating the copy right." | echohl None

endfunction

"判斷前10行代碼裡面,是否有Last modified這個單詞,

"如果沒有的話,代表沒有添加過作者資訊,需要新添加;

"如果有的話,那麼隻需要更新即可

function TitleDet()

let n=1

"預設為添加

while n < 10

let line = getline(n)

if line =~ '^/#/s*/S*Last/smodified:/S*.*</p>

call UpdateTitle()

return

endif

let n = n + 1

endwhile

call AddTitle()

endfunction

"}}}

"{​{​{視窗分割時,進行切換的按鍵熱鍵需要連接配接兩次,比如從下方視窗移動

"光标到上方視窗,需要<c-w><c-w>k,非常麻煩,現在重映射為<c-k>,切換的

"時候會變得非常友善.

""nnoremap <C-h> <C-w>h

""nnoremap <C-j> <C-w>j

""nnoremap <C-k> <C-w>k

""nnoremap <C-l> <C-w>l

"}}}

"{​{​{視窗最大化

function Maximize_Window()

silent !wmctrl -r :ACTIVE: -b add,maximized_vert,maximized_horz

endfunction

"}}}

"{​{​{删除^M

nmap <F9> :%s//r<CR>

"}}}

"{​{​{自動除去多餘的空白行

" From: Vigil

function RemoveTrailingWhitespace()

if &ft != "diff"

let b:curcol = col(".")

let b:curline = line(".")

silent! %s//s/+$//

silent! %s//(/s*/n/)/+/%$//

call cursor(b:curline, b:curcol)

endif

endfunction

autocmd BufWritePre * call RemoveTrailingWhitespace()

"}}}

"{​{​{簡單的sign設定

sign define siv text=-> icon=/usr/share/pixmaps/vim-32.xpm texthl=SignColumn linehl=ModeMsg

map <F7> :exe ":sign place 2 line=" . line('.') . " name=siv file=" . expand("%:p")<cr>

"sign settings

"hi SignColumn guifg=red guibg=darkgray

"sign define haha text=>> texthl=SignColumn

"sign place {id} line={lnum} name={name} file={fname}

"map <F7> :exe ":sign place 2 line=" . line('.') . " name=haha file=" . expand("%:p")<cr>

map <C-F7> :sign unplace *<cr>

"}}}

"{​{​{-----------------------------------------Configure the plugin – taglist------------------------------------------

map <F3> :silent! Tlist<CR> " 按下F3呼出

"nnoremap :TlistToggle

"let Tlist_Auto_Open=0 " Let the tag list open automatically

"let Tlist_Auto_Update " Update the tag list automatically

"let Tlist_Compact_Format=1 " Show small menu

"let Tlist_Ctags_Cmd='ctags' " Location of ctags

"let Tlist_Enable_Fold_Column=0 "do show folding tree

"let Tlist_Process_File_Always=1 " Always process the source file

"let Tlist_Show_One_File=1 " Only show the tag list of current file

"let Tlist_Exist_OnlyWindow=1 " If you are the last, kill yourself

"let Tlist_File_Fold_Auto_Close=0 " Fold closed other trees

"let Tlist_Sort_Type="name" " Order by name

"let Tlist_WinWidth=40 " Set the window 40 cols wide.

"let Tlist_Close_On_Select=1 " Close the list when a item is selected

let Tlist_Ctags_Cmd='ctags' "因為我們放在環境變量裡,是以可以直接執行

let Tlist_Use_Right_Window=0 "讓視窗顯示在右邊,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

"/pre>

"---------------------------------------end Configure the plugin – taglist----------------------------------------

"}}}

"{​{​{-----------------------------------------Configure the plugin – cscope------------------------------------------

"判斷目前作業系統

if(has("win32") || has("win95") || has("win64") || has("win16"))

let g:iswindows=1

else

let g:iswindows=0

endif

autocmd BufEnter * lcd %:p:h

map <F12> :call Do_CsTag()<CR>

nmap <C-@>s :cs find s <C-R>=expand("<cword>")<CR><CR>:copen<CR>

nmap <C-@>g :cs find g <C-R>=expand("<cword>")<CR><CR>

nmap <C-@>c :cs find c <C-R>=expand("<cword>")<CR><CR>:copen<CR>

nmap <C-@>t :cs find t <C-R>=expand("<cword>")<CR><CR>:copen<CR>

nmap <C-@>e :cs find e <C-R>=expand("<cword>")<CR><CR>:copen<CR>

nmap <C-@>f :cs find f <C-R>=expand("<cfile>")<CR><CR>:copen<CR>

nmap <C-@>i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>:copen<CR>

nmap <C-@>d :cs find d <C-R>=expand("<cword>")<CR><CR>:copen<CR>

function Do_CsTag()

autocmd BufEnter * lcd %:p:h

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

"-----------------------------------------end Configure the plugin – cscope---------------------------------------

"}}}

"{​{​{-----------------------------------------Configure the plugin - NeoComplCache--------------------------------------

"let g:NeoComplCache_DisableAutoComplete = 1 "設定不自動彈出NeoComplCache自動補全清單

"let g:NeoComplCache_EnableAtStartup = 1 "激活ncc

"Ok ,I enjoy the time!

"------------------------------------------end Configure the plugin-NeoComplCache----------------------------------

"}}}

"{​{​{-----------------------------------------Configure the plugin - NERDTree -----------------------------------------------------------

"let NERDTreeWinPos = "left" "where NERD tree window is placed on the screen

"let NERDTreeWinSize = 31 "size of the NERD tree

"nmap <F8> <ESC>:NERDTreeToggle<RETURN>" Open and close the NERD_tree.vim separately

"--------------------------------------------------------------------------------------------------------------------------------------------

"}}}

"{​{​{-----------------------------------------Configure the plugin - winManager---------------------------------------------------------------

"設定界面分割

"let g:winManagerWindowLayout = "BufExplorer,FileExplorer|TagList"

let g:winManagerWindowLayout = "TagList|FileExplorer,BufExplorer"

"設定winmanager的寬度,預設為25

let g:winManagerWidth = 30

"定義打開關閉winmanager按鍵

nmap <silent> <F2> :WMToggle<cr>

"---------------------------------------------------------------------------------------------------------------------------------------------

"}}}