]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - vimrc
vimrc: Don't reset 'runtimepath'.
[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).
8 if $HOME == '/root' || exists('$SUDO_USER')
9     echomsg 'Running as root is forbidden! Use sudoedit.'
10     qa
11 endif
12
13
14 " Save 'runtimepath' in case it was changed by the system's configuration
15 " files.
16 let s:save_runtimepath = &runtimepath
17 " Reset all options (except 'term, 'lines' and 'columns'). This makes sure a
18 " system wide configuration file doesn't change default values.
19 set all&
20 " And restore it after all other options were reset.
21 let &runtimepath = s:save_runtimepath
22
23 " Make sure Vim (and not Vi) settings are used.
24 set nocompatible
25
26 " Load my scripts from ~/.vim (my scripts) and ~/.vim/runtime (checkout of Vim
27 " runtime files).
28 set runtimepath-=~/.vim
29 set runtimepath^=~/.vim,~/.vim/runtime
30
31 " Don't store swap files in the same directory as the edited file.
32 set directory-=.
33
34 " Disable modelines as they may cause security problems. Instead use
35 " securemodelines (Vim script #1876).
36 set nomodeline
37
38 " When completing paths first use the longest path then display a list of all
39 " possible files.
40 set wildmode=longest,list
41
42
43 " EDIT SETTINGS
44
45 " Enable automatic file detection, plugin and indention support.
46 if has('autocmd')
47     filetype plugin indent on
48 endif
49
50 " Use UTF-8 file encoding for all files. Automatically recognize latin1 in
51 " existing files.
52 set fileencodings=utf-8,latin1
53
54 " Wrap text after 78 characters.
55 set textwidth=78
56
57 " Set tabs to 4 spaces, use softtabs.
58 set shiftwidth=4
59 set softtabstop=4
60 set expandtab
61 " When < and > is used indent/deindent to the next 'shiftwidth' boundary.
62 set shiftround
63 " Use the default value for real tabs.
64 set tabstop=8
65
66 " Enable auto indention.
67 set autoindent
68
69 " When joining lines only add one space after a sentence.
70 set nojoinspaces
71
72 " Allow backspacing over autoindent and line breaks.
73 set backspace=indent,eol
74
75 " Start a comment when hitting enter after a commented line (r) and when using
76 " o or O around a commented line (o).
77 set formatoptions+=ro
78 " Don't break a line if was already longer then 'textwidth' when insert mode
79 " started.
80 set formatoptions+=l
81
82 " Allow virtual editing (cursor can be positioned anywhere, even when there is
83 " no character) in visual block mode.
84 set virtualedit=block
85
86 " Already display matches while typing the search command. This makes spotting
87 " errors easy.
88 set incsearch
89
90 " Activate syntax folding.
91 if has('folding')
92     set foldmethod=syntax
93     set foldcolumn=2
94     set foldlevel=99 " no closed folds at default, 'foldenable' would disable
95                      " folding which is not what I want
96 endif
97
98 " Only check for case if the searched word contains a capital character.
99 set ignorecase
100 set smartcase
101
102 " Activate spell checking, use English as default. Don't use spell checking
103 " when diffing.
104 if v:version >= 700 && has('syntax') && !&diff
105     set spell
106     set spelllang=en_us
107 endif
108
109 " Allow buffers with changes to be hidden. Very important for effective
110 " editing with multiple buffers.
111 set hidden
112
113
114 " DISPLAY SETTINGS
115
116 " Use a dark background. Doesn't change the background color, only sets text
117 " colors for a dark terminal.
118 set background=dark
119
120 " Display line numbers.
121 set number
122 " But use as little space as necessary for the numbers column. Thanks to James
123 " Vega (http://git.jamessan.com/?p=etc/vim.git;a=summary).
124 if v:version >= 700
125     set numberwidth=1
126 endif
127 " Display the ruler with current line/file position. If 'statusline' is used
128 " then this only affects <C-g>.
129 set ruler
130 " Display partial commands in the status line.
131 set showcmd
132
133 " Don't redraw screen when executing macros; increases speed. Thanks to James
134 " Vega (http://git.jamessan.com/?p=etc/vim.git;a=summary).
135 set lazyredraw
136
137 " Visualize the line the cursor is currently in.
138 if v:version >= 700
139     set cursorline
140 endif
141
142 " Display tabs, trailing space, non breakable spaces and long lines (when
143 " wrapping is disabled).
144 set list
145 set listchars=trail:-,extends:>
146 if v:version >= 700
147     set listchars+=nbsp:!
148 endif
149
150 if has('statusline')
151     " Always display the status line even if there is only one window.
152     set laststatus=2
153
154     set statusline=
155     " on the left
156     set statusline+=%02n: " buffer number
157     set statusline+=%f\   " path to current file in buffer
158     set statusline+=%h    " [help] if buffer is help file
159     set statusline+=%w    " [Preview] if buffer is preview buffer
160     set statusline+=%m    " [+] if buffer was modified,
161                           " [-] if 'modifiable' is off
162     set statusline+=%r    " [RO] if buffer is read only
163
164     " on the right
165     set statusline+=%=                " right align
166     set statusline+=%-12.(%l,%c%V%)\  " line number (%l),
167                                       " column number (%c),
168                                       " virtual column number if different
169                                       "                       than %c (%V)
170     set statusline+=%P                " position in file in percent
171 endif
172
173
174 " MAPPINGS (except for plugins, see PLUGIN SETTINGS below)
175
176 " Disable arrow keys for all modes except command modes. Thanks to James Vega
177 " (http://git.jamessan.com/?p=etc/vim.git;a=summary).
178 map <right> <nop>
179 map <left>  <nop>
180 map <up>    <nop>
181 map <down>  <nop>
182 imap <right> <nop>
183 imap <left>  <nop>
184 imap <up>    <nop>
185 imap <down>  <nop>
186
187 " Use <space> to move down a page and - to move up one like in mutt.
188 nnoremap <Space> <C-f>
189 nnoremap - <C-b>
190
191 " Fast access to buffers.
192 nnoremap <Leader>1 :1b<CR>
193 nnoremap <Leader>2 :2b<CR>
194 nnoremap <Leader>3 :3b<CR>
195 nnoremap <Leader>4 :4b<CR>
196 nnoremap <Leader>5 :5b<CR>
197 nnoremap <Leader>6 :6b<CR>
198 nnoremap <Leader>7 :7b<CR>
199 nnoremap <Leader>8 :8b<CR>
200 nnoremap <Leader>9 :9b<CR>
201 nnoremap <Leader>0 :10b<CR>
202
203 " Make last active window the only window. Similar to <C-w> o.
204 nnoremap <C-w>O <C-w>p<C-w>o
205
206 " Maps to change spell language between English and German and disable spell
207 " checking.
208 if v:version >= 700
209     map <Leader>sn :set nospell<CR>
210     map <Leader>se :set spell spelllang=en_us<CR>
211     map <Leader>sd :set spell spelllang=de_de<CR>
212 endif
213
214 " Add semicolon to the end of the line. Thanks to
215 " http://www.van-laarhoven.org/vim/.vimrc for this idea and godlygeek in #vim
216 " on Freenode for an improved version which doesn't clobber any marks.
217 nnoremap <silent> <Leader>; :call setline(line('.'), getline('.') . ';')<CR>
218
219 " I often type "W" instead of "w" when trying to save a file. Fix my mistake.
220 " Thanks to Tony Mechelynck <antoine.mechelynck@gmail.com> from the Vim
221 " mailing list for the commands.
222 if v:version < 700
223     cnoreabbrev W w
224     cnoreabbrev Wa wa
225     cnoreabbrev Wq wq
226     cnoreabbrev Wqa wqa
227 else
228     cnoreabbrev <expr> W
229         \ ((getcmdtype() == ':' && getcmdpos() <= 2) ? 'w' : 'W')
230     cnoreabbrev <expr> Wa
231         \ ((getcmdtype() == ':' && getcmdpos() <= 3) ? 'wa' : 'Wa')
232     cnoreabbrev <expr> Wq
233         \ ((getcmdtype() == ':' && getcmdpos() <= 3) ? 'wq' : 'Wq')
234     cnoreabbrev <expr> Wqa
235         \ ((getcmdtype() == ':' && getcmdpos() <= 4) ? 'wqa' : 'Wqa')
236 endif
237 " Also fix my typo with "Q".
238 if v:version < 700
239     cnoreabbrev Q q
240     cnoreabbrev Qa qa
241 else
242     cnoreabbrev <expr> Q
243         \ ((getcmdtype() == ':' && getcmdpos() <= 2) ? 'q' : 'Q')
244     cnoreabbrev <expr> Qa
245         \ ((getcmdtype() == ':' && getcmdpos() <= 3) ? 'qa' : 'Qa')
246 endif
247
248 " Make sure xa0 (alt + space) is automatically changed to a normal whitespace
249 " if pressed accidentally while in insert mode (happens on Mac when alt
250 " doesn't send escape). filereadable() is necessary for Leopard were 'mac' is
251 " no longer set on the console.
252 if has('mac') || filereadable('/Users/.localized')
253     inoremap <Char-0xa0> <Space>
254 endif
255
256 " Disable Apple style movements in MacVim.
257 if has('gui_macvim') && has('eval')
258     let macvim_skip_cmd_opt_movement = 1
259 endif
260
261
262 " SYNTAX SETTINGS
263
264 " Activate syntax coloring.
265 if has('syntax')
266     syntax enable
267
268 " Don't highlight more than 200 columns as I normally don't have that long
269 " lines and they slow down syntax coloring. Thanks to Derek Wyatt
270 " (http://www.derekwyatt.org/vim/the-vimrc-file/).
271     set synmaxcol=200
272
273 " Highlight lines longer than 78 characters. Thanks to Tony Mechelynck
274 " <antoine.mechelynck@gmail.com> from the Vim mailing list. It can easily be
275 " disabled when necessary with :2match (in Vim >= 700).
276     if v:version >= 700
277         2match Todo /\%>78v./
278     else
279         match Todo /\%>78v./
280     endif
281
282 " Highlight TODO, FIXME, CHANGED and XXX in all documents.
283     if v:version > 701 || (v:version == 701 && has('patch42'))
284         call matchadd('Todo', '\(TODO\|FIXME\|CHANGED\|XXX\)')
285     endif
286 endif
287
288
289 " PLUGIN SETTINGS
290
291 " Settings for the NERD commenter.
292 " Don't create any mappings I don't want to use.
293 if has('eval')
294     let NERDCreateDefaultMappings = 0
295 endif
296 " Map toggle comment.
297 map <Leader><Leader> <plug>NERDCommenterToggle
298
299
300 " AUTO COMMANDS
301
302 " Use a custom auto group to prevent problems when the vimrc files is sourced
303 " twice.
304 if has('autocmd')
305     augroup vimrc
306         autocmd!
307
308 " Go to last position of opened files. Taken from :help last-position-jump.
309         autocmd BufReadPost *
310             \ if line("'\"") > 1 && line("'\"") <= line("$") |
311             \     execute "normal! g'\"" |
312             \ endif
313 " But not for Git commits, go to beginning of the file.
314         autocmd BufReadPost COMMIT_EDITMSG normal! gg
315
316 " Make sure 'list' and 'number' is disabled in help files. This is necessary
317 " when switching to a help buffer which is in the background with :buffer as
318 " these options are local to windows (and not only to buffers). This happens
319 " because I often want to use only one window and thus the help buffer is in
320 " the background.
321         autocmd BufWinEnter *.txt
322             \ if &filetype == 'help' |
323             \     setlocal nolist |
324             \     setlocal nonumber |
325             \ endif
326
327 " AFTER/FTPLUGIN AUTO COMMANDS
328
329 " Disable spell checking for files which don't need it.
330         autocmd FileType deb  setlocal nospell
331         autocmd FileType diff setlocal nospell
332         autocmd FileType tar  setlocal nospell
333 " Fix to allow Vim edit crontab files as crontab doesn't work with
334 " backupcopy=auto.
335         autocmd FileType crontab setlocal backupcopy=yes
336 " Don't use the modeline in git commits as the diff created by `git commit -v`
337 " may contain one which could change the filetype or other settings of the
338 " commit buffer. Also make sure we use only 72 characters per line which is
339 " the recommendation for git commit messages (http://tpope.net/node/106).
340         autocmd FileType gitcommit let g:secure_modelines_allowed_items = [] |
341                                  \ setlocal textwidth=72
342 " Allow folding in perl.
343         autocmd FileType perl let perl_fold = 1 |
344                             \ let perl_fold_blocks = 1
345 " Use the same comment string as for Vim files in Vimperator files.
346         autocmd FileType vimperator setlocal commentstring=\"%s
347
348 " FTDETECT AUTO COMMANDS
349
350 " Recognize .md as markdown files (Vim default is .mkd).
351         autocmd BufRead,BufNewFile *.md set filetype=mkd
352 " Recognize .test as Tcl files.
353         autocmd BufRead,BufNewFile *.test set filetype=tcl
354
355 " OTHER AUTO COMMANDS
356
357 " Use diff filetype for mercurial patches in patch queue.
358         autocmd BufReadPost */.hg/patches/* set filetype=diff
359
360     augroup END
361 endif