1 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 2 " 一般設定 3 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 4 " 設定默認解碼 5 set fenc=utf-8 6 set fencs=utf-8,usc-bom,euc-jp,gb18030,gbk,gb2312,cp936 7 8 " 不要使用vi的鍵盤模式,而是vim自己的 9 set nocompatible 10 11 " history文件中需要記錄的行數 12 set history=100 13 14 " 在處理未保存或只讀文件的時候,彈出確認 15 set confirm 16 17 " 與windows共享剪貼板 18 set clipboard+=unnamed 19 20 " 偵測文件類型 21 filetype on 22 23 " 載入文件類型插件 24 filetype plugin on 25 26 " 為特定文件類型載入相關縮進文件 27 filetype indent on 28 29 " 保存全局變量 30 set viminfo+=! 31 32 " 帶有如下符號的單詞不要被換行分割 33 set iskeyWord+=_,$,@,%,#,- 34 35 " 語法高亮 36 syntax on 37 38 " 高亮字符,讓其不受100列限制 39 :highlight OverLength ctermbg=red ctermfg=white guibg=red guifg=white 40 :match OverLength '/%101v.*' 41 42 " 狀態行顏色 43 highlight StatusLine guifg=SlateBlue guibg=Yellow 44 highlight StatusLineNC guifg=Gray guibg=White 45 46 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 47 " 文件設置 48 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 49 " 不要備份文件(根據自己需要取舍) 50 set nobackup 51 52 " 不要生成swap文件,當buffer被丟棄的時候隱藏它 53 setlocal noswapfile 54 set bufhidden=hide 55 56 " 字符間插入的像素行數目 57 set linespace=0 58 59 " 增強模式中的命令行自動完成操作 60 set wildmenu 61 62 " 在狀態行上顯示光標所在位置的行號和列號 63 set ruler 64 set rulerformat=%20(%2*%<%f%=/ %m%r/ %3l/ %c/ %p%%%) 65 66 " 命令行(在狀態行下)的高度,默認為1,這里是2 67 set cmdheight=2 68 69 " 使回格鍵(backspace)正常處理indent, eol, start等 70 set backspace=2 71 72 " 允許backspace和光標鍵跨越行邊界 73 set whichwrap+=<,>,h,l 74 75 " 可以在buffer的任何地方使用鼠標(類似office中在工作區雙擊鼠標定位) 76 set mouse=a 77 set selection=exclusive 78 set selectmode=mouse,key 79 80 " 啟動的時候不顯示那個援助索馬里兒童的提示 81 set shortmess=atI 82 83 " 通過使用: commands命令,告訴我們文件的哪一行被改變過 84 set report=0 85 86 " 不讓vim發出討厭的滴滴聲 87 set noerrorbells 88 89 " 在被分割的窗口間顯示空白,便于閱讀 90 set fillchars=vert:/ ,stl:/ ,stlnc:/ 91 92 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 93 " 搜索和匹配 94 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 95 " 高亮顯示匹配的括號 96 set showmatch 97 98 " 匹配括號高亮的時間(單位是十分之一秒) 99 set matchtime=5 100 101 " 在搜索的時候忽略大小寫 102 set ignorecase 103 104 " 不要高亮被搜索的句子(phrases) 105 set nohlsearch 106 107 " 在搜索時,輸入的詞句的逐字符高亮(類似Firefox的搜索) 108 set incsearch 109 110 " 輸入:set list命令是應該顯示些啥? 111 set listchars=tab:/|/ ,trail:.,extends:>,PRecedes:<,eol:$ 112 113 " 光標移動到buffer的頂部和底部時保持3行距離 114 set scrolloff=3 115 116 " 不要閃爍 117 set novisualbell 118 119 " 我的狀態行顯示的內容(包括文件類型和解碼) 120 set statusline=%F%m%r%h%w/ [FORMAT=%{&ff}]/ [TYPE=%Y]/ [POS=%l,%v][%p%%]/ %{strftime(/"%d/%m/%y/ -/ %H:%M/")} 121 122 " 總是顯示狀態行 123 set laststatus=2 124 125 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 126 " 文本格式和排版 127 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 128 " 自動格式化 129 set formatoptions=tcrqn 130 131 " 繼承前一行的縮進方式,特別適用于多行注釋 132 set autoindent 133 134 " 為C程序提供自動縮進 135 set smartindent 136 137 " 使用C樣式的縮進 138 set cindent 139 140 " 制表符為4 141 set tabstop=4 142 143 " 統一縮進為4 144 set softtabstop=4 145 set shiftwidth=4 146 147 " 不要用空格代替制表符 148 set noexpandtab 149 150 " 不要換行 151 set nowrap 152 153 " 在行和段開始處使用制表符 154 set smarttab 155 156 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 157 " CTags的設定 158 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 159 " 按照名稱排序 160 let Tlist_Sort_Type = "name" 161 162 " 在右側顯示窗口 163 let Tlist_Use_Right_Window = 1 164 165 " 壓縮方式 166 let Tlist_Compart_Format = 1 167 168 " 如果只有一個buffer,kill窗口也kill掉buffer 169 let Tlist_Exist_OnlyWindow = 1 170 171 " 不要關閉其他文件的tags 172 let Tlist_File_Fold_Auto_Close = 0 173 174 " 不要顯示折疊樹 175 let Tlist_Enable_Fold_Column = 0 176 177 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 178 " Autocommands 179 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 180 " 只在下列文件類型被偵測到的時候顯示行號,普通文本文件不顯示 181 182 if has("autocmd") 183 autocmd FileType xml,html,c,cs,java,perl,shell,bash,cpp,python,vim,php,ruby set number 184 autocmd FileType xml,html vmap 'o'>o--> 185 autocmd FileType java,c,cpp,cs vmap ''>o*/ 186 autocmd FileType html,text,php,vim,c,java,xml,bash,shell,perl,python setlocal textwidth=100 187 autocmd Filetype html,xml,xsl source $VIMRUNTIME/plugin/closetag.vim 188 autocmd BufReadPost * 189 / if line("'/"") > 0 && line("'/"") <= line("$") | 190 / exe "normal g`/"" | 191 / endif 192 endif " has("autocmd") 193 194 " F5編譯和運行C程序,F6編譯和運行C++程序 195 " 請注意,下述代碼在windows下使用會報錯 196 " 需要去掉./這兩個字符 197 198 " C的編譯和運行 199 map :call CompileRunGcc() 200 func! CompileRunGcc() 201 exec "w" 202 exec "!gcc % -o %<" 203 exec "! ./%<" 204 endfunc 205 206 " C++的編譯和運行 207 map :call CompileRunGpp() 208 func! CompileRunGpp() 209 exec "w" 210 exec "!g++ % -o %<" 211 exec "! ./%<" 212 endfunc 213 214 " 能夠漂亮地顯示.NFO文件 215 set encoding=utf-8 216 function! SetFileEncodings(encodings) 217 let b:myfileencodingsbak=&fileencodings 218 let &fileencodings=a:encodings 219 endfunction 220 function! RestoreFileEncodings() 221 let &fileencodings=b:myfileencodingsbak 222 unlet b:myfileencodingsbak 223 endfunction 224 225 au BufReadPre *.nfo call SetFileEncodings('cp437')|set ambiwidth=single 226 au BufReadPost *.nfo call RestoreFileEncodings() 227 228 " 高亮顯示普通txt文件(需要txt.vim腳本) 229 au BufRead,BufNewFile * setfiletype txt 230 231 " 用空格鍵來開關折疊 232 set foldenable 233 set foldmethod=manual 234 nnoremap @=((foldclosed(line('.')) < 0) ? 'zc' : 'zo') 235 236 " minibufexpl插件的一般設置 237 let g:miniBufExplMapWindowNavVim = 1 238 let g:miniBufExplMapWindowNavArrows = 1 239 let g:miniBufExplMapCTabSwitchBufs = 1 240 let g:miniBufExplModSelTarget = 1
1 let mapleader = ";" " 比較習慣用;作為命令前綴,右手小拇指直接能按到 2 " 把空格鍵映射成: 3 nmap <space> : 4 5 " 快捷打開編輯vimrc文件的鍵盤綁定 6 map <silent> <leader>ee :e $HOME/.vimrc<cr> 7 autocmd! bufwritepost *.vimrc source $HOME/.vimrc 8 9 " ^z快速進入shell 10 nmap <C-Z> :shell<cr> 11 inoremap <leader>n <esc> 12 13 " 判斷操作系統 14 if (has("win32") || has("win64") || has("win32unix")) 15 let g:isWin = 1 16 else 17 let g:isWin = 0 18 endif 19 20 " 判斷是終端還是gvim 21 if has("gui_running") 22 let g:isGUI = 1 23 else 24 let g:isGUI = 0 25 endif 26 27 set nocompatible " 關閉兼容模式 28 syntax enable " 語法高亮 29 filetype plugin on " 文件類型插件 30 filetype indent on 31 set shortmess=atI " 去掉歡迎界面 32 set autoindent 33 autocmd BufEnter * :syntax sync fromstart 34 set nu " 顯示行號 35 set showcmd " 顯示命令 36 set lz " 當運行宏時,在命令執行完成之前,不重繪屏幕 37 set hid " 可以在沒有保存的情況下切換buffer 38 set backspace=eol,start,indent 39 set whichwrap+=<,>,h,l " 退格鍵和方向鍵可以換行 40 set incsearch " 增量式搜索 41 set nohlsearch 42 "set hlsearch " 高亮搜索 43 set ignorecase " 搜索時忽略大小寫 44 set magic " 額,自己:h magic吧,一行很難解釋 45 set showmatch " 顯示匹配的括號 46 set nobackup " 關閉備份 47 set nowb 48 set noswapfile " 不使用swp文件,注意,錯誤退出后無法恢復 49 set lbr " 在breakat字符處而不是最后一個字符處斷行 50 set ai " 自動縮進 51 set si " 智能縮進 52 set cindent " C/C++風格縮進 53 set wildmenu 54 set nofen 55 set fdl=10 56 57 " tab轉化為4個字符 58 set expandtab 59 set smarttab 60 set shiftwidth=4 61 set tabstop=4 62 63 " 不使用beep或Flash 64 set vb t_vb= 65 66 set background=dark 67 set t_Co=256 68 colorscheme xoria256 69 70 set history=400 " vim記住的歷史操作的數量,默認的是20 71 set autoread " 當文件在外部被修改時,自動重新讀取 72 set mouse=n " 在所有模式下都允許使用鼠標,還可以是n,v,i,c等 73 74 "在gvim中高亮當前行 75 if (g:isGUI) 76 set cursorline 77 colorscheme wombat 78 hi cursorline guibg=#333333 79 hi CursorColumn guibg=#333333 80 "set guifont=Consolas/ 10 81 "set guifontwide=Consolas/ 10 82 set guifont=DejaVu/ Sans/ Mono/ 10 83 set gfw=DejaVu/ Sans/ Mono/ 10 84 " 不顯示toolbar 85 set guioptions-=T 86 " 不顯示菜單欄 87 "set guioptions-=m 88 endif 89 90 " 設置字符集編碼,默認使用utf8 91 if (g:isWin) 92 let &termencoding=&encoding " 通常win下的encoding為cp936 93 set fileencodings=utf8,cp936,ucs-bom,latin1 94 else 95 set encoding=utf8 96 set fileencodings=utf8,gb2312,gb18030,ucs-bom,latin1 97 endif 98 99 " 狀態欄100 set laststatus=2 " 總是顯示狀態欄101 highlight StatusLine cterm=bold ctermfg=yellow ctermbg=blue102 " 獲取當前路徑,將$HOME轉化為~103 function! CurDir()104 let curdir = substitute(getcwd(), $HOME, "~", "g")105 return curdir106 endfunction107 set statusline=[%n]/ %f%m%r%h/ /|/ / pwd:/ %{CurDir()}/ / /|%=/|/ %l,%c/ %p%%/ /|/ ascii=%b,hex=%b%{((&fenc==/"/")?/"/":/"/ /|/ /".&fenc)}/ /|/ %{$USER}/ @/ %{hostname()}/ 108 109 " 第80列往后加下劃線110 "au BufWinEnter * let w:m2=matchadd('Underlined', '/%>' . 80 . 'v./+', -1)111 112 " 根據給定方向搜索當前光標下的單詞,結合下面兩個綁定使用113 function! VisualSearch(direction) range114 let l:saved_reg = @"115 execute "normal! vgvy"116 let l:pattern = escape(@", '///.*$^~[]')117 let l:pattern = substitute(l:pattern, "/n$", "", "")118 if a:direction == 'b'119 execute "normal ?" . l:pattern . "<cr>"120 else121 execute "normal /" . l:pattern . "<cr>"122 endif123 let @/ = l:pattern124 let @" = l:saved_reg125 endfunction126 " 用 */# 向 前/后 搜索光標下的單詞127 vnoremap <silent> * :call VisualSearch('f')<CR>128 vnoremap <silent> # :call VisualSearch('b')<CR>129 130 " 在文件名上按gf時,在新的tab中打開131 "map gf :tabnew <cfile><cr>132 133 " 用c-j,k在buffer之間切換134 nn <C-J> :bn<cr>135 nn <C-K> :bp<cr>136 137 " Bash(Emacs)風格鍵盤綁定138 imap <C-e> <END>139 imap <C-a> <HOME>140 "imap <C-u> <esc>d0i141 "imap <C-k> <esc>d$i " 與自動補全中的綁定沖突142 143 "從系統剪切板中復制,剪切,粘貼144 map <F7> "+y145 map <F8> "+x146 map <F9> "+p147 148 " 恢復上次文件打開位置149 set viminfo='10,/"100,:20,%,n~/.viminfo150 au BufReadPost * if line("'/"") > 0|if line("'/"") <= line("$")|exe("norm '/"")|else|exe "norm $"|endif|endif151 152 " 刪除buffer時不關閉窗口153 command! Bclose call <SID>BufcloseCloseIt()154 function! <SID>BufcloseCloseIt()155 let l:currentBufNum = bufnr("%")156 let l:alternateBufNum = bufnr("#")157 158 if buflisted(l:alternateBufNum)159 buffer #160 else161 bnext162 endif163 164 if bufnr("%") == l:currentBufNum165 new166 endif167 168 if buflisted(l:currentBufNum)169 execute("bdelete! ".l:currentBufNum)170 endif171 endfunction172 173 174 " 快捷輸入175 " 自動完成括號和引號176 inoremap <leader>1 ()<esc>:let leavechar=")"<cr>i177 inoremap <leader>2 []<esc>:let leavechar="]"<cr>i178 inoremap <leader>3 {}<esc>:let leavechar="}"<cr>i179 inoremap <leader>4 {<esc>o}<esc>:let leavechar="}"<cr>O180 inoremap <leader>q ''<esc>:let leavechar="'"<cr>i181 inoremap <leader>w ""<esc>:let leavechar='"'<cr>i182 183 184 " 插件窗口的寬度,如TagList,NERD_tree等,自己設置185 let s:PlugWinSize = 25186 187 " taglist.vim188 " http://www.vim.org/scripts/script.php?script_id=273189 " <leader>t 打開TagList窗口,窗口在右邊190 nmap <silent> <leader>t :TlistToggle<cr>191 "let Tlist_Ctags_Cmd = '/usr/bin/ctags'192 let Tlist_Show_One_File = 0193 let Tlist_Exit_OnlyWindow = 1 194 let Tlist_Use_Right_Window = 1195 let Tlist_File_Fold_Auto_Close = 1196 let Tlist_GainFocus_On_ToggleOpen = 0197 let Tlist_WinWidth = s:PlugWinSize198 let Tlist_Auto_Open = 0199 let Tlist_Display_Prototype = 0200 "let Tlist_Close_On_Select = 1201 202 203 " OmniCppComplete.vim204 " http://www.vim.org/scripts/script.php?script_id=1520205 set completeopt=menu206 let OmniCpp_ShowPrototypeInAbbr = 1 207 let OmniCpp_DefaultNamespaces = ["std"] " 逗號分割的字符串208 let OmniCpp_MayCompleteScope = 1 209 let OmniCpp_ShowPrototypeInAbbr = 0 210 let OmniCpp_SelectFirstItem = 2 211 " c-j自動補全,當補全菜單打開時,c-j,k上下選擇212 imap <expr> <c-j> pumvisible()?"/<C-N>":"/<C-X><C-O>"213 imap <expr> <c-k> pumvisible()?"/<C-P>":"/<esc>"214 " f:文件名補全,l:行補全,d:字典補全,]:tag補全215 imap <C-]> <C-X><C-]>216 imap <C-F> <C-X><C-F>217 imap <C-D> <C-X><C-D>218 imap <C-L> <C-X><C-L> 219 220 " NERD_commenter.vim221 " http://www.vim.org/scripts/script.php?script_id=1218222 " Toggle單行注釋/“性感”注釋/注釋到行尾/取消注釋223 map <leader>cc ,c<space>224 map <leader>cs ,cs225 map <leader>c$ ,c$226 map <leader>cu ,cu227 228 " NERD tree229 " http://www.vim.org/scripts/script.php?script_id=1658230 let NERDTreeShowHidden = 1231 let NERDTreeWinPos = "left"232 let NERDTreeWinSize = s:PlugWinSize 233 nmap <leader>n :NERDTreeToggle<cr>234 235 " DoxygenToolkit.vim236 " http://www.vim.org/scripts/script.php?script_id=987237 " 暫時沒有使用238 239 " 更新ctags和cscope索引240 " href: http://www.vimer.cn/2009/10/把vim打造成一個真正的ide2.html241 " 稍作修改,提取出DeleteFile函數,修改ctags和cscope執行命令242 map <F6> :call Do_CsTag()<cr>243 function! Do_CsTag()244 let dir = getcwd()245 246 "先刪除已有的tags和cscope文件,如果存在且無法刪除,則報錯。247 if ( DeleteFile(dir, "tags") ) 248 return 249 endif250 if ( DeleteFile(dir, "cscope.files") ) 251 return 252 endif253 if ( DeleteFile(dir, "cscope.out") ) 254 return 255 endif256 257 if(executable('ctags'))258 silent! execute "!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q ."259 endif260 if(executable('cscope') && has("cscope") )261 if(g:isWin)262 silent! execute "!dir /s/b *.c,*.cpp,*.h,*.java,*.cs >> cscope.files"263 else264 silent! execute "!find . -iname '*.[ch]' -o -name '*.cpp' > cscope.files"265 endif266 silent! execute "!cscope -b"267 execute "normal :"268 if filereadable("cscope.out")269 execute "cs add cscope.out"270 endif271 endif272 " 刷新屏幕273 execute "redr!"274 endfunction275 276 function! DeleteFile(dir, filename)277 if filereadable(a:filename)278 if (g:isWin)279 let ret = delete(a:dir."//".a:filename)280 else281 let ret = delete("./".a:filename)282 endif283 if (ret != 0)284 echohl WarningMsg | echo "Failed to delete ".a:filename | echohl None285 return 1286 else287 return 0288 endif289 endif290 return 0291 endfunction292 293 " cscope 綁定294 if has("cscope")295 set csto=1296 set cst297 set nocsverb298 if filereadable("cscope.out")299 cs add cscope.out300 endif301 set csverb302 " s: C語言符號 g: 定義 d: 這個函數調用的函數 c: 調用這個函數的函數303 " t: 文本 e: egrep模式 f: 文件 i: include本文件的文件304 nmap <leader>ss :cs find s <C-R>=expand("<cword>")<CR><CR>:copen<CR>305 nmap <leader>sg :cs find g <C-R>=expand("<cword>")<CR><CR>306 nmap <leader>sc :cs find c <C-R>=expand("<cword>")<CR><CR>:copen<CR>307 nmap <leader>st :cs find t <C-R>=expand("<cword>")<CR><CR>:copen<CR>308 nmap <leader>se :cs find e <C-R>=expand("<cword>")<CR><CR>:copen<CR>309 nmap <leader>sf :cs find f <C-R>=expand("<cfile>")<CR><CR>:copen<CR>310 nmap <leader>si :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>:copen<CR>311 nmap <leader>sd :cs find d <C-R>=expand("<cword>")<CR><CR>:copen<CR>312 endif313 314 " Quick Fix 設置315 map <F3> :cw<cr>316 map <F4> :cp<cr>317 map <F5> :cn<cr>318 319 " Buffers Explorer (需要genutils.vim)320 " http://vim.sourceforge.net/scripts/script.php?script_id=42321 " http://www.vim.org/scripts/script.php?script_id=197322 let g:bufExplorerDefaultHelp=0 " Do not show default help.323 let g:bufExplorerShowRelativePath=1 " Show relative paths.324 let g:bufExplorerSortBy='mru' " Sort by most recently used.325 let g:bufExplorerSplitRight=0 " Split left.326 let g:bufExplorerSplitVertical=1 " Split vertically.327 let g:bufExplorerSplitVertSize = s:PlugWinSize " Split width328 let g:bufExplorerUseCurrentWindow=1 " Open in new window.329 autocmd BufWinEnter /[Buf/ List/] setl nonumber330 nmap <silent> <Leader>b :BufExplorer<CR>331 332 " Vimwiki配置333 let g:vimwiki_list = [{'path': '~/Dropbox/MyWiki/my_site/',334 / 'path-html': '~/Dropbox/MyWiki/my_site_html/',335 / 'html-header': '~/Dropbox/MyWiki/templates/header.tpl',336 / 'html-footer': '~/Dropbox/MyWiki/templates/footer.tpl'}]337 let g:vimwiki_camel_case=0338 339 let wiki = {}340 let wiki.path = '~/Dropbox/MyWiki/my_site/'341 let wiki.nested_syntaxes = {'python': 'python', 'c++': 'cpp'}342 let g:vimwiki_list = [wiki]343 344 "pythoncomplete配置345 autocmd filetype python set omnifunc=pythoncomplete#Complete
新聞熱點
疑難解答