1 " from http://bwaldvogel.de/rc/vim.html
  2 " the XHTML files are generated using :%TOhtml
  3 "
  4 " other excellent vimrcs:
  5 "   https://github.com/sjl/dotfiles/blob/master/vim/.vimrc
  6 "   http://www.jukie.net/~bart/conf/vimrc
  7 "
  8 " nice blog post:
  9 "   http://items.sjbach.com/319/configuring-vim-right
 10 "
 11 " startup benchmarks
 12 "
 13 " Tip: vim --startuptime startup.log <filename>
 14 "
 15 " • vim-7.3.266 [desktop]
 16 "   # time vim -cq
 17 "     vim -cq  0,10s user 0,00s system 97% cpu 0,106 total
 18 " • vim-7.1.028 [laptop]
 19 "   # time vim -cq
 20 "     vim -cq  0.25s user 0.02s system 88% cpu 0.302 total
 21 " • vim-7.1.042 [laptop]
 22 "   # time vim -cq
 23 "     vim -cq  0.09s user 0.01s system 95% cpu 0.101 total
 24 "
 25 " use :help <keyword> in case you don't know what something stands for
 26 
 27 " Tricks:
 28 "
 29 " 1. enumerate lines in block mode
 30 "   C-v !cat -n
 31 "   or better use VisIncr → c-V :I [#]
 32 "
 33 " 2. insert unicode characters
 34 "   <C-v>u20ac → €
 35 "
 36 
 37 let g:omni_sql_no_default_maps = 1
 38 
 39 set nocompatible
 40 set showcmd
 41 
 42 " be careful: tabs may look different on other systems
 43 set tabstop=4
 44 set shiftwidth=4
 45 set autoindent
 46 set smartindent
 47 
 48 " don't go to the start of line when doing G etc.
 49 set nosol
 50 
 51 " recognize lines as you can see at the bottom
 52 set modeline
 53 set modelines=3
 54 
 55 " don't insert TWO spaces after '.', '?' etc. when joining lines
 56 set nojoinspaces
 57 
 58 " do macros, do them FAST!
 59 set lazyredraw
 60 
 61 " pop a completion list when pressing <tab> (like zsh does...)
 62 set wildmenu
 63 " have the completion similar to shell completion
 64 set wildmode=list:longest
 65 set wildignore=*.class,*.o,*.bak,*.swp,*.tmp,*.part
 66 
 67 " display also the hexcode of the char under the cursor
 68 set rulerformat=0x%02B\ %3p%%\ %l,%c%V
 69 " allways show the ruler
 70 set ruler
 71 
 72 " don't give the intro message when starting
 73 set shortmess+=I
 74 
 75 let maplocalleader=','
 76 
 77 " allow backspacing over autoindent, line breaks (join lines),
 78 " the start of insert; CTRL-W and CTRL-U stop once at the start of insert.
 79 set backspace=indent,eol,start
 80 
 81 " debugging {{{1
 82 " set verbosefile=/tmp/vim.verbose
 83 " set verbose=100
 84 
 85 " {{{1 highlighting
 86 " see ~/.vim/after/syntax/syncolor.vim too
 87 syntax on
 88 " color scheme for dark backgrounds >:)
 89 set background=dark
 90 " :so $VIMRUNTIME/syntax/hitest.vim
 91 
 92 " I configure my screen with --enable-colors256 (Gentoo does this by default!)
 93 if $TERM =~ '^screen' | set t_Co=256 | endif
 94 " I use a patched rxvt-unicode which is able to display 256 colors
 95 if $TERM =~ '^rxvt-unicode' | set t_Co=256 | endif
 96 if $TERM =~ '^xterm' | set t_Co=256 | endif
 97 
 98 if &t_Co >= 256
 99   try
100     colorscheme 256asu1black
101   catch
102     colorscheme pablo
103   endtry
104 else
105   colorscheme pablo
106 endif
107 
108 " favourite colorschemes:
109 "  • 256_asu1dark
110 "  • solarized
111 "  • pablo
112 "  • torte
113 "  • desert256
114 "
115 
116 let g:solarized_termcolors=256
117 
118 " statusline {{{1
119 " https://twitter.com/#!/grantlucas/statuses/111827076373422082
120 " https://bitbucket.org/sjl/dotfiles/src/2e25b11e75fc/vim/.vimrc#cl-86
121 set laststatus=2
122 set statusline+=\                            " Space for padding on left side
123 set statusline+=%<                           " Truncate on the left side of text if too long
124 set statusline+=%t                           " File name (Tail)
125 set statusline+=%r                           " Readonly Flag
126 set statusline+=%m                           " Modified Flag
127 set statusline+=\                            " Space
128 set statusline+=%#warningmsg#                " Set warning highlighting
129 set statusline+=%{SyntasticStatuslineFlag()} " Show syntax errors provided by syntastic plugin
130 set statusline+=%*                           " clear highlighting
131 set statusline+=%=                           " Right Align
132 set statusline+=%{SynGroup()}                " Show current syntax group
133 set statusline+=\                            " Space
134 set statusline+=%{ShowSpell()}               " Show whether or not spell is currently on
135 set statusline+=\                            " Space
136 set statusline+=%{ShowWrap()}                " Show whether or not wrap is currently on
137 set statusline+=\                            " Space
138 set statusline+=%{fugitive#statusline()}     " Git branch name courtesy of Fugitive plugin
139 set statusline+=%w                           " Preview window flag
140 set statusline+=\                            " Space
141 set statusline+=%{&ff}                       " Format of file
142 set statusline+=/
143 set statusline+=%{strlen(&fenc)?&fenc:&enc}  " Encoding of file
144 set statusline+=/
145 set statusline+=%{&ft}                       " Type of file
146 set statusline+=\                            " Space
147 set statusline+=0x%02B                       " Hex of current symbol
148 set statusline+=\                            " Space
149 set statusline+=(%l/%L,%c%V)                 " Current position and line count
150 set statusline+=\                            " Space
151 set statusline+=%P                           " Percent
152 set statusline+=\                            " Space for padding on right side
153 
154 function! SynGroup()
155   return synIDattr(synID(line("."), col("."), 1), "name")
156 endfunction
157 
158 function! ShowWrap()
159   if &wrap
160     return "[Wrap]"
161   else
162     return ""
163 endfunction
164 
165 function! ShowSpell()
166   if &spell
167     return "[Spell]"
168   else
169     return ""
170 endfunction
171 
172 hi StatusLine ctermfg=white ctermbg=236
173 hi StatusLineNC ctermfg=247 ctermbg=236
174 " swap statusline colors in insert mode
175 " inspired by https://twitter.com/#!/dotvimrc/statuses/114358110054920192
176 au InsertEnter * hi StatusLine ctermfg=236 ctermbg=white
177 au InsertLeave * hi StatusLine ctermfg=white ctermbg=236
178 
179 " {{{1 Python
180 " I want all possible Python highlighting (see ft-python-syntax)
181 let python_highlight_all=1
182 
183 " {{{1 folding (see :h folding)
184 " show all folds closed
185 set foldenable
186 " fold on markers tripple {
187 set foldmethod=marker
188 autocmd FileType c,cpp,d,perl,java,cs set foldmethod=syntax
189 autocmd FileType python set foldmethod=indent
190 set foldcolumn=1
191 set foldlevel=99
192 
193 " http://vim.wikia.com/wiki/Auto-fold_Perl_subs
194 let perl_fold=1
195 let perl_fold_blocks=1
196 
197 "Comments {{{1
198 " add new filetypes:
199 autocmd FileType apache,gitcommit set commentstring=#\ %s
200 
201 vmap <silent> <LocalLeader>c <Plug>Commentary
202 nmap <silent> <LocalLeader>c <Plug>CommentaryLine
203 
204 " searching {{{1
205 " highlight search
206 set hls
207 " higlight while searching
208 set incsearch
209 " search for upper and lowercase
210 set ignorecase
211 " but if user types uppercase - search exactly
212 set smartcase
213 nmap <silent> <LocalLeader>n :nohlsearch<cr>
214 
215 " Files (types, skeletons, indenting) {{{1
216 " detect filetypes and turn on indentation (cindent for c files e.g.)
217 filetype plugin indent on
218 " break c/c++/perl/python/java after 80 lines
219 " comments only, but it depends on 'formatoptions'
220 
221 " Indenting in source files
222 autocmd FileType c,cpp,cs,d,html,java,perl,php,python,shell,vim,xhtml,xml,xslt,xsl set tabstop=2 shiftwidth=2 noexpandtab
223 " automatic detection of et,sw,ts setting with script #1171
224 " autocmd BufReadPost * :DetectIndent
225 
226 " skeletons {{{2
227 " perl
228 autocmd BufNewFile *.pl 0r ~/.vim/skeleton.pl|normal G
229 " python
230 autocmd BufNewFile *.py 0r ~/.vim/skeleton.py|normal G
231 " C
232 autocmd BufNewFile *.c 0r ~/.vim/skeleton.c|normal G
233 " C++
234 autocmd BufNewFile *.cpp 0r ~/.vim/skeleton.cpp|normal G
235 " D
236 autocmd BufNewFile *.d 0r ~/.vim/skeleton.d|normal G
237 " (X)HTML
238 autocmd BufNewFile *.html 0r ~/.vim/skeleton.html|normal GddA
239 autocmd BufNewFile *.xhtml,*.shtml 0r ~/.vim/skeleton.xhtml|set ft=xhtml|normal G
240 " XML
241 autocmd BufNewFile *.xml 0r ~/.vim/skeleton.xml|normal G
242 " XSL
243 autocmd BufNewFile *.xsl 0r ~/.vim/skeleton.xsl|normal 2j26|
244 " Shell
245 autocmd BufNewFile *.sh 0r ~/.vim/skeleton.sh|normal G
246 " Lisp
247 autocmd BufNewFile *.lisp 0r ~/.vim/skeleton.lisp|normal G
248 " Markdown
249 autocmd BufNewFile *.mkd 0r ~/.vim/skeleton.mkd|normal G
250 
251 " Terminal {{{1
252 set mouse=a
253 " I almost use vim within a xterm compatible terminal (rxvt eg.)
254 set ttymouse=xterm
255 nmap <MiddleMouse> <Nop>
256 map <S-Insert> <MiddleMouse>
257 " I hate beeping (if your term is too slow: try rxvt :P)
258 set visualbell
259 
260 
261 " When on, the title of the window will be set to the value of 'titlestring'
262 set title
263 
264 " {{{1 vim7+ spell stuff
265 if version >= 700
266   " toggle spell on/off
267   map <F7> :set spell!<Cr>
268   imap <F7> <Esc>:set spell!<Cr>`]a
269 
270   " I'd like to use Ctrl-C like Escape
271   noremap <C-c> <Esc>
272 
273   " I prefer to turn on spellchecking via modeline or on demand
274   set nospell
275 
276   " I don't want modelines to be spellchecked
277   autocmd Syntax * syn region vimModeline start="\svim.\{-}:" end=+:+ display oneline transparent containedin=.*Comment.* contains=@NoSpell
278 
279 endif
280 
281 " abbreviations, nice shortcuts {{{1
282 
283 " swap (transpose) two letters. 'ab' becomes 'ba'
284 nmap <silent> <LocalLeader>l xph
285 " swap (transpose) two words
286 nmap <silent> <LocalLeader>w "_yiw:s/\(\%#\w\+\)\(\W\+\)\(\w\+\)/\3\2\1/<cr><c-o>:nohlsearch<cr>
287 " show the syntax group of the char under the cursor
288 nmap <LocalLeader>s :echo SynGroup()<cr>
289 nmap <LocalLeader>S :echo "hi<" . synIDattr(synID(line("."),col("."),1),"name") . "> trans<" . synIDattr(synID(line("."),col("."),0),"name") . "> lo<" . synIDattr(synIDtrans(synID(line("."),col("."),1)),"name") . ">"<cr>
290 
291 imap <silent> <C-Z> <Esc><C-Z>a
292 
293 " reindent the whole file (and restore position)
294 noremap <silent> <LocalLeader>i msHmtgg=G``'tzt`s
295 vnoremap <silent> <LocalLeader>i =
296 
297 " paste x11 clipboard content
298 noremap <silent> <LocalLeader>p "+p
299 
300 " I often type :Q/W instead of :q/w
301 command -bang Q q<bang>
302 command -bang Qa qa<bang>
303 " bar: The command can be followed by a "|" and another command
304 command -bang -bar -nargs=? -complete=file W w<bang> <args>
305 
306 " http://stackoverflow.com/questions/95072/what-are-your-favorite-vim-tricks
307 cmap w!! %!sudo tee > /dev/null %
308 
309 " toggle between matching of long lines and trailing whitespaces {{{2
310 " highlight whitespaces at EOL
311 autocmd Syntax * call MatchTrailingWhitespaces()
312 
313 highlight rightMargin term=bold ctermfg=blue guifg=blue
314 
315 noremap <silent> <LocalLeader>m :call ToggleMatch()<Cr>
316 
317 fun MatchTrailingWhitespaces()
318   match Error /\s\+$/
319   map <silent> <LocalLeader>t /\s\+$<Cr>
320   let g:MatchLongLines=0
321 endfun
322 
323 fun MatchLongLines()
324   match rightMargin /.\%>80v/
325   map <silent> <LocalLeader>t /\v(.%>80v)+<Cr>
326   let g:MatchLongLines=1
327 endfun
328 
329 fun ToggleMatch()
330   if !exists( "g:MatchLongLines" ) || g:MatchLongLines==0
331     call MatchLongLines()
332   elseif g:MatchLongLines==1
333     call MatchTrailingWhitespaces()
334   endif
335 endfun
336 
337 " this one is nice:  press ',u' to get the unicode name of the symbol under the cursor {{{2
338 " CREDITS: mgedmin on sf.net#vim
339 " EXAMPLE: go over this symbol: Ω and press ,u → it will print 'GREEK CAPITAL LETTER OMEGA'
340 if has('python')
341   nmap <silent> <LocalLeader>u :exec('py getUnicodeName()')<Cr>
342 python <<PYTHON
343 # -*- coding: utf-8 -*-
344 import vim, unicodedata
345 
346 def getUnicodeName():
347   try:
348     print unicodedata.name(vim.current.line[vim.current.window.cursor[1]:].decode('UTF-8')[0])
349   except IndexError, inst:
350     print "getUnicodeName():", inst
351 
352 PYTHON
353 endif
354 
355 " F5(make), F8(taglist), F9(numbering), F10(listchars) {{{1
356 noremap <F5> :make<Cr>
357 inoremap <F5> <Esc>:make<Cr>
358 
359 " toggle taglist (taglist plugin required!)
360 noremap <silent> <F6> :TlistToggle<Cr>
361 inoremap <silent> <F6> <Esc>:TlistToggle<Cr>`]a
362 
363 " toggle relative line numbers (RltvNmbr.vim plugin required)
364 " http://vim.sourceforge.net/scripts/script.php?script_id=2351
365 noremap <silent> <F8> :RN<Cr>
366 inoremap <silent> <F8> <Esc>:RN<Cr>`]a
367 
368 " use 'numberwidth' to set the number of columns for the line number
369 " toggle line numeration
370 noremap <silent> <F9> :set number!<Cr>
371 inoremap <silent> <F9> <Esc>:set number!<Cr>`]a
372 " however for some files I want line numbering by default
373 autocmd BufRead *known_hosts,*authorized_keys,*rc,*history set number
374 
375 " Remove whitespaces at EOL in the whole file
376 function TrimWhiteSpace()
377   %s/\s*$//
378 :endfunction
379 
380 " see :he restore-position
381 noremap <F12> msHmt:call TrimWhiteSpace()<CR>'tzt`s
382 inoremap <F12> msHmt:call TrimWhiteSpace()<CR>'tzt`s
383 
384 set listchars=tab:▸\ ,trail:_,eol:¬,extends:,precedes:385 "set listchars=tab:\ \ ,trail:_
386 noremap <silent> <F10> :set list!<Cr>
387 inoremap <silent> <F10> <Esc>:set list!<Cr>`]a
388 
389 set pastetoggle=<F11>
390 
391 " :TOhtml {{{1
392 let html_ignore_folding = 1
393 let html_number_lines = 1
394 let html_use_css = 1
395 let use_xhtml = 1
396 
397 " latex {{{1
398 " OPTIONAL: Starting with Vim 7, the filetype of empty .tex files defaults to
399 " 'plaintex' instead of 'tex', which results in vim-latex not being loaded.
400 " The following changes the default filetype back to 'tex':
401 let g:tex_flavor='latex'
402 
403 " plug-in settings {{{1
404 
405 " showmarks.vim plugin settings {{{2
406 " I want to enable showmarks on demand (using \mt)
407 let g:showmarks_enable=0
408 let g:showmarks_textlower=" "
409 let g:showmarks_textupper=" "
410 let g:showmarks_textother=" "
411 let g:showmarks_include="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
412 
413 " closetag.vim plugin settings {{{2
414 let b:closetag_html_style=1
415 
416 " omni completion {{{1
417 " disabled, because systags was 67mb large and hence much too slow
418 " do ctags -R -f ~/.vim/systags /usr/include /usr/local/include to tag system functions
419 " see :h ft-c-omni
420 " set tags+=~/.vim/systags
421 
422 " cscope {{{1
423 " if has("cscope")
424 "   set csprg=/usr/bin/cscope
425 "   set csto=0
426 "   set cst
427 "   set nocsverb
428 "   " add any database in current directory
429 "   if filereadable("cscope.out")
430 "     cs add cscope.out
431 "     " else add database pointed to by environment
432 "   elseif $CSCOPE_DB != ""
433 "     cs add $CSCOPE_DB
434 "   endif
435 "   set csverb
436 " endif
437 
438 " plugins {{{1
439 
440 " syntactics
441 let g:syntastic_enable_signs=1
442 
443 " pathogen
444 " http://www.vim.org/scripts/script.php?script_id=2332
445 " http://github.com/tpope/vim-pathogen
446 runtime bundle/tpope-vim-pathogen/autoload/pathogen.vim
447 call pathogen#infect()
448 
449 " gundo
450 " http://briancarper.net/blog/573/vim-undo-tree-visualization
451 " https://bitbucket.org/sjl/gundo.vim/
452 nnoremap <localleader>z :GundoToggle<CR>
453 
454 " }}}1
455 " calculator (http://www.unfug.org/data/foils/ss10/vimrc)
456 ino <C-A> <C-O>yiW<End>=<C-R>=<C-R>0<CR>
457 
458 if version >= 700
459   " function to go to the Most Recently Used (MRU) tab page
460   " like Ctrl-a Ctrl-a in screen does
461   " or Alt-Tab in the common window managers
462   au TabLeave * let g:MRUtabPage = tabpagenr()
463   fun MRUTab()
464     if exists( "g:MRUtabPage" )
465       exe "tabn " g:MRUtabPage
466     endif
467   endfun
468   noremap <silent> gl :call MRUTab()<Cr>
469 endif
470 
471 if !exists(':DiffOrig')
472   command DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis
473         \ | wincmd p | diffthis
474 endif
475 
476 " mark incorrect modelines (not closed with a colon)
477 autocmd Syntax * 2match Error "vim\d\{0,3}:\s\+set[^:]*$"
478 
479 " alternate file
480 " set timeout timeoutlen=250 ttimeoutlen=100
481 map <silent> <LocalLeader>h :A<Cr>
482 map <silent> <LocalLeader>as :AS<Cr>
483 map <silent> <LocalLeader>av :AV<Cr>
484 
485 if filereadable(".vimrc.host")
486   source .vimrc.host
487 endif
488 
489 " vim: set ft=vim fdm=marker fdl=0 fenc=utf-8 :
490 " vim700: set spelllang=en :