]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - vimrc
b4e44dcb487bd3f6017a9f289189abb6c5359f4b
[config/dotfiles.git] / vimrc
1 " Vim main configuration file.
2
3
4 " EDITOR SETTINGS
5
6 " Prevent editing as root as it may cause security problems. Use sudoedit
7 " instead. Thanks to godlygeek in #vim on Freenode (2009-06-19 22:21 CEST).
8 if $HOME == '/root' || exists('$SUDO_USER')
9     echomsg 'Running as root is forbidden! Use sudoedit.'
10     qall
11 endif
12
13
14 " Save 'runtimepath' in case it was changed by the system's configuration
15 " files.
16 if has('eval')
17     let s:save_runtimepath = &runtimepath
18 endif
19 " Reset all options (except 'term', 'lines' and 'columns'). This makes sure a
20 " system wide configuration file doesn't change default values.
21 set all&
22 " And restore it after all other options were reset.
23 if has('eval')
24     let &runtimepath = s:save_runtimepath
25 endif
26
27 " Make sure Vim (and not Vi) settings are used.
28 set nocompatible
29
30 " Load my scripts from ~/.vim (my scripts) and ~/.vim/runtime (checkout of Vim
31 " runtime files).
32 set runtimepath-=~/.vim
33 set runtimepath^=~/.vim,~/.vim/runtime
34
35 " Don't store swap files in the same directory as the edited file.
36 set directory-=.
37
38 " Disable modelines as they may cause security problems. Instead use
39 " securemodelines (Vim script #1876).
40 set nomodeline
41
42 " Complete to longest common string (list:longest) and then complete all full
43 " matches after another (full). Thanks to pbrisbin
44 " (http://pbrisbin.com:8080/dotfiles/vimrc).
45 set wildmode=list:longest,full
46
47 " Increase history of executed commands (:).
48 set history=1000
49
50 " Increase number of possible undos.
51 set undolevels=1000
52
53 " Use strong encryption if possible, also used for swap/undo files.
54 if v:version >= 703
55     set cryptmethod=blowfish
56 endif
57
58
59 " EDIT SETTINGS
60
61 " Enable automatic file detection, plugin and indention support.
62 if has('autocmd')
63     filetype plugin indent on
64 endif
65
66 " Use UTF-8 file encoding for all files. Automatically recognize latin1 in
67 " existing files.
68 set fileencodings=utf-8,latin1
69
70 " Wrap text after 78 characters.
71 set textwidth=78
72
73 " Set tabs to 4 spaces, use softtabs.
74 set shiftwidth=4
75 set softtabstop=4
76 set expandtab
77 " When < and > is used indent/deindent to the next 'shiftwidth' boundary.
78 set shiftround
79 " Use the default value for real tabs.
80 set tabstop=8
81
82 " Enable auto indention.
83 set autoindent
84
85 " When joining lines only add one space after a sentence.
86 set nojoinspaces
87
88 " Allow backspacing over autoindent and line breaks.
89 set backspace=indent,eol
90
91 " Start a comment when hitting enter after a commented line (r) and when using
92 " o or O around a commented line (o).
93 set formatoptions+=ro
94 " Don't break a line if was already longer then 'textwidth' when insert mode
95 " started.
96 set formatoptions+=l
97
98 " Allow virtual editing (cursor can be positioned anywhere, even when there is
99 " no character) in visual block mode.
100 set virtualedit=block
101
102 " Already display matches while typing the search command. This makes spotting
103 " errors easy.
104 set incsearch
105
106 " Activate syntax folding.
107 if has('folding')
108     set foldmethod=syntax
109     set foldcolumn=2
110     set foldlevel=99 " no closed folds at default, 'foldenable' would disable
111                      " folding which is not what I want
112 endif
113
114 " Only check for case if the searched word contains a capital character.
115 set ignorecase
116 set smartcase
117
118 " Activate spell checking, use English as default. Don't use spell checking
119 " when diffing.
120 if v:version >= 700 && has('syntax') && !&diff
121     set spell
122     set spelllang=en_us
123 endif
124
125 " Allow buffers with changes to be hidden. Very important for effective
126 " editing with multiple buffers.
127 set hidden
128
129
130 " DISPLAY SETTINGS
131
132 " Use a dark background. Doesn't change the background color, only sets text
133 " colors for a dark terminal.
134 set background=dark
135
136 " Display line numbers.
137 set number
138 " But use as little space as necessary for the numbers column. Thanks to James
139 " Vega (http://git.jamessan.com/?p=etc/vim.git;a=summary).
140 if v:version >= 700
141     set numberwidth=1
142 endif
143 " Display the ruler with current line/file position. If 'statusline' is used
144 " then this only affects <C-G>.
145 set ruler
146 " Display partial commands in the status line.
147 set showcmd
148
149 " Don't redraw screen when executing macros; increases speed. Thanks to James
150 " Vega (http://git.jamessan.com/?p=etc/vim.git;a=summary).
151 set lazyredraw
152
153 " Visualize the line the cursor is currently in.
154 if v:version >= 700
155     set cursorline
156 endif
157
158 " Display tabs, trailing space, non breakable spaces and long lines (when
159 " wrapping is disabled).
160 set list
161 set listchars=trail:-,extends:>
162 if v:version >= 700
163     set listchars+=nbsp:!
164 endif
165
166 if has('statusline')
167     " Always display the status line even if there is only one window.
168     set laststatus=2
169
170     set statusline=
171     " on the left
172     set statusline+=%02n: " buffer number
173     set statusline+=%f\   " path to current file in buffer
174     set statusline+=%h    " [help] if buffer is help file
175     set statusline+=%w    " [Preview] if buffer is preview buffer
176     set statusline+=%m    " [+] if buffer was modified,
177                           " [-] if 'modifiable' is off
178     set statusline+=%r    " [RO] if buffer is read only
179
180     " on the right
181     set statusline+=%=                " right align
182     set statusline+=0x%-8B\           " current character under cursor as hex
183     set statusline+=%-12.(%l,%c%V%)\  " line number (%l),
184                                       " column number (%c),
185                                       " virtual column number if different
186                                       "                       than %c (%V)
187     set statusline+=%P                " position in file in percent
188 endif
189
190
191 " MAPPINGS (except for plugins, see PLUGIN SETTINGS below)
192
193 " Easy way to exit insert mode.
194 inoremap jj <Esc>
195 inoremap jk <Esc>
196 " Also for command mode, thanks to http://github.com/mitechie/pyvim
197 " (2010-10-15).
198 cnoremap jj <C-C>
199 cnoremap jk <C-C>
200
201 " Disable arrow keys for all modes except command modes. Thanks to James Vega
202 " (http://git.jamessan.com/?p=etc/vim.git;a=summary).
203 map <Right>  <Nop>
204 map <Left>   <Nop>
205 map <Up>     <Nop>
206 map <Down>   <Nop>
207 imap <Right> <Nop>
208 imap <Left>  <Nop>
209 imap <Up>    <Nop>
210 imap <Down>  <Nop>
211 " Also disable arrow keys in command mode, use <C-P>/<C-N> as replacement (see
212 " below).
213 cmap <Up>    <Nop>
214 cmap <Down>  <Nop>
215 cmap <Right> <Nop>
216 cmap <Left>  <Nop>
217
218 " Use Ctrl-P/Ctrl-N as replacement for <Up>/<Down> in command mode. Thanks to
219 " abstrakt and grayw in #vim on Freenode (2010-04-12 21:20 CEST).
220 cnoremap <C-P> <Up>
221 cnoremap <C-N> <Down>
222
223 " Use <Space> to move down a page and - to move up one like in mutt.
224 nnoremap <Space> <C-F>
225 nnoremap - <C-B>
226
227 " Behave like 'scrolloff' but only while searching. Thanks to "Benjamin R.
228 " Haskell" <vim@benizi.com> from the Vim mailing list (2010-10-26).
229 nnoremap n nzv3j3k
230 nnoremap N Nzv3k3j
231
232 " Go to next and previous buffer. Thanks to elik in #vim on Freenode
233 " (2010-05-16 18:38 CEST) for this idea.
234 nnoremap <silent> gb :bnext<CR>
235 nnoremap <silent> gB :bprev<CR>
236
237 " Fast access to buffers.
238 nnoremap <Leader>1 :1b<CR>
239 nnoremap <Leader>2 :2b<CR>
240 nnoremap <Leader>3 :3b<CR>
241 nnoremap <Leader>4 :4b<CR>
242 nnoremap <Leader>5 :5b<CR>
243 nnoremap <Leader>6 :6b<CR>
244 nnoremap <Leader>7 :7b<CR>
245 nnoremap <Leader>8 :8b<CR>
246 nnoremap <Leader>9 :9b<CR>
247 nnoremap <Leader>0 :10b<CR>
248
249 " Make last active window the only window. Similar to <C-w> o.
250 nnoremap <C-W>O <C-W>p<C-W>o
251
252 " Maps to change spell language between English and German and disable spell
253 " checking.
254 if v:version >= 700
255     noremap <Leader>sn :set nospell<CR>
256     noremap <Leader>se :set spell spelllang=en_us<CR>
257     noremap <Leader>sd :set spell spelllang=de_de<CR>
258 endif
259
260 " Add semicolon to the end of the line. Thanks to
261 " http://www.van-laarhoven.org/vim/.vimrc for this idea and godlygeek in #vim
262 " on Freenode for an improved version which doesn't clobber any marks.
263 nnoremap <silent> <Leader>; :call setline(line('.'), getline('.') . ';')<CR>
264
265 " * and # for selections in visual mode. Thanks to
266 " http://got-ravings.blogspot.com/2008/07/vim-pr0n-visual-search-mappings.html
267 " and all nerds involved (godlygeek, strull in #vim on Freenode).
268 if has('eval')
269     function! s:VSetSearch()
270         let temp = @@
271         normal! gvy
272         let @/ = '\V' . substitute(escape(@@, '\'), '\n', '\\n', 'g')
273         let @@ = temp
274     endfunction
275     vnoremap * :<C-U>call <SID>VSetSearch()<CR>//<CR>
276     vnoremap # :<C-U>call <SID>VSetSearch()<CR>??<CR>
277 endif
278
279 " I often type "W" instead of "w" when trying to save a file. Fix my mistake.
280 " Thanks to Tony Mechelynck <antoine.mechelynck@gmail.com> from the Vim
281 " mailing list for the commands.
282 if v:version < 700
283     cnoreabbrev W w
284     cnoreabbrev Wa wa
285     cnoreabbrev Wq wq
286     cnoreabbrev Wqa wqa
287 else
288     cnoreabbrev <expr> W
289         \ ((getcmdtype() == ':' && getcmdpos() <= 2) ? 'w' : 'W')
290     cnoreabbrev <expr> Wa
291         \ ((getcmdtype() == ':' && getcmdpos() <= 3) ? 'wa' : 'Wa')
292     cnoreabbrev <expr> Wq
293         \ ((getcmdtype() == ':' && getcmdpos() <= 3) ? 'wq' : 'Wq')
294     cnoreabbrev <expr> Wqa
295         \ ((getcmdtype() == ':' && getcmdpos() <= 4) ? 'wqa' : 'Wqa')
296 endif
297 " Also fix my typo with "Q".
298 if v:version < 700
299     cnoreabbrev Q q
300     cnoreabbrev Qa qa
301 else
302     cnoreabbrev <expr> Q
303         \ ((getcmdtype() == ':' && getcmdpos() <= 2) ? 'q' : 'Q')
304     cnoreabbrev <expr> Qa
305         \ ((getcmdtype() == ':' && getcmdpos() <= 3) ? 'qa' : 'Qa')
306 endif
307
308 " Make sure xa0 (alt + space) is automatically changed to a normal whitespace
309 " if pressed accidentally while in insert mode (happens on Mac when alt
310 " doesn't send escape). filereadable() is necessary for Leopard were 'mac' is
311 " no longer set on the console.
312 if has('mac') || filereadable('/Users/.localized')
313     inoremap <Char-0xa0> <Space>
314 endif
315
316 " Disable Apple style movements in MacVim.
317 if has('gui_macvim') && has('eval')
318     let macvim_skip_cmd_opt_movement = 1
319 endif
320
321 " In case 'hlsearch' is used disable it with <C-L>. Thanks to frogonwheels and
322 " vimgor (bot) in #vim on Freenode (2010-03-30 05:58 CEST).
323 noremap <silent> <C-L> :nohlsearch<CR><C-L>
324
325
326 " ABBREVIATIONS
327
328 " Fix some of my spelling mistakes.
329 iabbrev relle reelle
330 iabbrev reele reelle
331
332
333 " SYNTAX SETTINGS
334
335 " Activate syntax coloring.
336 if has('syntax')
337     syntax enable
338
339 " Don't highlight more than 500 columns as I normally don't have that long
340 " lines and they slow down syntax coloring. Thanks to Derek Wyatt
341 " (http://www.derekwyatt.org/vim/the-vimrc-file/).
342     set synmaxcol=500
343
344 " Highlight lines longer than 78 characters. Thanks to Tony Mechelynck
345 " <antoine.mechelynck@gmail.com> from the Vim mailing list. It can easily be
346 " disabled when necessary with :2match (in Vim >= 700).
347     if v:version >= 700
348         2match Todo /\%>78v./
349     else
350         match Todo /\%>78v./
351     endif
352
353 " Highlight TODO, FIXME, CHANGED and XXX in all documents.
354     if v:version > 701 || (v:version == 701 && has('patch42'))
355         call matchadd('Todo', '\(TODO\|FIXME\|CHANGED\|XXX\)')
356     endif
357 endif
358
359
360 " PLUGIN SETTINGS
361
362 " Settings for the NERD commenter.
363 " Don't create any mappings I don't want to use.
364 if has('eval')
365     let NERDCreateDefaultMappings = 0
366 endif
367 " Map toggle comment.
368 map <Leader><Leader> <Plug>NERDCommenterToggle
369
370 " XPTemplate settings.
371 if has('eval')
372     " Try to maintain snippet rendering even after editing outside of a
373     " snippet.
374     let g:xptemplate_strict = 0
375     " Don't complete any braces automatically.
376     let g:xptemplate_brace_complete = 0
377     " Only highlight the current placeholder.
378     let g:xptemplate_highlight = 'current'
379 endif
380
381
382 " AUTO COMMANDS
383
384 " Use a custom auto group to prevent problems when the vimrc files is sourced
385 " twice.
386 if has('autocmd')
387     augroup vimrc
388         autocmd!
389
390 " Go to last position of opened files. Taken from :help last-position-jump.
391         autocmd BufReadPost *
392             \ if line("'\"") > 1 && line("'\"") <= line("$") |
393             \     execute "normal! g'\"" |
394             \ endif
395 " But not for Git commits, go to beginning of the file.
396         autocmd BufReadPost COMMIT_EDITMSG normal! gg
397
398 " Make sure 'list' and 'number' is disabled in help files. This is necessary
399 " when switching to a help buffer which is in the background with :buffer as
400 " these options are local to windows (and not only to buffers). This happens
401 " because I often want to use only one window and thus the help buffer is in
402 " the background.
403         autocmd BufWinEnter *.txt
404             \ if &filetype == 'help' |
405             \     setlocal nolist |
406             \     setlocal nonumber |
407             \ endif
408
409 " Automatically disable 'paste' mode when leaving insert mode. Thanks to
410 " Raimondi in #vim on Freenode (2010-08-14 23:01 CEST). Very useful as I only
411 " want to paste once and then 'paste' gets automatically unset.
412         autocmd InsertLeave * set nopaste
413
414 " AFTER/FTPLUGIN AUTO COMMANDS
415
416 " Disable spell checking for files which don't need it.
417         autocmd FileType deb  setlocal nospell
418         autocmd FileType diff setlocal nospell
419         autocmd FileType tar  setlocal nospell
420 " Fix to allow Vim edit crontab files as crontab doesn't work with
421 " backupcopy=auto.
422         autocmd FileType crontab setlocal backupcopy=yes
423 " Don't use the modeline in git commits as the diff created by `git commit -v`
424 " may contain one which could change the filetype or other settings of the
425 " commit buffer. Also make sure we use only 72 characters per line which is
426 " the recommendation for git commit messages (http://tpope.net/node/106).
427         autocmd FileType gitcommit let g:secure_modelines_allowed_items = [] |
428                                  \ setlocal textwidth=72
429 " Allow folding in perl.
430         autocmd FileType perl let perl_fold = 1 |
431                             \ let perl_fold_blocks = 1
432 " Use the same comment string as for Vim files in Vimperator files.
433         autocmd FileType vimperator setlocal commentstring=\"%s
434
435 " FTDETECT AUTO COMMANDS
436
437 " Recognize .md as markdown files (Vim default is .mkd).
438         autocmd BufRead,BufNewFile *.md set filetype=mkd
439 " Recognize .test as Tcl files.
440         autocmd BufRead,BufNewFile *.test set filetype=tcl
441
442 " OTHER AUTO COMMANDS
443
444 " Disable spell checking, displaying of list characters and long lines when
445 " viewing documentation.
446         autocmd BufReadPost /usr/share/doc/* setlocal nospell nolist | 2match
447
448 " Use diff filetype for mercurial patches in patch queue.
449         autocmd BufReadPost */.hg/patches/* set filetype=diff
450
451     augroup END
452 endif