]> ruderich.org/simon Gitweb - config/dotfiles.git/blob - vimrc
98009b8611eb2885af03a11a4057c57899698ce9
[config/dotfiles.git] / vimrc
1 " Vim main configuration file.
2
3
4 " EDITOR SETTINGS
5
6 " Make sure Vim (and not Vi) settings are used.
7 set nocompatible
8
9 " Load my scripts from ~/.vim (my scripts) and ~/.vim/runtime (checkout of Vim
10 " runtime files).
11 set runtimepath-=~/.vim
12 set runtimepath^=~/.vim,~/.vim/runtime
13
14 " Disable modelines as they may cause security problems. Instead use
15 " securemodelines (Vim script #1876).
16 set nomodeline
17
18 " When completing paths first use the longest path then display a list of all
19 " possible files.
20 set wildmode=longest,list
21
22
23 " EDIT SETTINGS
24
25 " Enable automatic file detection, plugin and indention support.
26 if has('autocmd')
27     filetype plugin indent on
28 endif
29
30 " Use UTF-8 file encoding for all files. Automatically recognize latin1 in
31 " existing files.
32 set fileencodings=utf-8,latin1
33
34 " Wrap text after 78 characters.
35 set textwidth=78
36
37 " Set tabs to 4 spaces, use softtabs.
38 set shiftwidth=4
39 set softtabstop=4
40 set expandtab
41 " When < and > is used indent/deindent to the next 'shiftwidth' boundary.
42 set shiftround
43 " Use the default value for real tabs.
44 set tabstop=8
45
46 " Enable auto indention.
47 set autoindent
48
49 " When joining lines only add one space after a sentence.
50 set nojoinspaces
51
52 " Allow backspacing over autoindent and line breaks.
53 set backspace=indent,eol
54
55 " Start a comment when hitting enter after a commented line (r) and when using
56 " o or O around a commented line (o).
57 set formatoptions+=ro
58 " Don't break a line if was already longer then 'textwidth' when insert mode
59 " started.
60 set formatoptions+=l
61
62 " Allow virtual editing (cursor can be positioned anywhere, even when there is
63 " no character) in visual block mode.
64 set virtualedit=block
65
66 " Already display matches while typing the search command. This makes spotting
67 " errors easy.
68 set incsearch
69
70 " Activate syntax folding.
71 if has('folding')
72     set foldmethod=syntax
73     set foldcolumn=2
74     set foldlevel=99 " no closed folds at default, 'foldenable' would disable
75                      " folding which is not what I want
76 endif
77
78 " Only check for case if the searched word contains a capital character.
79 set ignorecase
80 set smartcase
81
82 " Activate spell checking, use English as default. Don't use spell checking
83 " when diffing.
84 if v:version >= 700 && !&diff
85     set spell
86     set spelllang=en_us
87 endif
88
89 " Allow buffers with changes to be hidden. Very important for effective
90 " editing with multiple buffers.
91 set hidden
92
93
94 " DISPLAY SETTINGS
95
96 " Use a dark background. Doesn't change the background color, only sets text
97 " colors for a dark terminal.
98 set background=dark
99
100 " Display line numbers.
101 set number
102 " Display the ruler with current line/file position.
103 set ruler
104 " Display partial commands in the status line.
105 set showcmd
106
107 " Visualize the line the cursor is currently in.
108 if v:version >= 700
109     set cursorline
110 endif
111
112 " Display tabs, trailing space, non breakable spaces and long lines (when
113 " wrapping is enabled).
114 set list
115 set listchars=trail:-,extends:>
116 if v:version >= 700
117     set listchars+=nbsp:!
118 endif
119
120
121 " MAPPINGS (except for plugins, see PLUGIN SETTINGS below)
122
123 " Use <space> to move down a page and - to move up one like in mutt.
124 nnoremap <Space> <C-f>
125 nnoremap - <C-b>
126
127 " Fast access to buffers.
128 nnoremap <Leader>1 :1b<CR>
129 nnoremap <Leader>2 :2b<CR>
130 nnoremap <Leader>3 :3b<CR>
131 nnoremap <Leader>4 :4b<CR>
132 nnoremap <Leader>5 :5b<CR>
133 nnoremap <Leader>6 :6b<CR>
134 nnoremap <Leader>7 :7b<CR>
135 nnoremap <Leader>8 :8b<CR>
136 nnoremap <Leader>9 :9b<CR>
137 nnoremap <Leader>0 :10b<CR>
138
139 " Maps to change spell language between English and German and disable spell
140 " checking.
141 if v:version >= 700
142     map <Leader>sn :set nospell<CR>
143     map <Leader>se :set spell spelllang=en_us<CR>
144     map <Leader>sd :set spell spelllang=de_de<CR>
145 endif
146
147 " Add semicolon to the end of the line. Thanks to
148 " http://www.van-laarhoven.org/vim/.vimrc for this idea and godlygeek in #vim
149 " on Freenode for an improved version which doesn't clobber any marks.
150 nnoremap <silent> ; :call setline(line('.'), getline('.') . ';')<CR>
151
152 " I often type "W" instead of "w" when trying to save a file. Fix my mistake.
153 " Thanks to Tony Mechelynck <antoine.mechelynck@gmail.com> from the Vim
154 " mailing list for the commands.
155 if v:version < 700
156     cnoreabbrev W w
157     cnoreabbrev Wq wq
158     cnoreabbrev Wqa wqa
159 else
160     cnoreabbrev <expr> W
161         \ ((getcmdtype() == ':' && getcmdpos() <= 2) ? 'w' : 'W')
162     cnoreabbrev <expr> Wq
163         \ ((getcmdtype() == ':' && getcmdpos() <= 3) ? 'wq' : 'Wq')
164     cnoreabbrev <expr> Wqa
165         \ ((getcmdtype() == ':' && getcmdpos() <= 4) ? 'wqa' : 'Wqa')
166 endif
167 " Also fix my typo with "Q".
168 if v:version < 700
169     cnoreabbrev Q q
170     cnoreabbrev Qa qa
171 else
172     cnoreabbrev <expr> Q
173         \ ((getcmdtype() == ':' && getcmdpos() <= 2) ? 'q' : 'Q')
174     cnoreabbrev <expr> Qa
175         \ ((getcmdtype() == ':' && getcmdpos() <= 3) ? 'qa' : 'Qa')
176 endif
177
178 " Make sure xa0 (alt + space) is automatically changed to a normal whitespace
179 " if pressed accidentally while in insert mode (happens on Mac when alt
180 " doesn't send escape). filereadable() is necessary for Leopard were 'mac' is
181 " no longer set on the console.
182 if has('mac') || filereadable('/Users/.localized')
183     inoremap <Char-0xa0> <Space>
184 endif
185
186 " Disable Apple style movements in MacVim.
187 if has('gui_macvim') && has('eval')
188     let macvim_skip_cmd_opt_movement = 1
189 endif
190
191
192 " SYNTAX SETTINGS
193
194 " Activate syntax coloring.
195 if has('syntax')
196     syntax enable
197
198 " Highlight lines longer than 78 characters. Thanks to Tony Mechelynck
199 " <antoine.mechelynck@gmail.com> from the Vim mailing list.
200 " It can easily be disabled if necessary with :2match (in Vim >= 700).
201     if v:version >= 700
202         2match Todo /\%>78v./
203     else
204         match Todo /\%>78v./
205     endif
206
207 " Highlight TODO, FIXME, CHANGED and XXX in all documents.
208     if v:version > 701 || (v:version == 701 && has('patch42'))
209         call matchadd('Todo', '\(TODO\|FIXME\|CHANGED\|XXX\)')
210     endif
211 endif
212
213
214 " PLUGIN SETTINGS
215
216 " Settings for the NERD commenter.
217 " Don't create any mappings I don't want to use.
218 if has('eval')
219     let NERDCreateDefaultMappings = 0
220 endif
221 " Map toggle comment.
222 map <Leader><Leader> <plug>NERDCommenterToggle
223
224
225 " AUTO COMMANDS
226
227 " Use a custom auto group to prevent problems when the vimrc files is sourced
228 " twice.
229 if has('autocmd')
230     augroup vimrc
231         autocmd!
232
233 " Go to last position of opened files. Taken from :help last-position-jump.
234         autocmd BufReadPost *
235             \ if line("'\"") > 1 && line("'\"") <= line("$") |
236             \     execute "normal! g'\"" |
237             \ endif
238 " But not for Git commits, go to beginning of the file.
239         autocmd BufReadPost COMMIT_EDITMSG normal! gg
240
241 " AFTER/FTPLUGIN AUTO COMMANDS
242
243 " Disable spell checking for files which don't need it.
244         autocmd FileType deb  setlocal nospell
245         autocmd FileType diff setlocal nospell
246         autocmd FileType tar  setlocal nospell
247 " Fix to allow Vim edit crontab files as crontab doesn't work with
248 " backupcopy=auto.
249         autocmd FileType crontab setlocal backupcopy=yes
250 " Don't use the modeline as the diff created by `git commit -v` may contain
251 " one which could change the filetype or other settings of the commit window.
252         autocmd FileType gitcommit setlocal nomodeline |
253                                  \ let g:secure_modelines_allowed_items = []
254 " Allow folding in perl.
255         autocmd FileType perl let perl_fold = 1 |
256                             \ let perl_fold_blocks = 1
257 " Use the same comment string as for Vim files in vimperator files.
258         autocmd FileType vimperator setlocal commentstring=\"%s
259
260 " FTDETECT AUTO COMMANDS
261
262 " Recognize .md as markdown files (Vim default is .mkd).
263         autocmd BufRead,BufNewFile *.md set filetype=mkd
264 " Recognize .test as Tcl files.
265         autocmd BufRead,BufNewFile *.test set filetype=tcl
266
267 " OTHER AUTO COMMANDS
268
269 " Use diff filetype for mercurial patches in patch queue.
270         autocmd BufReadPost */.hg/patches/* set filetype=diff
271
272     augroup END
273 endif