最好用的vimrc(摘自网络)

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