]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - vimrc
vimrc: Allow running as root.
[config/dotfiles.git] / vimrc
1 " Vim main configuration file.
2
3
4 " EDITOR SETTINGS
5
6 " Save 'runtimepath' in case it was changed by the system's configuration
7 " files.
8 if has('eval')
9     let s:save_runtimepath = &runtimepath
10 endif
11 " Reset all options (except 'term', 'lines' and 'columns'). This makes sure a
12 " system wide configuration file doesn't change default values.
13 set all&
14 " And restore it after all other options were reset.
15 if has('eval')
16     let &runtimepath = s:save_runtimepath
17     unlet s:save_runtimepath
18 endif
19
20 " Make sure Vim (and not Vi) settings are used.
21 set nocompatible
22
23 " Load my scripts from ~/.vim (my scripts) and ~/.vim/runtime (checkout of Vim
24 " runtime files).
25 set runtimepath-=~/.vim
26 set runtimepath^=~/.vim,~/.vim/runtime
27
28 " Don't store swap files in the same directory as the edited file.
29 set directory-=.
30 " But store them in ~/.tmp if available.
31 set directory^=~/.tmp
32
33 " Disable modelines as they may cause security problems. Instead use
34 " securemodelines (Vim script #1876).
35 set nomodeline
36
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
41
42 " Increase history of executed commands (:).
43 set history=1000
44
45 " Increase number of possible undos.
46 set undolevels=1000
47
48 " Use strong encryption if possible, also used for swap/undo files.
49 if exists('+cryptmethod')
50     set cryptmethod=blowfish
51 endif
52
53
54 " EDIT SETTINGS
55
56 " Enable automatic file detection, plugin and indention support.
57 if has('autocmd')
58     filetype off " necessary for pathogen to force a reload of ftplugins
59     filetype plugin indent on
60 endif
61
62 " Use UTF-8 file encoding for all files. Automatically recognize latin1 in
63 " existing files.
64 set fileencodings=utf-8,latin1
65
66 " Wrap text after 78 characters.
67 set textwidth=78
68
69 " Set tabs to 4 spaces, use softtabs.
70 set shiftwidth=4
71 set softtabstop=4
72 set expandtab
73 " When < and > is used indent/deindent to the next 'shiftwidth' boundary.
74 set shiftround
75 " Use the default value for real tabs.
76 set tabstop=8
77
78 " Enable auto indention.
79 set autoindent
80
81 " When joining lines only add one space after a sentence.
82 set nojoinspaces
83
84 " Allow backspacing over autoindent and line breaks.
85 set backspace=indent,eol
86
87 " Start a comment when hitting enter after a commented line (r) and when using
88 " o or O around a commented line (o).
89 set formatoptions+=ro
90 " Don't break a line if was already longer then 'textwidth' when insert mode
91 " started.
92 set formatoptions+=l
93
94 " Allow virtual editing (cursor can be positioned anywhere, even when there is
95 " no character) in visual block mode.
96 set virtualedit=block
97
98 " Already display matches while typing the search command. This makes spotting
99 " errors easy.
100 set incsearch
101
102 " Activate syntax folding.
103 if has('folding')
104     set foldmethod=syntax
105     set foldcolumn=2
106     set foldlevel=99 " no closed folds at default, 'foldenable' would disable
107                      " folding which is not what I want
108 endif
109
110 " Only check for case if the searched word contains a capital character.
111 set ignorecase
112 set smartcase
113
114 " Activate spell checking, use English as default. Don't use spell checking
115 " when diffing.
116 if exists('+spell') && has('syntax') && !&diff
117     set spell
118     set spelllang=en_us
119 endif
120
121 " Allow buffers with changes to be hidden. Very important for effective
122 " editing with multiple buffers.
123 set hidden
124
125
126 " DISPLAY SETTINGS
127
128 " Use a dark background. Doesn't change the background color, only sets text
129 " colors for a dark terminal.
130 set background=dark
131
132 " Display line numbers.
133 set number
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')
137     set numberwidth=1
138 endif
139 " Display the ruler with current line/file position. If 'statusline' is used
140 " then this only affects <C-G>.
141 set ruler
142 " Display partial commands in the status line.
143 set showcmd
144
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).
147 set lazyredraw
148
149 " Visualize the line the cursor is currently in.
150 if exists('+cursorline')
151     set cursorline
152 endif
153
154 " Display tabs, trailing space, non breakable spaces and long lines (when
155 " wrapping is disabled).
156 set list
157 set listchars=trail:-,extends:>
158 if v:version >= 700
159     set listchars+=nbsp:!
160 endif
161
162 if has('statusline')
163     " Always display the status line even if there is only one window.
164     set laststatus=2
165
166     set statusline=
167     " on the left
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
175
176     " on the right
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
182                                       "                       than %c (%V)
183     set statusline+=%P                " position in file in percent
184 endif
185
186
187 " MAPPINGS (except for plugins, see PLUGIN SETTINGS below)
188
189 " Easy way to exit insert mode.
190 inoremap jj <Esc>
191 inoremap jk <Esc>
192 " Also for command mode, thanks to http://github.com/mitechie/pyvim
193 " (2010-10-15).
194 cnoremap jj <C-C>
195 cnoremap jk <C-C>
196
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).
199 map <Right>  <Nop>
200 map <Left>   <Nop>
201 map <Up>     <Nop>
202 map <Down>   <Nop>
203 imap <Right> <Nop>
204 imap <Left>  <Nop>
205 imap <Up>    <Nop>
206 imap <Down>  <Nop>
207 " Also disable arrow keys in command mode, use <C-P>/<C-N> as replacement (see
208 " below).
209 cmap <Up>    <Nop>
210 cmap <Down>  <Nop>
211 cmap <Right> <Nop>
212 cmap <Left>  <Nop>
213
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).
216 cnoremap <C-P> <Up>
217 cnoremap <C-N> <Down>
218
219 " Write before suspending, thanks to deryni in #vim on Freenode (2011-05-09
220 " 20:02 CEST). To suspend without saving either unmap it or use :stop<CR>.
221 nnoremap <C-Z> :update<CR>:stop<CR>
222
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>
226
227 " Use <Space> to move down a page and - to move up one like in mutt.
228 nnoremap <Space> <C-F>
229 nnoremap - <C-B>
230
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>
235
236 " Fast access to buffers.
237 nnoremap <Leader>1 :1b<CR>
238 nnoremap <Leader>2 :2b<CR>
239 nnoremap <Leader>3 :3b<CR>
240 nnoremap <Leader>4 :4b<CR>
241 nnoremap <Leader>5 :5b<CR>
242 nnoremap <Leader>6 :6b<CR>
243 nnoremap <Leader>7 :7b<CR>
244 nnoremap <Leader>8 :8b<CR>
245 nnoremap <Leader>9 :9b<CR>
246 nnoremap <Leader>0 :10b<CR>
247
248 " Make last active window the only window. Similar to <C-w> o.
249 nnoremap <C-W>O <C-W>p<C-W>o
250
251 " Maps to change spell language between English and German and disable spell
252 " checking.
253 if exists('+spell')
254     noremap <Leader>sn :set nospell<CR>
255     noremap <Leader>se :set spell spelllang=en_us<CR>
256     noremap <Leader>sd :set spell spelllang=de_de<CR>
257 endif
258
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>
263
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).
267 if has('eval')
268     function! s:VSetSearch()
269         let temp = @@
270         normal! gvy
271         let @/ = '\V' . substitute(escape(@@, '\'), '\n', '\\n', 'g')
272         let @@ = temp
273     endfunction
274     vnoremap * :<C-U>call <SID>VSetSearch()<CR>//<CR>
275     vnoremap # :<C-U>call <SID>VSetSearch()<CR>??<CR>
276 endif
277
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.
281 if v:version < 700
282     cnoreabbrev W w
283     cnoreabbrev Wa wa
284     cnoreabbrev Wq wq
285     cnoreabbrev Wqa wqa
286 else
287     cnoreabbrev <expr> W
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')
295 endif
296 " Also fix my typo with "Q".
297 if v:version < 700
298     cnoreabbrev Q q
299     cnoreabbrev Qa qa
300 else
301     cnoreabbrev <expr> Q
302         \ ((getcmdtype() == ':' && getcmdpos() <= 2) ? 'q' : 'Q')
303     cnoreabbrev <expr> Qa
304         \ ((getcmdtype() == ':' && getcmdpos() <= 3) ? 'qa' : 'Qa')
305 endif
306
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>
313 endif
314
315 " Disable Apple style movements in MacVim.
316 if has('gui_macvim')
317     let g:macvim_skip_cmd_opt_movement = 1
318 endif
319
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>
323
324
325 " ABBREVIATIONS
326
327 " Fix some of my spelling mistakes.
328 iabbrev relle reelle
329 iabbrev reele reelle
330
331
332 " SYNTAX SETTINGS
333
334 " Activate syntax coloring.
335 if has('syntax')
336     syntax enable
337
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')
342         set synmaxcol=500
343     endif
344
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).
348     if exists(':2match')
349         2match Todo /\%>78v./
350     else
351         match Todo /\%>78v./
352     endif
353
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)
358         endfor
359
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',
364                 \ 'feff']
365             call matchadd('Error', '\%u' . x)
366         endfor
367     endif
368
369 " Settings for specific filetypes.
370
371     " Haskell.
372     let g:hs_highlight_boolean = 1
373     let g:hs_highlight_types = 1
374     let g:hs_highlight_more_types = 1
375
376     " Perl.
377     let g:perl_fold = 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
381 endif
382
383
384 " PLUGIN SETTINGS
385
386 " Use pathogen which allows one 'runtimepath' entry per plugin. This makes
387 " installing/removing/updating plugins simple. (Used for plugins with more
388 " than one file.)
389 if v:version >= 700
390     execute 'call pathogen#runtime_append_all_bundles()'
391 endif
392
393 " Settings for the NERD commenter.
394 " Don't create any mappings I don't want to use.
395 if has('eval')
396     let g:NERDCreateDefaultMappings = 0
397 endif
398 " Map toggle comment.
399 map <Leader><Leader> <Plug>NERDCommenterToggle
400
401 " XPTemplate settings.
402 if has('eval')
403     " Try to maintain snippet rendering even after editing outside of a
404     " snippet.
405     let g:xptemplate_strict = 0
406     " Don't complete any braces automatically.
407     let g:xptemplate_brace_complete = 0
408     " Only highlight the current placeholder.
409     let g:xptemplate_highlight = 'current'
410 endif
411
412
413 " AUTO COMMANDS
414
415 " Use a custom auto group to prevent problems when the vimrc files is sourced
416 " twice.
417 if has('autocmd')
418     augroup vimrc
419 " Remove all autocmds from the current group.
420         autocmd!
421
422 " Go to last position of opened files. Taken from :help last-position-jump.
423         autocmd BufReadPost *
424             \ if line("'\"") > 1 && line("'\"") <= line("$") |
425             \     execute "normal! g'\"" |
426             \ endif
427 " But not for Git commits, go to beginning of the file.
428         autocmd BufReadPost COMMIT_EDITMSG normal! gg
429
430 " Make sure 'list' and 'number' is disabled in help files. This is necessary
431 " when switching to a help buffer which is in the background with :buffer as
432 " these options are local to windows (and not only to buffers). This happens
433 " because I often want to use only one window and thus the help buffer is in
434 " the background.
435         autocmd BufWinEnter *.txt
436             \ if &filetype == 'help' |
437             \     setlocal nolist |
438             \     setlocal nonumber |
439             \ endif
440
441 " Automatically disable 'paste' mode when leaving insert mode. Thanks to
442 " Raimondi in #vim on Freenode (2010-08-14 23:01 CEST). Very useful as I only
443 " want to paste once and then 'paste' gets automatically unset.
444         if exists('##InsertLeave')
445             autocmd InsertLeave * set nopaste
446         endif
447
448 " Save changes when running :mak[e] before 'makeprg' is called.
449         autocmd QuickFixCmdPre * write
450
451 " AFTER/FTPLUGIN AUTO COMMANDS
452
453 " Disable spell checking for files which don't need it.
454         autocmd FileType deb  setlocal nospell
455         autocmd FileType diff setlocal nospell
456         autocmd FileType tar  setlocal nospell
457 " Fix to allow Vim edit crontab files as crontab doesn't work with
458 " backupcopy=auto.
459         autocmd FileType crontab setlocal backupcopy=yes
460 " Don't use the modeline in git commits as the diff created by `git commit -v`
461 " may contain one which could change the filetype or other settings of the
462 " commit buffer. Also make sure we use only 72 characters per line which is
463 " the recommendation for git commit messages (http://tpope.net/node/106).
464         autocmd FileType gitcommit let g:secure_modelines_allowed_items = [] |
465                                  \ setlocal textwidth=72
466 " Use the same comment string as for Vim files in Vimperator files.
467         autocmd FileType vimperator setlocal commentstring=\"%s
468 " Use tex compiler for (La)TeX files.
469         autocmd FileType tex compiler tex
470
471 " FTDETECT AUTO COMMANDS
472
473 " Recognize .md as markdown files (Vim default is .mkd).
474         autocmd BufRead,BufNewFile *.md set filetype=mkd
475 " Recognize .test as Tcl files.
476         autocmd BufRead,BufNewFile *.test set filetype=tcl
477
478 " OTHER AUTO COMMANDS
479
480 " Disable spell checking, displaying of list characters and long lines when
481 " viewing documentation.
482         autocmd BufReadPost /usr/share/doc/* setlocal nospell nolist | 2match
483
484 " Use diff filetype for mercurial patches in patch queue.
485         autocmd BufReadPost */.hg/patches/* set filetype=diff
486
487     augroup END
488 endif
489
490
491 " CUSTOM FUNCTIONS
492
493 if has('eval')
494     " New text-objects ii and ai to work on text with the same indentation.
495     " Thanks to
496     " http://vim.wikia.com/index.php?title=Indent_text_object&oldid=27126
497     " (visited on 2011-11-19).
498     onoremap <silent>ai :<C-U>call <SID>IndTxtObj(0)<CR>
499     onoremap <silent>ii :<C-U>call <SID>IndTxtObj(1)<CR>
500     vnoremap <silent>ai :<C-U>call <SID>IndTxtObj(0)<CR><Esc>gv
501     vnoremap <silent>ii :<C-U>call <SID>IndTxtObj(1)<CR><Esc>gv
502
503     function! s:IndTxtObj(inner)
504         let curline = line(".")
505         let lastline = line("$")
506         let i = indent(line(".")) - &shiftwidth * (v:count1 - 1)
507         let i = i < 0 ? 0 : i
508         if getline(".") !~ "^\\s*$"
509             let p = line(".") - 1
510             let nextblank = getline(p) =~ "^\\s*$"
511             while p > 0
512                     \ && ((i == 0 && !nextblank)
513                         \ || (i > 0 && ((indent(p) >= i
514                             \ && !(nextblank && a:inner))
515                             \ || (nextblank && !a:inner))))
516                 -
517                 let p = line(".") - 1
518                 let nextblank = getline(p) =~ "^\\s*$"
519             endwhile
520             normal! 0V
521             call cursor(curline, 0)
522             let p = line(".") + 1
523             let nextblank = getline(p) =~ "^\\s*$"
524             while p <= lastline
525                     \ && ((i == 0 && !nextblank)
526                         \ || (i > 0 && ((indent(p) >= i
527                             \ && !(nextblank && a:inner))
528                             \ || (nextblank && !a:inner))))
529                 +
530                 let p = line(".") + 1
531                 let nextblank = getline(p) =~ "^\\s*$"
532             endwhile
533             normal! $
534         endif
535     endfunction
536
537 endif