_vimrc win7 gvim
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 set nocompatible 2 source $VIMRUNTIME/vimrc_example.vim 3 source $VIMRUNTIME/mswin.vim 4 behave mswin 5 6 set diffexpr=MyDiff() 7 function MyDiff() 8 let opt = '-a --binary ' 9 if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif 10 if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif 11 let arg1 = v:fname_in 12 if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif 13 let arg2 = v:fname_new 14 if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif 15 let arg3 = v:fname_out 16 if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif 17 let eq = '' 18 if $VIMRUNTIME =~ ' ' 19 if &sh =~ '\<cmd' 20 let cmd = '""' . $VIMRUNTIME . '\diff"' 21 let eq = '"' 22 else 23 let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"' 24 endif 25 else 26 let cmd = $VIMRUNTIME . '\diff' 27 endif 28 silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq 29 endfunction 30 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 31 " => General 32 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 33 " Sets how many lines of history VIM has to remember 34 set history=700 35 36 " Enable filetype plugins 37 filetype plugin on 38 filetype indent on 39 40 " Set to auto read when a file is changed from the outside 41 set autoread 42 43 " With a map leader it's possible to do extra key combinations 44 " like <leader>w saves the current file 45 let mapleader = "," 46 let g:mapleader = "," 47 48 " Fast saving 49 nmap <leader>w :w!<cr> 50 51 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 52 " => VIM user interface 53 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 54 " Set 7 lines to the cursor - when moving vertically using j/k 55 set so=7 56 " show nu 57 set nu 58 " Turn on the WiLd menu 59 set wildmenu 60 61 " Ignore compiled files 62 set wildignore=*.o,*~,*.pyc 63 64 "Always show current position 65 set ruler 66 67 " Height of the command bar 68 set cmdheight=2 69 70 " A buffer becomes hidden when it is abandoned 71 set hid 72 73 " Configure backspace so it acts as it should act 74 set backspace=eol,start,indent 75 set whichwrap+=<,>,h,l 76 77 " Ignore case when searching 78 set ignorecase 79 80 " When searching try to be smart about cases 81 set smartcase 82 83 " Highlight search results 84 set hlsearch 85 86 " Makes search act like search in modern browsers 87 set incsearch 88 89 " Don't redraw while executing macros (good performance config) 90 set lazyredraw 91 92 " For regular expressions turn magic on 93 set magic 94 95 " Show matching brackets when text indicator is over them 96 set showmatch 97 " How many tenths of a second to blink when matching brackets 98 set mat=2 99 100 " No annoying sound on errors 101 set noerrorbells 102 set novisualbell 103 set t_vb= 104 set tm=500 105 106 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 107 " => Colors and Fonts 108 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 109 " Enable syntax highlighting 110 syntax enable 111 colorscheme desert 112 set background=dark 113 " Set extra options when running in GUI mode 114 if has("gui_running") 115 set guioptions-=T 116 set guioptions+=e 117 set t_Co=256 118 set guitablabel=%M\ %t 119 endif 120 " Set utf8 as standard encoding and en_US as the standard language 121 " set encoding=utf8 122 123 " Use Unix as the standard file type 124 set ffs=unix,dos,mac 125 126 127 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 128 " => Files, backups and undo 129 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 130 " Turn backup off, since most stuff is in SVN, git et.c anyway... 131 set nobackup 132 set nowb 133 set noswapfile 134 135 136 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 137 " => Text, tab and indent related 138 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 139 " Use spaces instead of tabs 140 set expandtab 141 142 " Be smart when using tabs ;) 143 set smarttab 144 145 " 1 tab == 4 spaces 146 set shiftwidth=4 147 set tabstop=4 148 149 " Linebreak on 500 characters 150 set lbr 151 set tw=500 152 153 set ai "Auto indent 154 set si "Smart indent 155 set wrap "Wrap lines 156 157 158 """""""""""""""""""""""""""""" 159 " => Visual mode related 160 """""""""""""""""""""""""""""" 161 " Visual mode pressing * or # searches for the current selection 162 " Super useful! From an idea by Michael Naumann 163 vnoremap <silent> * :call VisualSelection('f')<CR> 164 vnoremap <silent> # :call VisualSelection('b')<CR> 165 166 167 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 168 " => Moving around, tabs, windows and buffers 169 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 170 " Treat long lines as break lines (useful when moving around in them) 171 map j gj 172 map k gk 173 174 " Map <Space> to / (search) and Ctrl-<Space> to ? (backwards search) 175 map <space> / 176 map <c-space> ? 177 178 " Disable highlight when <leader><cr> is pressed 179 map <silent> <leader><cr> :noh<cr> 180 181 " Smart way to move between windows 182 map <C-j> <C-W>j 183 map <C-k> <C-W>k 184 map <C-h> <C-W>h 185 map <C-l> <C-W>l 186 187 " Close the current buffer 188 map <leader>bd :Bclose<cr> 189 190 " Close all the buffers 191 map <leader>ba :1,1000 bd!<cr> 192 193 " Useful mappings for managing tabs 194 map <leader>tn :tabnew<cr> 195 map <leader>to :tabonly<cr> 196 map <leader>tc :tabclose<cr> 197 map <leader>tm :tabmove 198 199 " Opens a new tab with the current buffer's path 200 " Super useful when editing files in the same directory 201 map <leader>te :tabedit <c-r>=expand("%:p:h")<cr>/ 202 203 " Switch CWD to the directory of the open buffer 204 map <leader>cd :cd %:p:h<cr>:pwd<cr> 205 206 " Specify the behavior when switching between buffers 207 try 208 set switchbuf=useopen,usetab,newtab 209 set stal=2 210 catch 211 endtry 212 213 " Return to last edit position when opening files (You want this!) 214 autocmd BufReadPost * 215 \ if line("'\"") > 0 && line("'\"") <= line("$") | 216 \ exe "normal! g`\"" | 217 \ endif 218 " Remember info about open buffers on close 219 set viminfo^=% 220 221 222 """""""""""""""""""""""""""""" 223 " => Status line 224 """""""""""""""""""""""""""""" 225 " Always show the status line 226 set laststatus=2 227 228 " Format the status line 229 set statusline=\ %{HasPaste()}%F%m%r%h\ %w\ \ CWD:\ %r%{getcwd()}%h\ \ \ Line:\ %l 230 231 232 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 233 " => Editing mappings 234 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 235 " Remap VIM 0 to first non-blank character 236 map 0 ^ 237 238 " Move a line of text using ALT+[jk] or Comamnd+[jk] on mac 239 nmap <M-j> mz:m+<cr>`z 240 nmap <M-k> mz:m-2<cr>`z 241 vmap <M-j> :m'>+<cr>`<my`>mzgv`yo`z 242 vmap <M-k> :m'<-2<cr>`>my`<mzgv`yo`z 243 244 if has("mac") || has("macunix") 245 nmap <D-j> <M-j> 246 nmap <D-k> <M-k> 247 vmap <D-j> <M-j> 248 vmap <D-k> <M-k> 249 endif 250 251 " Delete trailing white space on save, useful for Python and CoffeeScript ;) 252 func! DeleteTrailingWS() 253 exe "normal mz" 254 %s/\s\+$//ge 255 exe "normal `z" 256 endfunc 257 autocmd BufWrite *.py :call DeleteTrailingWS() 258 autocmd BufWrite *.coffee :call DeleteTrailingWS() 259 260 261 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 262 " => vimgrep searching and cope displaying 263 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 264 " When you press gv you vimgrep after the selected text 265 vnoremap <silent> gv :call VisualSelection('gv')<CR> 266 267 " Open vimgrep and put the cursor in the right position 268 map <leader>g :vimgrep // **/*.<left><left><left><left><left><left><left> 269 270 " Vimgreps in the current file 271 map <leader><space> :vimgrep // <C-R>%<C-A><right><right><right><right><right><right><right><right><right> 272 273 " When you press <leader>r you can search and replace the selected text 274 vnoremap <silent> <leader>r :call VisualSelection('replace')<CR> 275 276 " Do :help cope if you are unsure what cope is. It's super useful! 277 " 278 " When you search with vimgrep, display your results in cope by doing: 279 " <leader>cc 280 " 281 " To go to the next search result do: 282 " <leader>n 283 " 284 " To go to the previous search results do: 285 " <leader>p 286 " 287 map <leader>cc :botright cope<cr> 288 map <leader>co ggVGy:tabnew<cr>:set syntax=qf<cr>pgg 289 map <leader>n :cn<cr> 290 map <leader>p :cp<cr> 291 292 293 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 294 " => Spell checking 295 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 296 " Pressing ,ss will toggle and untoggle spell checking 297 map <leader>ss :setlocal spell!<cr> 298 299 " Shortcuts using <leader> 300 map <leader>sn ]s 301 map <leader>sp [s 302 map <leader>sa zg 303 map <leader>s? z= 304 305 306 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 307 " => Misc 308 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 309 " Remove the Windows ^M - when the encodings gets messed up 310 noremap <Leader>m mmHmt:%s/<C-V><cr>//ge<cr>'tzt'm 311 312 " Quickly open a buffer for scripbble 313 map <leader>q :e ~/buffer<cr> 314 315 " Toggle paste mode on and off 316 map <leader>pp :setlocal paste!<cr> 317 318 319 320 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 321 " => Helper functions 322 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 323 function! CmdLine(str) 324 exe "menu Foo.Bar :" . a:str 325 emenu Foo.Bar 326 unmenu Foo 327 endfunction 328 329 function! VisualSelection(direction) range 330 let l:saved_reg = @" 331 execute "normal! vgvy" 332 333 let l:pattern = escape(@", '\\/.*$^~[]') 334 let l:pattern = substitute(l:pattern, "\n$", "", "") 335 336 if a:direction == 'b' 337 execute "normal ?" . l:pattern . "^M" 338 elseif a:direction == 'gv' 339 call CmdLine("vimgrep " . '/'. l:pattern . '/' . ' **/*.') 340 elseif a:direction == 'replace' 341 call CmdLine("%s" . '/'. l:pattern . '/') 342 elseif a:direction == 'f' 343 execute "normal /" . l:pattern . "^M" 344 endif 345 346 let @/ = l:pattern 347 let @" = l:saved_reg 348 endfunction 349 350 351 " Returns true if paste mode is enabled 352 function! HasPaste() 353 if &paste 354 return 'PASTE MODE ' 355 en 356 return '' 357 endfunction 358 359 " Don't close window, when deleting a buffer 360 command! Bclose call <SID>BufcloseCloseIt() 361 function! <SID>BufcloseCloseIt() 362 let l:currentBufNum = bufnr("%") 363 let l:alternateBufNum = bufnr("#") 364 365 if buflisted(l:alternateBufNum) 366 buffer # 367 else 368 bnext 369 endif 370 371 if bufnr("%") == l:currentBufNum 372 new 373 endif 374 375 if buflisted(l:currentBufNum) 376 execute("bdelete! ".l:currentBufNum) 377 endif 378 endfunction 379 " Append by wfyaibaib 380 " echo ">^.^<" 381 " source .vimrc 382 nnoremap <leader>sv :source $MYVIMRC<cr> 383 " edit .vimrc 384 nnoremap <leader>ev :vsplit $MYVIMRC<cr> 385 " use ,, change to mormal model 386 inoremap <leader>, <esc> 387 vnoremap <leader>, <esc> 388 " write and quit 389 nnoremap <leader>q :wq<cr> 390 " write 391 nnoremap <leader>w :w<cr> 392 " forget <esc> 393 inoremap <esc> <nop> 394 " open a line, nomorl mode 395 nnoremap <leader>o o<esc> 396 nnoremap <leader>O O<esc> 397 " comment lines 398 " Commenting blocks of code. 399 autocmd FileType c,cpp,java,scala let b:comment_leader = '// ' 400 autocmd FileType sh,ruby,python,perl let b:comment_leader = '# ' 401 autocmd FileType conf,fstab let b:comment_leader = '# ' 402 autocmd FileType tex let b:comment_leader = '% ' 403 autocmd FileType mail let b:comment_leader = '> ' 404 autocmd FileType vim let b:comment_leader = '" ' 405 noremap <silent> ,cc :<C-B>silent <C-E>s/^/<C-R>=escape(b:comment_leader,'\/')<CR>/<CR>:nohlsearch<CR> 406 noremap <silent> ,cu :<C-B>silent <C-E>s/^\V<C-R>=escape(b:comment_leader,'\/')<CR>//e<CR>:nohlsearch<CR> 407 408 " show reg 409 nnoremap <leader>r :reg<cr> 410 " gui 411 if has("gui_running") 412 if has("gui_gtk2") 413 set guifont=Inconsolata\ 12 414 elseif has("gui_win32") 415 set guifont=Consolas:h11:cANSI 416 endif 417 endif