1 " Vim main configuration file.
6 " Save 'runtimepath' in case it was changed by the system's configuration
9 let s:save_runtimepath = &runtimepath
11 " Reset all options (except 'term', 'lines' and 'columns'). This makes sure a
12 " system wide configuration file doesn't change default values.
14 " And restore it after all other options were reset.
16 let &runtimepath = s:save_runtimepath
17 unlet s:save_runtimepath
20 " Make sure Vim (and not Vi) settings are used.
23 " Load my scripts from ~/.vim (my scripts) and ~/.vim/runtime (checkout of Vim
25 set runtimepath-=~/.vim
26 set runtimepath^=~/.vim,~/.vim/runtime
28 " Don't store swap files in the same directory as the edited file.
30 " But store them in ~/.tmp if available.
33 " Disable modelines as they may cause security problems. Instead use
34 " securemodelines (Vim script #1876).
37 " Complete to longest common string (list:longest) and then complete all full
38 " matches after another (full). Thanks to pbrisbin
39 " (http://pbrisbin.com:8080/dotfiles/vimrc).
40 set wildmode=list:longest,full
42 " Increase history of executed commands (:).
45 " Increase number of possible undos.
48 " Use strong encryption if possible, also used for swap/undo files.
49 if exists('+cryptmethod')
50 set cryptmethod=blowfish
56 " Enable automatic file detection, plugin and indention support.
58 filetype off " necessary for pathogen to force a reload of ftplugins
59 filetype plugin indent on
62 " Use UTF-8 file encoding for all files. Automatically recognize latin1 in
64 set fileencodings=utf-8,latin1
66 " Wrap text after 78 characters.
69 " Set tabs to 4 spaces, use softtabs.
73 " When < and > is used indent/deindent to the next 'shiftwidth' boundary.
75 " Use the default value for real tabs.
78 " Enable auto indention.
81 " When joining lines only add one space after a sentence.
84 " Allow backspacing over autoindent and line breaks.
85 set backspace=indent,eol
87 " Start a comment when hitting enter after a commented line (r) and when using
88 " o or O around a commented line (o).
90 " Don't break a line if was already longer then 'textwidth' when insert mode
94 " Allow virtual editing (cursor can be positioned anywhere, even when there is
95 " no character) in visual block mode.
98 " Already display matches while typing the search command. This makes spotting
102 " Activate syntax folding.
104 set foldmethod=syntax
106 set foldlevel=99 " no closed folds at default, 'foldenable' would disable
107 " folding which is not what I want
110 " Only check for case if the searched word contains a capital character.
114 " Activate spell checking, use English as default. Don't use spell checking
116 if exists('+spell') && has('syntax') && !&diff
121 " Allow buffers with changes to be hidden. Very important for effective
122 " editing with multiple buffers.
128 " Use a dark background. Doesn't change the background color, only sets text
129 " colors for a dark terminal.
132 " Display line numbers.
134 " But use as little space as necessary for the numbers column. Thanks to James
135 " Vega (http://git.jamessan.com/?p=etc/vim.git;a=summary).
136 if exists('+numberwidth')
139 " Display the ruler with current line/file position. If 'statusline' is used
140 " then this only affects <C-G>.
142 " Display partial commands in the status line.
145 " Don't redraw screen when executing macros; increases speed. Thanks to James
146 " Vega (http://git.jamessan.com/?p=etc/vim.git;a=summary).
149 " Visualize the line the cursor is currently in.
150 if exists('+cursorline')
154 " Display tabs, trailing space, non breakable spaces and long lines (when
155 " wrapping is disabled).
157 set listchars=trail:-,extends:>
159 set listchars+=nbsp:!
163 " Always display the status line even if there is only one window.
168 set statusline+=%02n: " buffer number
169 set statusline+=%f\ " path to current file in buffer
170 set statusline+=%h " [help] if buffer is help file
171 set statusline+=%w " [Preview] if buffer is preview buffer
172 set statusline+=%m " [+] if buffer was modified,
173 " [-] if 'modifiable' is off
174 set statusline+=%r " [RO] if buffer is read only
177 set statusline+=%= " right align
178 set statusline+=0x%-8B\ " current character under cursor as hex
179 set statusline+=%-12.(%l,%c%V%)\ " line number (%l),
180 " column number (%c),
181 " virtual column number if different
183 set statusline+=%P " position in file in percent
187 " MAPPINGS (except for plugins, see PLUGIN SETTINGS below)
189 " Easy way to exit insert mode.
192 " Also for command mode, thanks to http://github.com/mitechie/pyvim
197 " Disable arrow keys for all modes except command modes. Thanks to James Vega
198 " (http://git.jamessan.com/?p=etc/vim.git;a=summary).
207 " Also disable arrow keys in command mode, use <C-P>/<C-N> as replacement (see
214 " Use Ctrl-P/Ctrl-N as replacement for <Up>/<Down> in command mode. Thanks to
215 " abstrakt and grayw in #vim on Freenode (2010-04-12 21:20 CEST).
217 cnoremap <C-N> <Down>
219 " Write before suspending, thanks to deryni in #vim on Freenode (2011-05-09
220 " 20:02 CEST). To suspend without saving either unmap this or use :stop<CR>.
221 nnoremap <silent> <C-Z> :update<CR>:stop<CR>
223 " 2<C-G> gives more verbose information, use it by default. Thanks to NCS_One
224 " in #vim on Freenode (2011-08-15 00:17 CEST).
225 nnoremap <C-G> 2<C-G>
227 " Use <Space> to move down a page and - to move up one like in mutt.
228 nnoremap <Space> <C-F>
231 " Go to next and previous buffer. Thanks to elik in #vim on Freenode
232 " (2010-05-16 18:38 CEST) for this idea.
233 nnoremap <silent> gb :bnext<CR>
234 nnoremap <silent> gB :bprev<CR>
236 " Fast access to buffers.
237 nnoremap <silent> <Leader>1 :1b<CR>
238 nnoremap <silent> <Leader>2 :2b<CR>
239 nnoremap <silent> <Leader>3 :3b<CR>
240 nnoremap <silent> <Leader>4 :4b<CR>
241 nnoremap <silent> <Leader>5 :5b<CR>
242 nnoremap <silent> <Leader>6 :6b<CR>
243 nnoremap <silent> <Leader>7 :7b<CR>
244 nnoremap <silent> <Leader>8 :8b<CR>
245 nnoremap <silent> <Leader>9 :9b<CR>
246 nnoremap <silent> <Leader>0 :10b<CR>
248 " Make last active window the only window. Similar to <C-W> o.
249 nnoremap <C-W>O <C-W>p<C-W>o
251 " Maps to change spell language between English and German and disable spell
254 noremap <silent> <Leader>sn :set nospell<CR>
255 noremap <silent> <Leader>se :set spell spelllang=en_us<CR>
256 noremap <silent> <Leader>sd :set spell spelllang=de_de<CR>
259 " Add semicolon to the end of the line. Thanks to
260 " http://www.van-laarhoven.org/vim/.vimrc for this idea and godlygeek in #vim
261 " on Freenode for an improved version which doesn't clobber any marks.
262 nnoremap <silent> <Leader>; :call setline(line('.'), getline('.') . ';')<CR>
264 " * and # for selections in visual mode. Thanks to
265 " http://got-ravings.blogspot.com/2008/07/vim-pr0n-visual-search-mappings.html
266 " and all nerds involved (godlygeek, strull in #vim on Freenode).
268 function! s:VSetSearch()
271 let @/ = '\V' . substitute(escape(@@, '\'), '\n', '\\n', 'g')
274 vnoremap * :<C-U>call <SID>VSetSearch()<CR>//<CR>
275 vnoremap # :<C-U>call <SID>VSetSearch()<CR>??<CR>
278 " I often type "W" instead of "w" when trying to save a file. Fix my mistake.
279 " Thanks to Tony Mechelynck <antoine.mechelynck@gmail.com> from the Vim
280 " mailing list for the commands.
288 \ ((getcmdtype() == ':' && getcmdpos() <= 2) ? 'w' : 'W')
289 cnoreabbrev <expr> Wa
290 \ ((getcmdtype() == ':' && getcmdpos() <= 3) ? 'wa' : 'Wa')
291 cnoreabbrev <expr> Wq
292 \ ((getcmdtype() == ':' && getcmdpos() <= 3) ? 'wq' : 'Wq')
293 cnoreabbrev <expr> Wqa
294 \ ((getcmdtype() == ':' && getcmdpos() <= 4) ? 'wqa' : 'Wqa')
296 " Also fix my typo with "Q".
302 \ ((getcmdtype() == ':' && getcmdpos() <= 2) ? 'q' : 'Q')
303 cnoreabbrev <expr> Qa
304 \ ((getcmdtype() == ':' && getcmdpos() <= 3) ? 'qa' : 'Qa')
307 " Make sure xa0 (alt + space) is automatically changed to a normal whitespace
308 " if pressed accidentally while in insert mode (happens on Mac when alt
309 " doesn't send escape). filereadable() is necessary for Leopard were 'mac' is
310 " no longer set on the console.
311 if has('mac') || filereadable('/Users/.localized')
312 inoremap <Char-0xa0> <Space>
315 " Disable Apple style movements in MacVim.
317 let g:macvim_skip_cmd_opt_movement = 1
320 " In case 'hlsearch' is used disable it with <C-L>. Thanks to frogonwheels and
321 " vimgor (bot) in #vim on Freenode (2010-03-30 05:58 CEST).
322 noremap <silent> <C-L> :nohlsearch<CR><C-L>
327 " Fix some of my spelling mistakes.
334 " Activate syntax coloring.
338 " Don't highlight more than 500 columns as I normally don't have that long
339 " lines and they slow down syntax coloring. Thanks to Derek Wyatt
340 " (http://www.derekwyatt.org/vim/the-vimrc-file/).
341 if exists('+synmaxcol')
345 " Highlight lines longer than 78 characters. Thanks to Tony Mechelynck
346 " <antoine.mechelynck@gmail.com> from the Vim mailing list. It can easily be
347 " disabled when necessary with :2match (in Vim >= 700).
349 2match Todo /\%>78v./
354 if exists('*matchadd')
355 " Highlight some important keywords in all documents.
356 for x in ['TODO', 'XXX', 'FIXME', 'CHANGED', 'REMOVED', 'DELETED']
357 call matchadd('Todo', x)
360 " Highlight unicode whitespace which is no normal whitespace (0x20).
361 for x in ['00a0', '1680', '180e', '2000', '2001', '2002', '2003',
362 \ '2004', '2005', '2006', '2007', '2008', '2009', '200a',
363 \ '200b', '200c', '200d', '202f', '205f', '2060', '3000',
365 call matchadd('Error', '\%u' . x)
369 " Settings for specific filetypes.
372 let g:hs_highlight_boolean = 1
373 let g:hs_highlight_types = 1
374 let g:hs_highlight_more_types = 1
378 let g:perl_fold_blocks = 1
379 let g:perl_nofold_packages = 1
380 let g:perl_include_pod = 1 " syntax coloring for PODs
387 " Use pathogen which allows one 'runtimepath' entry per plugin. This makes
388 " installing/removing/updating plugins simple. (Used for plugins with more
391 execute 'call pathogen#runtime_append_all_bundles()'
394 " Settings for the NERD commenter.
395 " Don't create any mappings I don't want to use.
396 let NERDCreateDefaultMappings = 0
397 " Map toggle comment.
398 map <Leader><Leader> <Plug>NERDCommenterToggle
400 " XPTemplate settings.
401 " Try to maintain snippet rendering even after editing outside of a
403 let g:xptemplate_strict = 0
404 " Don't complete any braces automatically.
405 let g:xptemplate_brace_complete = 0
406 " Only highlight the current placeholder.
407 let g:xptemplate_highlight = 'current'
413 " Use a custom auto group to prevent problems when the vimrc files is sourced
417 " Remove all autocmds from the current group.
420 " Go to last position of opened files. Taken from :help last-position-jump.
421 autocmd BufReadPost *
422 \ if line("'\"") > 1 && line("'\"") <= line("$") |
423 \ execute "normal! g'\"" |
425 " But not for Git commits, go to beginning of the file.
426 autocmd BufReadPost COMMIT_EDITMSG normal! gg
428 " Make sure 'list' and 'number' is disabled in help files. This is necessary
429 " when switching to a help buffer which is in the background with :buffer as
430 " these options are local to windows (and not only to buffers). This happens
431 " because I often want to use only one window and thus the help buffer is in
433 autocmd BufWinEnter *.txt
434 \ if &filetype == 'help' |
436 \ setlocal nonumber |
439 " Automatically disable 'paste' mode when leaving insert mode. Thanks to
440 " Raimondi in #vim on Freenode (2010-08-14 23:01 CEST). Very useful as I only
441 " want to paste once and then 'paste' gets automatically unset.
442 if exists('##InsertLeave')
443 autocmd InsertLeave * set nopaste
446 " Save changes when running :mak[e] before 'makeprg' is called.
447 autocmd QuickFixCmdPre * write
449 " AFTER/FTPLUGIN AUTO COMMANDS
451 " Disable spell checking for files which don't need it.
452 autocmd FileType deb setlocal nospell
453 autocmd FileType diff setlocal nospell
454 autocmd FileType tar setlocal nospell
455 " Fix to allow Vim edit crontab files as crontab doesn't work with
457 autocmd FileType crontab setlocal backupcopy=yes
458 " Don't use the modeline in git commits as the diff created by `git commit -v`
459 " may contain one which could change the filetype or other settings of the
460 " commit buffer. Also make sure we use only 72 characters per line which is
461 " the recommendation for git commit messages (http://tpope.net/node/106).
462 autocmd FileType gitcommit let g:secure_modelines_allowed_items = [] |
463 \ setlocal textwidth=72
464 " Use the same comment string as for Vim files in Vimperator files.
465 autocmd FileType vimperator setlocal commentstring=\"%s
466 " Use tex compiler for (La)TeX files.
467 autocmd FileType tex compiler tex
469 " FTDETECT AUTO COMMANDS
471 " Recognize .md as markdown files (Vim default is .mkd).
472 autocmd BufRead,BufNewFile *.md set filetype=mkd
473 " Recognize .test as Tcl files.
474 autocmd BufRead,BufNewFile *.test set filetype=tcl
476 " OTHER AUTO COMMANDS
478 " Disable spell checking, displaying of list characters and long lines when
479 " viewing documentation.
480 autocmd BufReadPost /usr/share/doc/* setlocal nospell nolist | 2match
482 " Use diff filetype for mercurial patches in patch queue.
483 autocmd BufReadPost */.hg/patches/* set filetype=diff
492 " New text-objects ii and ai to work on text with the same indentation.
494 " http://vim.wikia.com/index.php?title=Indent_text_object&oldid=27126
495 " (visited on 2011-11-19).
496 onoremap <silent> ai :<C-U>call <SID>IndTxtObj(0)<CR>
497 onoremap <silent> ii :<C-U>call <SID>IndTxtObj(1)<CR>
498 vnoremap <silent> ai :<C-U>call <SID>IndTxtObj(0)<CR><Esc>gv
499 vnoremap <silent> ii :<C-U>call <SID>IndTxtObj(1)<CR><Esc>gv
501 function! s:IndTxtObj(inner)
502 let curline = line(".")
503 let lastline = line("$")
504 let i = indent(line(".")) - &shiftwidth * (v:count1 - 1)
505 let i = i < 0 ? 0 : i
506 if getline(".") !~ "^\\s*$"
507 let p = line(".") - 1
508 let nextblank = getline(p) =~ "^\\s*$"
510 \ && ((i == 0 && !nextblank)
511 \ || (i > 0 && ((indent(p) >= i
512 \ && !(nextblank && a:inner))
513 \ || (nextblank && !a:inner))))
515 let p = line(".") - 1
516 let nextblank = getline(p) =~ "^\\s*$"
519 call cursor(curline, 0)
520 let p = line(".") + 1
521 let nextblank = getline(p) =~ "^\\s*$"
523 \ && ((i == 0 && !nextblank)
524 \ || (i > 0 && ((indent(p) >= i
525 \ && !(nextblank && a:inner))
526 \ || (nextblank && !a:inner))))
528 let p = line(".") + 1
529 let nextblank = getline(p) =~ "^\\s*$"