" ***************Vimrc files for GVim *************
" * CreateDate : Apr 20,2010
" * LastModified : Apr 20,2010
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 全局設(shè)定 "
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 不要使用vi的鍵盤模式,而是vim自己的
set nocompatible
" 加載配置。
behave mswin
"設(shè)置快速編輯.vimrc文件 ,e 編輯.vimrc
map <silent> <leader>e :call SwitchToBuf("~/_vimrc")<cr>
"保存.vimrc文件后會自動調(diào)用新的.vimrc
autocmd! bufwritepost .vimrc source ~/_vimrc
" 設(shè)定解碼
if has("multi_byte")
" When 'fileencodings' starts with 'ucs-bom', don't do this manually
"set bomb
set fileencodings=ucs-bom,utf-8,chinese,taiwan,japan,korea,latin1
" CJK environment detection and corresponding setting
if v:lang =~ "^zh_CN"
" Simplified Chinese, on Unix euc-cn, on MS-Windows cp936
set encoding=utf-8
set termencoding=utf-8
if &fileencoding == ''
set fileencoding=utf-8
endif
elseif v:lang =~ "^zh_TW"
" Traditional Chinese, on Unix euc-tw, on MS-Windows cp950
set encoding=euc-tw
set termencoding=euc-tw
if &fileencoding == ''
set fileencoding=euc-tw
endif
elseif v:lang =~ "^ja_JP"
" Japanese, on Unix euc-jp, on MS-Windows cp932
set encoding=euc-jp
set termencoding=euc-jp
if &fileencoding == ''
set fileencoding=euc-jp
endif
elseif v:lang =~ "^ko"
" Korean on Unix euc-kr, on MS-Windows cp949
set encoding=euc-kr
set termencoding=euc-kr
if &fileencoding == ''
set fileencoding=ecu-kr
endif
endif
" Detect UTF-8 locale, and override CJK setting if needed
if v:lang =~ "utf8$" || v:lang =~ "UTF-8$"
set encoding=utf-8
endif
else
echoerr 'Sorry, this version of (g)Vim was not compiled with "multi_byte"'
endif
" browsedir設(shè)置
set browsedir=buffer
" 自動格式化設(shè)置
filetype indent on
set autoindent
set smartindent
" 設(shè)置備份及備份目錄。
set backspace=indent,eol,start
set backupdir=D:\vim_back_files
set autochdir
" history文件中需要記錄的行數(shù),恢復(fù)必須用到。
set history=1024
" 窗口最大化
autocmd GUIEnter * simalt ~x
" 解決菜單亂碼,先指定語言。
set langmenu=zh_CN.UTF-8
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
language message zh_CN.UTF-8
set fileencodings=utf-8,GB2312,cp936,gb18030,big5,euc-jp,euc-kr,latin1
" 在處理未保存或只讀文件的時候,彈出確認
set confirm
" 共享外部剪貼板
set clipboard+=unnamed
" 顯示未完成命令
set showcmd
" 偵測文件類型
filetype on
" 載入文件類型插件
filetype plugin on
" 為特定文件類型載入相關(guān)縮進文件
filetype indent on
" 保存全局變量
set viminfo+=!
" 帶有如下符號的單詞不要被換行分割
set iskeyword+=_,$,@,%,#,-
" 語法高亮
syntax on
" 設(shè)置不兼容模式
set nocp
" 設(shè)置鼠標支持
set mouse=a
" 不要生成swap文件
setlocal noswapfile
" 當buffer被丟棄時隱藏
set bufhidden=hide
" 保存窗口大小
set sessionoptions+=resize
" C++頭文件識別
au BufEnter /usr/include/c++/* setf cpp
au BufEnter /usr/include/g++-3/* setf cpp
" GNU標準
au BufEnter /usr/* call GnuIndent()
" 顯示行號
set number
" tab寬度
set tabstop=4
set cindent shiftwidth=4
set autoindent shiftwidth=4
" C/C++注釋
set comments=://
" 修正自動C式樣注釋功能 <2005/07/16>
set comments=s1:/*,mb:*,ex0:/
" 增強檢索功能
set tags=./tags,./../tags,./**/tags
" 保存文件格式
set fileformats=unix,dos
" 文件被其他程序修改時自動載入
set autoread
" 高亮字符,讓其不受100列限制
:highlight OverLength ctermbg=red ctermfg=white guibg=red guifg=white
:match OverLength '\%101v.*'
" 總是顯示狀態(tài)行
set laststatus=2
" 狀態(tài)行顏色
" highlight StatusLine guifg=SlateBlue guibg=Yellow
highlight StatusLine guifg=SlateBlue guibg=White
highlight StatusLineNC guifg=Gray guibg=White
" 我的狀態(tài)行顯示的內(nèi)容(包括文件類型和解碼)
set statusline=[%n]%<%f%y%h%m%r%=[%b\ 0x%B]\ %l\ of\ %L,%c%V\ Page\ %N\ %P
" 設(shè)置命令行高度為2行
set cmdheight=1
" 命令行補全
set wildmenu
" 自動補全括號,包括大括號
:inoremap ( ()<ESC>i
:inoremap ) <c-r>=ClosePair(')')<CR>
:inoremap { {}<ESC>i
:inoremap } <c-r>=ClosePair('}')<CR>
:inoremap [ []<ESC>i
:inoremap ] <c-r>=ClosePair(']')<CR>
:inoremap < <><ESC>i
:inoremap > <c-r>=ClosePair('>')<CR>
" 打開文件時,總是跳到退出之前的光標處
autocmd BufReadPost *
\ if line("'\"") > 0 && line("'\"") <= line("$") |
\ exe "normal! g`\"" |
\ endif
" 用淺色高亮當前行
if has("gui_running")
autocmd InsertLeave * se nocul
autocmd InsertEnter * se cul
endif
filetype plugin on "允許使用ftplugin目錄下的文件類型特定腳本
filetype indent on "允許使用indent目錄下的文件類型縮進
" 設(shè)置自動粘貼功能。
set pastetoggle=<F3>
" 設(shè)置以縮進的方式自動折疊和設(shè)置快捷方式
set foldmethod=indent
" map <F3> zO " 打開折疊
map <F4> zc " 關(guān)閉折疊
map <F5> zR " 打開所有折疊
map <F6> zM " 關(guān)閉所有折疊
" 保存代碼文件前自動修改最后修改時間
au BufWritePre *.sh call TimeStamp('#')
au BufWritePre .vimrc,*.vim call TimeStamp('"')
au BufWritePre *.c,*.h call TimeStamp('//')
au BufWritePre *.cpp,*.hpp call TimeStamp('//')
au BufWritePre *.cxx,*.hxx call TimeStamp('//')
au BufWritePre *.java call TimeStamp('//')
au BufWritePre *.rb call TimeStamp('#')
au BufWritePre *.py call TimeStamp('#')
au BufWritePre Makefile call TimeStamp('#')
au BufWritePre *.php
\call TimeStamp('<?php //', '?>')
au BufWritePre *.html,*htm
\call TimeStamp('<!--', '-->')
" 更改Leader為","
let g:C_MapLeader = ','
" 設(shè)置顯示字體
if has("win32")
" set guifont=Courier_New:h11:cANSI
set guifont=YaHei\ Mono:h11
"set guifontwide=Microsoft\ Yahei\ Monotype:h11
"set guifont=YaHei\ Consolas\ Hybrid:h12
endif
ab xmain int main(int argc, char *argv[])<cr>{<cr>}<up><cr>return
" 使用 murphy 調(diào)色板
colo murphy
" 不要閃爍
set novisualbell
" 能夠漂亮地顯示.NFO文件
set encoding=utf-8
function! SetFileEncodings(encodings)
let b:myfileencodingsbak=&fileencodings
let &fileencodings=a:encodings
endfunction
function! RestoreFileEncodings()
let &fileencodings=b:myfileencodingsbak
unlet b:myfileencodingsbak
endfunction
au BufReadPre *.nfo call SetFileEncodings('cp437')|set ambiwidth=single
au BufReadPost *.nfo call RestoreFileEncodings()
" 用空格鍵來開關(guān)折疊
set foldenable
set foldmethod=manual
nnoremap <space> @=((foldclosed(line('.')) < 0) ? 'zc' : 'zo')<CR>
" 隱藏掉菜單和工具條。
set guioptions-=m
set guioptions-=T
map <silent> <F2> :if &guioptions =~# 'T' <Bar>
\set guioptions-=T <Bar>
\set guioptions-=m <bar>
\else <Bar>
\set guioptions+=T <Bar>
\set guioptions+=m <Bar>
\endif<CR>
" 標簽頁設(shè)置
if has("gui_running")
set showtabline=2
map! tn tabnew
nmap <C-c> :tabclose<CR>
endif
" 標簽頁只顯示文件名
function ShortTabLabel ()
let bufnrlist = tabpagebuflist (v:lnum)
let label = bufname (bufnrlist[tabpagewinnr (v:lnum) -1])
let filename = fnamemodify (label, ':t')
return filename
endfunction
set guitablabel=%{ShortTabLabel()}
" 使回格鍵(backspace)正常處理indent, eol, start等
set backspace=eol,start,indent
" 允許backspace和光標鍵跨越行邊界
" set whichwrap+=<,>,h,l
" 可以在buffer的任何地方使用鼠標(類似office中在工作區(qū)雙擊鼠標定位)
set mouse=a
set selection=exclusive
set selectmode=mouse,key
" 啟動的時候不顯示那個援助索馬里兒童的提示
set shortmess=atI
" 通過使用: commands命令,告訴我們文件的哪一行被改變過
set report=0
" 不讓vim發(fā)出討厭的滴滴聲
set noerrorbells
" 在被分割的窗口間顯示空白,便于閱讀
set fillchars=vert:\ ,stl:\ ,stlnc:\
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 搜索和匹配 "
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 高亮顯示匹配的括號
set showmatch
" 匹配括號高亮的時間(單位是十分之一秒)
set matchtime=3
" 在搜索的時候忽略大小寫
set ignorecase
" 不要高亮被搜索的句子(phrases)
" set nohlsearch
" 在搜索時,輸入的詞句的逐字符高亮(類似firefox的搜索)
set incsearch
" 輸入:set list命令是應(yīng)該顯示些啥?
set listchars=tab:\|\ ,trail:.,extends:>,precedes:<,eol:$
" Tab補全時忽略這些忽略這些
set wildignore=*.o,*.obj,*.bak,*.exe
" 光標移動到buffer的頂部和底部時保持3行距離
set scrolloff=3
"搜索出之后高亮關(guān)鍵詞
set hlsearch
nmap <silent> <leader><cr> :noh<cr>
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" CTags的設(shè)定 "
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set tags=tags;
" 按照名稱排序
let Tlist_Sort_Type = "name"
" 在右側(cè)顯示窗口
let Tlist_Use_Right_Window = 1
" 壓縮方式
let Tlist_Compart_Format = 1
" 如果只有一個buffer,kill窗口也kill掉buffer
let Tlist_Exist_OnlyWindow = 1
" 不要關(guān)閉其他文件的tags
let Tlist_File_Fold_Auto_Close = 0
" 不要顯示折疊樹
let Tlist_Enable_Fold_Column = 0
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 快捷鍵映射 "
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"設(shè)置其他快捷鍵
map <F7> :vsplit<ENTER>:edit `pwd`<ENTER>i
"F7 打開左側(cè)垂直窗口選擇待編輯的文件
" 自動完成的縮進。
if has("autocmd")
if has("gui")
autocmd WinLeave * set nocursorline nocursorcolumn
autocmd WinEnter * set cursorline cursorcolumn
else
autocmd WinLeave * set nocursorline nocursorcolumn
autocmd WinEnter * set cursorline nocursorcolumn
endif
" Enable file type detection.
" load view saved by the mkview command
autocmd FileType * loadview
autocmd FileType * set noexpandtab
autocmd BufWinEnter * loadview
" Use the default filetype settings, so that mail gets 'tw' set to 72,
" 'cindent' is on in C files, etc.
" Also load indent files, to automatically do language-dependent indenting.
filetype plugin indent on
" In text files, always limit the width of text to 78 characters
autocmd FileType text set textwidth=78 expandtab softtabstop=4
autocmd FileType sh set shiftwidth=4 expandtab softtabstop=4
autocmd FileType php set shiftwidth=4 expandtab softtabstop=4
autocmd FileType html set shiftwidth=4 expandtab softtabstop=4
autocmd FileType javascript set shiftwidth=4 expandtab softtabstop=4
autocmd FileType python set shiftwidth=4 expandtab softtabstop=4
autocmd FileType ruby set shiftwidth=4 expandtab softtabstop=4
autocmd FileType eruby set shiftwidth=4 expandtab softtabstop=4
autocmd FileType sql set shiftwidth=4 expandtab softtabstop=4
" When editing a file, always jump to the last known cursor position.
" Don't do it when the position is invalid or when inside an event handler
" (happens when dropping a file on gvim).
autocmd BufReadPost *
\ if line("'\"") > 0 && line("'\"") <= line("$") |
\ execute "normal g`\"" |
\ endif
augroup prog
" Remove all cprog autocommands
autocmd!
" When starting to edit a file:
" For C and C++ files set formatting of comments and set C-indenting on.
" For other files switch it off.
" Don't change the order, it's important that the line with * comes first.
autocmd FileType * set formatoptions=tcoql nocindent comments&
autocmd BufWinLeave *.sh,*.c,*.cpp,*.perl,*.py mkview
autocmd BufWinEnter *.sh,*.c,*.cpp,*.perl,*.py silent loadview
function! CleverTab()
if strpart( getline('.'), 0, col('.')-1 ) =~ '^\s*$'
return "\<Tab>"
else
return "\<C-N>"
endfunction
autocmd FileType c,cpp noremap! <S-Tab> <C-R>=CleverTab()<CR>
autocmd FileType c,cpp noremap! <C-]> <C-X><C-]>
autocmd FileType c,cpp noremap! <C-F> <C-X><C-F>
autocmd FileType c,cpp noremap! <C-D> <C-X><C-D>
autocmd FileType c,cpp noremap! <C-L> <C-X><C-L>
autocmd FileType c,cpp,sh,perl,python set fileformat=unix
autocmd FileType sh set formatoptions=croql cindent comments=b:#
autocmd FileType c,cpp set expandtab shiftwidth=4 softtabstop=4
augroup END
endif " has("autocmd")
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" PYTHON 相關(guān)的設(shè)置 "
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"Python 文件的一般設(shè)置,比如不要 tab 等
"設(shè)置自動縮進為4,插入模式里: 插入 <Tab> 時使用合適數(shù)量的空格。
"要插入實際的制表,可用 CTRL-V<Tab>
autocmd FileType python setlocal expandtab | setlocal shiftwidth=4 |
\setlocal softtabstop=4 | setlocal textwidth=76 |
\tabstop=4
"pydiction插件設(shè)置(需要安裝pydiction插件)
"設(shè)置python的字典路徑
let g:pydiction_location = '~/.vim/skeleton/pydict/complete-dict'
"設(shè)置pydict的菜單高度
let g:pydiction_menu_height = 10
"Python Unittest 的一些設(shè)置
"在編寫 Python 代碼及 unittest 測試時不需要離開 vim
"鍵入 :make 或者點擊 gvim 工具條上的 make 按鈕就自動執(zhí)行測試用例
autocmd FileType python compiler pyunit
autocmd FileType python setlocal makeprg=python\ ~/.vim/skeleton/alltests.py
autocmd BufNewFile,BufRead test*.py setlocal makeprg=python\ %
"自動使用新文件模板(需要提前準備skeleton相關(guān)文件)
autocmd BufNewFile test*.py 0r ~/.vim/skeleton/test.py
autocmd BufNewFile alltests.py 0r ~/.vim/skeleton/alltests.py
autocmd BufNewFile wx*.py 0r ~/.vim/skeleton/wxskeleton.py
autocmd BufNewFile *.py 0r ~/.vim/skeleton/skeleton.py
"設(shè)置= + - * 前后自動空格
"設(shè)置,后面自動添加空格
au FileType python inoremap <buffer>= <c-r>=EqualSign('=')<CR>
au FileType python inoremap <buffer>+ <c-r>=EqualSign('+')<CR>
au FileType python inoremap <buffer>- <c-r>=EqualSign('-')<CR>
au FileType python inoremap <buffer>* <c-r>=EqualSign('*')<CR>
au FileType python inoremap <buffer>/ <c-r>=EqualSign('/')<CR>
au FileType python inoremap <buffer>> <c-r>=EqualSign('>')<CR>
au FileType python inoremap <buffer>< <c-r>=EqualSign('<')<CR>
au FileType python inoremap <buffer>: <c-r>=Swap()<CR>
au FileType python inoremap <buffer>, ,<space>
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 腳本內(nèi)部用到的自定義函數(shù) "
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
"函數(shù)后面加上!是防止vimrc文件重新載入時報錯
"實現(xiàn)光標位置自動交換:) --> ):
function! Swap()
if getline('.')[col('.') - 1] =~ ")"
return "\<ESC>la:"
else
return ":"
endif
endf
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
"實現(xiàn)+-*/前后自動添加空格,逗號后面自動添加空格,適用python
"支持+= -+ *= /+格式
function! EqualSign(char)
if a:char =~ '=' && getline('.') =~ ".*("
return a:char
endif
let ex1 = getline('.')[col('.') - 3]
let ex2 = getline('.')[col('.') - 2]
if ex1 =~ "[-=+><>\/\*]"
if ex2 !~ "\s"
return "\<ESC>i".a:char."\<SPACE>"
else
return "\<ESC>xa".a:char."\<SPACE>"
endif
else
if ex2 !~ "\s"
return "\<SPACE>".a:char."\<SPACE>\<ESC>a"
else
return a:char."\<SPACE>\<ESC>a"
endif
endif
endf
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
"實現(xiàn)括號的自動配對后防止重復(fù)輸入),適用python
function! ClosePair(char)
if getline('.')[col('.') - 1] == a:char
return "\<Right>"
else
return a:char
endif
endf
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
"SwitchToBuf()實現(xiàn)它在所有標簽頁的窗口中查找指定的文件名,如果找到這樣一個窗口,
"就跳到此窗口中;否則,它新建一個標簽頁來打開vimrc文件
"上面自動編輯.vimrc文件用到的函數(shù)
function! SwitchToBuf(filename)
let bufwinnr = bufwinnr(a:filename)
if bufwinnr != -1
exec bufwinnr . "wincmd w"
return
else
" find in each tab
tabfirst
let tab = 1
while tab <= tabpagenr("$")
let bufwinnr = bufwinnr(a:filename)
if bufwinnr != -1
exec "normal " . tab . "gt"
exec bufwinnr . "wincmd w"
return
endif
tabnext
let tab = tab + 1
endwhile
" not exist, new tab
exec "tabnew " . a:filename
endif
endfunction
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
"Last change用到的函數(shù),返回時間,能夠自動調(diào)整位置
function! TimeStamp(...)
let sbegin = ''
let send = ''
if a:0 >= 1
let sbegin = a:1.'\s*'
endif
if a:0 >= 2
let send = ' '.a:2
endif
let pattern = 'Last Change: .\+'
\. send
let pattern = '^\s*' . sbegin . pattern . '\s*$'
let now = strftime('%Y-%m-%d %H:%M:%S',
\localtime())
let row = search(pattern, 'n')
if row == 0
let now = a:1 . ' Last Change: '
\. now . send
call append(2, now)
else
let curstr = getline(row)
let col = match( curstr , 'Last')
let spacestr = repeat(' ',col - 1)
let now = a:1 . spacestr . 'Last Change: '
\. now . send
call setline(row, now)
endif
endfunction
原文地址