]> ruderich.org/simon Gitweb - config/dotfiles.git/blobdiff - vimrc
vimrc: Add support to undo <C-@>, <C-A> and <C-W> in insert mode.
[config/dotfiles.git] / vimrc
diff --git a/vimrc b/vimrc
index 97d0c371cfdce3393c500537e02bc7e53cf31200..6516546234febcfcfa372f56827426d8cc30d28a 100644 (file)
--- a/vimrc
+++ b/vimrc
@@ -3,18 +3,13 @@
 
 " EDITOR SETTINGS
 
-" Prevent editing as root as it may cause security problems. Use sudoedit
-" instead. Thanks to godlygeek in #vim on Freenode (2009-06-19 22:21 CEST).
-if $HOME == '/root' || exists('$SUDO_USER')
-    echomsg 'Running as root is forbidden! Use sudoedit.'
-    qall
-endif
-
-
 " Save 'runtimepath' in case it was changed by the system's configuration
-" files.
+" files. Also save 'diff' as set all& resets it; but somehow later (after
+" sourcing the vimrc - for example in a VimEnter autocmd) it gets
+" automagically restored to the correct value.
 if has('eval')
     let s:save_runtimepath = &runtimepath
+    let s:save_diff = &diff
 endif
 " Reset all options (except 'term', 'lines' and 'columns'). This makes sure a
 " system wide configuration file doesn't change default values.
@@ -22,18 +17,23 @@ set all&
 " And restore it after all other options were reset.
 if has('eval')
     let &runtimepath = s:save_runtimepath
+    let &diff = s:save_diff
+    unlet s:save_runtimepath
+    unlet s:save_diff
 endif
 
 " Make sure Vim (and not Vi) settings are used.
 set nocompatible
 
 " Load my scripts from ~/.vim (my scripts) and ~/.vim/runtime (checkout of Vim
-" runtime files).
+" runtime files) if available.
 set runtimepath-=~/.vim
 set runtimepath^=~/.vim,~/.vim/runtime
 
 " Don't store swap files in the same directory as the edited file.
 set directory-=.
+" But store them in ~/.tmp if available.
+set directory^=~/.tmp
 
 " Disable modelines as they may cause security problems. Instead use
 " securemodelines (Vim script #1876).
@@ -51,7 +51,7 @@ set history=1000
 set undolevels=1000
 
 " Use strong encryption if possible, also used for swap/undo files.
-if v:version >= 703
+if exists('+cryptmethod')
     set cryptmethod=blowfish
 endif
 
@@ -116,10 +116,12 @@ endif
 set ignorecase
 set smartcase
 
-" Activate spell checking, use English as default. Don't use spell checking
-" when diffing.
-if v:version >= 700 && has('syntax') && !&diff
-    set spell
+" Activate spell checking, use English as default.
+if exists('+spell') && has('syntax')
+    " But not when diffing as spell checking is distracting in this case.
+    if !&diff
+        set spell
+    endif
     set spelllang=en_us
 endif
 
@@ -138,7 +140,7 @@ set background=dark
 set number
 " But use as little space as necessary for the numbers column. Thanks to James
 " Vega (http://git.jamessan.com/?p=etc/vim.git;a=summary).
-if v:version >= 700
+if exists('+numberwidth')
     set numberwidth=1
 endif
 " Display the ruler with current line/file position. If 'statusline' is used
@@ -152,7 +154,7 @@ set showcmd
 set lazyredraw
 
 " Visualize the line the cursor is currently in.
-if v:version >= 700
+if exists('+cursorline')
     set cursorline
 endif
 
@@ -221,41 +223,44 @@ cmap <Left>  <Nop>
 cnoremap <C-P> <Up>
 cnoremap <C-N> <Down>
 
+" Write before suspending, thanks to deryni in #vim on Freenode (2011-05-09
+" 20:02 CEST). To suspend without saving either unmap this or use :stop<CR>.
+nnoremap <silent> <C-Z> :update<CR>:stop<CR>
+
+" 2<C-G> gives more verbose information, use it by default. Thanks to NCS_One
+" in #vim on Freenode (2011-08-15 00:17 CEST).
+nnoremap <C-G> 2<C-G>
+
 " Use <Space> to move down a page and - to move up one like in mutt.
 nnoremap <Space> <C-F>
 nnoremap - <C-B>
 
-" Behave like 'scrolloff' but only while searching. Thanks to "Benjamin R.
-" Haskell" <vim@benizi.com> from the Vim mailing list (2010-10-26).
-nnoremap n nzv3j3k
-nnoremap N Nzv3k3j
-
 " Go to next and previous buffer. Thanks to elik in #vim on Freenode
 " (2010-05-16 18:38 CEST) for this idea.
 nnoremap <silent> gb :bnext<CR>
 nnoremap <silent> gB :bprev<CR>
 
 " Fast access to buffers.
-nnoremap <Leader>1 :1b<CR>
-nnoremap <Leader>2 :2b<CR>
-nnoremap <Leader>3 :3b<CR>
-nnoremap <Leader>4 :4b<CR>
-nnoremap <Leader>5 :5b<CR>
-nnoremap <Leader>6 :6b<CR>
-nnoremap <Leader>7 :7b<CR>
-nnoremap <Leader>8 :8b<CR>
-nnoremap <Leader>9 :9b<CR>
-nnoremap <Leader>0 :10b<CR>
-
-" Make last active window the only window. Similar to <C-w> o.
+nnoremap <silent> <Leader>1 :1b<CR>
+nnoremap <silent> <Leader>2 :2b<CR>
+nnoremap <silent> <Leader>3 :3b<CR>
+nnoremap <silent> <Leader>4 :4b<CR>
+nnoremap <silent> <Leader>5 :5b<CR>
+nnoremap <silent> <Leader>6 :6b<CR>
+nnoremap <silent> <Leader>7 :7b<CR>
+nnoremap <silent> <Leader>8 :8b<CR>
+nnoremap <silent> <Leader>9 :9b<CR>
+nnoremap <silent> <Leader>0 :10b<CR>
+
+" Make last active window the only window. Similar to <C-W> o.
 nnoremap <C-W>O <C-W>p<C-W>o
 
 " Maps to change spell language between English and German and disable spell
 " checking.
-if v:version >= 700
-    noremap <Leader>sn :set nospell<CR>
-    noremap <Leader>se :set spell spelllang=en_us<CR>
-    noremap <Leader>sd :set spell spelllang=de_de<CR>
+if exists('+spell')
+    noremap <silent> <Leader>sn :set nospell<CR>
+    noremap <silent> <Leader>se :set spell spelllang=en_us<CR>
+    noremap <silent> <Leader>sd :set spell spelllang=de_de<CR>
 endif
 
 " Add semicolon to the end of the line. Thanks to
@@ -315,14 +320,25 @@ if has('mac') || filereadable('/Users/.localized')
 endif
 
 " Disable Apple style movements in MacVim.
-if has('gui_macvim') && has('eval')
-    let macvim_skip_cmd_opt_movement = 1
+if has('gui_macvim')
+    let g:macvim_skip_cmd_opt_movement = 1
 endif
 
 " In case 'hlsearch' is used disable it with <C-L>. Thanks to frogonwheels and
 " vimgor (bot) in #vim on Freenode (2010-03-30 05:58 CEST).
 noremap <silent> <C-L> :nohlsearch<CR><C-L>
 
+" Ctrl-U in insert mode deletes a lot, break undo sequence before deleting the
+" line so the change can be undone. Thanks to the vimrc_example.vim file in
+" Vim's source.
+inoremap <C-U> <C-G>u<C-U>
+" Same for <C-@> (insert previously inserted text and leave insert mode).
+inoremap <C-@> <C-G>u<C-@>
+" And for <C-A> (insert previously inserted text).
+inoremap <C-A> <C-G>u<C-A>
+" And for <C-W> (delete word before cursor).
+inoremap <C-W> <C-G>u<C-W>
+
 
 " ABBREVIATIONS
 
@@ -340,43 +356,66 @@ if has('syntax')
 " Don't highlight more than 500 columns as I normally don't have that long
 " lines and they slow down syntax coloring. Thanks to Derek Wyatt
 " (http://www.derekwyatt.org/vim/the-vimrc-file/).
-    set synmaxcol=500
+    if exists('+synmaxcol')
+        set synmaxcol=500
+    endif
 
 " Highlight lines longer than 78 characters. Thanks to Tony Mechelynck
 " <antoine.mechelynck@gmail.com> from the Vim mailing list. It can easily be
 " disabled when necessary with :2match (in Vim >= 700).
-    if v:version >= 700
+    if exists(':2match')
         2match Todo /\%>78v./
     else
         match Todo /\%>78v./
     endif
 
-" Highlight TODO, FIXME, CHANGED and XXX in all documents.
-    if v:version > 701 || (v:version == 701 && has('patch42'))
-        call matchadd('Todo', '\(TODO\|FIXME\|CHANGED\|XXX\)')
+    if exists('*matchadd')
+" Highlight some important keywords in all documents.
+        for x in ['TODO', 'XXX', 'FIXME', 'CHANGED', 'REMOVED', 'DELETED']
+            call matchadd('Todo', x)
+        endfor
+
+" Highlight unicode whitespace which is no normal whitespace (0x20).
+        for x in ['00a0', '1680', '180e', '2000', '2001', '2002', '2003',
+                \ '2004', '2005', '2006', '2007', '2008', '2009', '200a',
+                \ '200b', '200c', '200d', '202f', '205f', '2060', '3000',
+                \ 'feff']
+            call matchadd('Error', '\%u' . x)
+        endfor
     endif
+
+" Settings for specific filetypes.
+
+    " Haskell.
+    let g:hs_highlight_boolean = 1
+    let g:hs_highlight_types = 1
+    let g:hs_highlight_more_types = 1
+
+    " Perl.
+    let g:perl_fold = 1
+    let g:perl_fold_blocks = 1
+    let g:perl_nofold_packages = 1
+    let g:perl_include_pod = 1 " syntax coloring for PODs
 endif
 
 
 " PLUGIN SETTINGS
 
+if has('eval')
 " Use pathogen which allows one 'runtimepath' entry per plugin. This makes
 " installing/removing/updating plugins simple. (Used for plugins with more
 " than one file.)
-if has('eval')
-    call pathogen#runtime_append_all_bundles()
-endif
+    if v:version >= 700
+        execute 'call pathogen#runtime_append_all_bundles()'
+    endif
 
 " Settings for the NERD commenter.
-" Don't create any mappings I don't want to use.
-if has('eval')
+    " Don't create any mappings I don't want to use.
     let NERDCreateDefaultMappings = 0
-endif
-" Map toggle comment.
-map <Leader><Leader> <Plug>NERDCommenterToggle
+    " Map toggle comment.
+    map <Leader><Leader> <Plug>NERDCommenterToggle
 
 " XPTemplate settings.
-if has('eval')
     " Try to maintain snippet rendering even after editing outside of a
     " snippet.
     let g:xptemplate_strict = 0
@@ -393,6 +432,7 @@ endif
 " twice.
 if has('autocmd')
     augroup vimrc
+" Remove all autocmds from the current group.
         autocmd!
 
 " Go to last position of opened files. Taken from :help last-position-jump.
@@ -417,7 +457,12 @@ if has('autocmd')
 " Automatically disable 'paste' mode when leaving insert mode. Thanks to
 " Raimondi in #vim on Freenode (2010-08-14 23:01 CEST). Very useful as I only
 " want to paste once and then 'paste' gets automatically unset.
-        autocmd InsertLeave * set nopaste
+        if exists('##InsertLeave')
+            autocmd InsertLeave * set nopaste
+        endif
+
+" Write file when running :mak[e] before 'makeprg' is called.
+        autocmd QuickFixCmdPre * write
 
 " AFTER/FTPLUGIN AUTO COMMANDS
 
@@ -434,11 +479,10 @@ if has('autocmd')
 " the recommendation for git commit messages (http://tpope.net/node/106).
         autocmd FileType gitcommit let g:secure_modelines_allowed_items = [] |
                                  \ setlocal textwidth=72
-" Allow folding in perl.
-        autocmd FileType perl let perl_fold = 1 |
-                            \ let perl_fold_blocks = 1
 " Use the same comment string as for Vim files in Vimperator files.
         autocmd FileType vimperator setlocal commentstring=\"%s
+" Use tex compiler for (La)TeX files.
+        autocmd FileType tex compiler tex
 
 " FTDETECT AUTO COMMANDS
 
@@ -458,3 +502,68 @@ if has('autocmd')
 
     augroup END
 endif
+
+
+" CUSTOM FUNCTIONS AND COMMANDS
+
+if has('eval')
+" New text-objects ii and ai to work on text with the same indentation. Thanks
+" to http://vim.wikia.com/index.php?title=Indent_text_object&oldid=27126
+" (visited on 2011-11-19).
+    onoremap <silent> ai :<C-U>call <SID>IndTxtObj(0)<CR>
+    onoremap <silent> ii :<C-U>call <SID>IndTxtObj(1)<CR>
+    vnoremap <silent> ai :<C-U>call <SID>IndTxtObj(0)<CR><Esc>gv
+    vnoremap <silent> ii :<C-U>call <SID>IndTxtObj(1)<CR><Esc>gv
+
+    function! s:IndTxtObj(inner)
+        let curline = line(".")
+        let lastline = line("$")
+        let i = indent(line(".")) - &shiftwidth * (v:count1 - 1)
+        let i = i < 0 ? 0 : i
+        if getline(".") !~ "^\\s*$"
+            let p = line(".") - 1
+            let nextblank = getline(p) =~ "^\\s*$"
+            while p > 0
+                    \ && ((i == 0 && !nextblank)
+                        \ || (i > 0 && ((indent(p) >= i
+                            \ && !(nextblank && a:inner))
+                            \ || (nextblank && !a:inner))))
+                -
+                let p = line(".") - 1
+                let nextblank = getline(p) =~ "^\\s*$"
+            endwhile
+            normal! 0V
+            call cursor(curline, 0)
+            let p = line(".") + 1
+            let nextblank = getline(p) =~ "^\\s*$"
+            while p <= lastline
+                    \ && ((i == 0 && !nextblank)
+                        \ || (i > 0 && ((indent(p) >= i
+                            \ && !(nextblank && a:inner))
+                            \ || (nextblank && !a:inner))))
+                +
+                let p = line(".") + 1
+                let nextblank = getline(p) =~ "^\\s*$"
+            endwhile
+            normal! $
+        endif
+    endfunction
+
+" Convenient command to see the difference between the current buffer and the
+" file it was loaded from, thus the changes you made. Thanks to the
+" vimrc_example.vim file in Vim's source. Modified to use the same filetype
+" for the diffed file than the filetype for the original file.
+    if !exists(":DiffOrig")
+        command DiffOrig
+            \ let s:diff_orig_filetype = &filetype
+            \ | vertical new
+            \ | let &filetype = s:diff_orig_filetype
+            \ | unlet s:diff_orig_filetype
+            \ | set buftype=nofile
+            \ | read ++edit #
+            \ | 0d_
+            \ | diffthis
+            \ | wincmd p
+            \ | diffthis
+    endif
+endif