+" autocmds {{{1
+" Resize splits when the window is resized {{{2
+augroup resize
+ au!
+ autocmd VimResized * :wincmd =
+augroup END
+
+" Only show cursorline in the current window and in normal mode {{{2
+augroup cline
+ au!
+ au WinLeave,InsertEnter * set nocursorline
+ au WinEnter,InsertLeave * set cursorline
+augroup END
+
+" Treat buffers from stdin (e.g.: echo foo | vim -) as scratch {{{2
+augroup ft_stdin
+ au!
+ au StdinReadPost * :set buftype=nofile
+augroup END
+
+" Jump to last known cursor position {{{2
+augroup cursor_pos
+ au!
+ " blacklist certain filetype
+ let blacklist = ['gitcommit']
+ autocmd BufReadPost *
+ \ if index(blacklist, &ft) < 0 && line("'\"") > 1 && line("'\"") <= line("$") |
+ \ exe "normal! g`\"" |
+ \ endif
+augroup END
+
+" auto source vimrc when saved {{{2
+augroup source_vimrc
+ au!
+ autocmd bufwritepost vimrc source $MYVIMRC
+augroup END
+
+" bindings {{{1
+
+" allow both <space> and / to be <leader>
+map <space> <leader>
+
+" make
+nnoremap <leader><cr> :make<cr>
+
+" unhighlight search
+nnoremap <silent> <Leader>/ :silent nohl<CR>
+
+" Tabs
+nnoremap <leader>[ :tabprev<cr>
+nnoremap <leader>] :tabnext<cr>
+
+" Wrap
+nnoremap <leader>W :set wrap!<cr>
+
+" paste from selection
+nnoremap <leader>p* :silent! set paste<CR>"*p:set nopaste<CR>
+" paste from clipboard
+nnoremap <leader>p+ :silent! set paste<CR>"+p:set nopaste<CR>
+
+" Clean trailing whitespace
+nnoremap <silent> <leader>ww m':%s/\s\+$//<cr>:let @/=''<cr>``zz
+
+" Source
+vnoremap <leader>S y:execute @@<cr>:echo 'Sourced selection.'<cr>
+nnoremap <leader>S ^vg_y:execute @@<cr>:echo 'Sourced line.'<cr>
+
+" jump to last cursor position
+noremap ' `
+
+" Select (charwise) the contents of the current line, excluding indentation.
+nnoremap vv ^vg_
+
+" Toggle [i]nvisible characters
+nnoremap <leader>i :set list!<cr>
+
+" Unfuck my screen
+nnoremap U :syntax sync fromstart<cr>:AirlineRefresh<cr>:redraw!<cr>
+
+" Ranger
+nnoremap <leader>r :silent !ranger %:h<cr>:redraw!<cr>
+nnoremap <leader>R :silent !ranger<cr>:redraw!<cr>
+
+" Use sane regexes.
+nnoremap / /\v
+vnoremap / /\v
+cnoremap s/ s/\v
+
+" display the number of matches for the last search
+nmap <Leader># :%s:<C-R>/::gn<CR>
+
+" center cursor after search and open folds
+nnoremap n nzzzv
+nnoremap N Nzzzv
+
+" same when jumping around
+nnoremap g; g;zzzv
+nnoremap g, g,zzzv
+nnoremap <c-o> <c-o>zzzv
+
+" Not using the default mappings of 'To line from top/bottom'
+noremap H ^
+noremap L $
+vnoremap H ^
+vnoremap L g_
+
+" Heresy, emacs insert bindings
+inoremap <c-a> <esc>I
+inoremap <c-e> <esc>A
+cnoremap <c-a> <home>
+cnoremap <c-e> <end>
+
+" disable arrows
+noremap <Up> <NOP>
+noremap <Down> <NOP>
+noremap <Left> <NOP>
+noremap <Right> <NOP>
+inoremap <Up> <NOP>
+inoremap <Down> <NOP>
+inoremap <Left> <NOP>
+inoremap <Right> <NOP>
+cnoremap <Up> <NOP>
+cnoremap <Down> <NOP>
+cnoremap <Left> <NOP>
+cnoremap <Right> <NOP>
+cnoremap <C-K> <Up>
+cnoremap <C-J> <Down>
+cnoremap <C-H> <Left>
+cnoremap <C-L> <Right>
+
+" close all folds open fold in cursor
+nnoremap zx zMzxzz15<C-e>
+
+" edit vimrc in new tab
+nmap <leader>ev :tabedit $MYVIMRC<CR>
+
+map <F1> :ls<CR>:b<space>
+
+nnoremap <C-L> <C-W>w
+nnoremap <C-H> <C-W>W
+
+"xterm mouse with middleclick paste
+nnoremap <MiddleMouse> i<MiddleMouse>
+vnoremap <MiddleMouse> s<MiddleMouse>
+
+" fix legacy vi inconsistency
+map Y y$
+
+" allow repeat operator on visual
+vnoremap . :normal .<CR>
+
+" add line without changing position or leaving mode
+noremap <silent> <Leader>o :set paste<CR>m`o<ESC>``:set nopaste<CR>
+noremap <silent> <Leader>O :set paste<CR>m`O<ESC>``:set nopaste<CR>
+
+" Don't use Ex mode, use Q for formatting
+map Q gq
+
+" allow undoing in insert-mode
+inoremap <C-U> <C-G>u<C-U>
+inoremap <C-W> <C-G>u<C-W>
+
+nmap <Leader>b :set expandtab tabstop=4 shiftwidth=4 softtabstop=4<CR>
+nmap <Leader>B :set expandtab tabstop=8 shiftwidth=8 softtabstop=4<CR>
+nmap <Leader>M :set noexpandtab tabstop=8 softtabstop=4 shiftwidth=4<CR>
+nmap <Leader>m :set expandtab tabstop=2 shiftwidth=2 softtabstop=2<CR>
+
+" space will toggle current fold in normal mode
+nnoremap <leader><Space> za
+" create folds around visual selection
+vnoremap <leader><Space> zf
+
+autocmd BufWinLeave *.* mkview
+autocmd BufWinEnter *.* silent loadview
+
+" save with sudo
+cnoremap w!! w !sudo tee % > /dev/null
+