" options {{{ " general {{{ set nocompatible filetype indent plugin on syntax on set modeline " some systems disable this by default " }}} " persistence {{{ set history=10000 if has("persistent_undo") set undofile set undodir=~/.cache/vim/undo endif " }}} " buffers {{{ set autoread set autowrite set confirm set hidden set nostartofline " }}} " display {{{ set display+=lastline,uhex set lazyredraw set linebreak set report=0 set scrolloff=3 set showbreak=> set showcmd if has('conceal') set conceallevel=2 endif " }}} " editing {{{ set autoindent set backspace=indent,eol,start set cinoptions+=:0,l1,g0,(0,W1s set expandtab set formatoptions+=j set nojoinspaces set shiftround set shiftwidth=4 set softtabstop=-1 " }}} " command mode {{{ set wildignore+=*.o,.git/*,.svn/*,blib/* set wildmenu set wildmode=longest,list,full if exists("+wildignorecase") set wildignorecase endif " }}} " search {{{ set hlsearch set ignorecase set smartcase " }}} " terminal stuff {{{ set vb t_vb= set ttimeoutlen=50 set ttyfast " }}} " }}} " colors {{{ " general {{{ set background=light set t_Co=256 " }}} " recolorings {{{ highlight Pmenu ctermfg=grey ctermbg=darkblue guifg=grey guibg=darkblue highlight PmenuSel ctermfg=red ctermbg=darkblue guifg=red guibg=darkblue highlight PmenuSbar ctermbg=cyan guibg=cyan highlight PmenuThumb ctermfg=red guifg=red highlight WildMenu ctermfg=grey ctermbg=darkblue guifg=grey guibg=darkblue highlight Folded ctermfg=darkgreen ctermbg=black guifg=green guibg=black highlight Search NONE ctermfg=red guifg=red " }}} " highlight end of line whitespace {{{ autocmd InsertEnter * syn clear EOLWS | syn match EOLWS excludenl /\s\+\%#\@!$/ autocmd InsertLeave * syn clear EOLWS | syn match EOLWS excludenl /\s\+$/ highlight EOLWS ctermbg=red guibg=red " }}} " highlight diff conflict markers {{{ match ErrorMsg '^\(<\||\|=\|>\)\{7\}\([^=].\+\)\?$' " }}} " }}} " bindings {{{ " general {{{ let mapleader = ';' let maplocalleader = ';' " }}} " keep the current selection when indenting {{{ xnoremap < >gv " }}} " M to :make {{{ noremap M :makek " }}} " F11 for spell checking {{{ function! s:spell() if !exists("s:spell_check") || s:spell_check == 0 let s:spell_check = 1 setlocal spell spelllang=en_us else let s:spell_check = 0 setlocal spell spelllang= endif endfunction noremap :call spell() inoremap :call spell() " }}} " arrow keys {{{ noremap gk noremap gj inoremap gk inoremap gj " }}} " editing binary files {{{ nnoremap :%!xxd nnoremap :%!xxd -r " }}} " auto-append closing characters {{{ for pair in [['(', ')'], ['{', '}'], ['[', ']']] exe "inoremap " . pair[0] . " " . pair[0] . pair[1] . "" exe "inoremap " . pair[0] . " " . pair[0] . "" . pair[1] . "O" exe "inoremap " . pair[1] . " strpart(getline('.'), col('.')-1, 1) == '" . pair[1] . "' ? '' : '" . pair[1] . "'" endfor inoremap ' strpart(getline('.'), col('.')-1, 1) == "\'" ? "\" : col('.') == 1 \|\| match(strpart(getline('.'), col('.')-2, 1), '\W') != -1 ? "\'\'\" : "\'" inoremap " strpart(getline('.'), col('.')-1, 1) == "\"" ? "\" : "\"\"\" " }}} " tab for completion {{{ inoremap strpart(getline('.'), 0, col('.')-1) =~ '^\s*$' ? "\" : "\" inoremap " }}} " easier tag traversal {{{ nnoremap nnoremap autocmd CmdWinEnter * nnoremap " }}} " buffer switching {{{ nnoremap H :bp nnoremap L :bn " }}} " fixups for my keyboard remappings {{{ nmap ) 0 nmap g) g0 nmap \ " }}} " miscellaneous {{{ nnoremap e c nnoremap E C nnoremap r nnoremap Y y$ nnoremap , : xnoremap , : nnoremap ! :! xnoremap ! :! autocmd BufEnter * exe "nnoremap T :e " . expand('%') nnoremap / :nohl nnoremap :w nnoremap \ :q nnoremap :bd " }}} " }}} " hooks {{{ " When editing a file, always jump to the last cursor position {{{ autocmd BufReadPost * \ if &filetype != 'gitcommit' && line("'\"") > 0 && line("'\"") <= line("$") | \ exe "normal g`\"" | \ endif " }}} " Prompt to create directories if they don't exist {{{ autocmd BufNewFile * :call ensure_dir_exists() function! s:ensure_dir_exists () let required_dir = expand("%:h") if !isdirectory(required_dir) call ask_quit("Directory '" . required_dir . "' doesn't exist.", "&Create it?") try call mkdir( required_dir, 'p' ) catch call ask_quit("Can't create '" . required_dir . "'", "&Continue anyway?") endtry endif endfunction function! s:ask_quit (msg, proposed_action) if confirm(a:msg, "&Quit?\n" . a:proposed_action) == 1 exit endif endfunction " }}} " }}} " plugin configuration {{{ " ale {{{ let g:ale_lint_on_text_changed = 'normal' let g:ale_lint_on_insert_leave = 1 let g:ale_history_enabled = 0 let g:ale_history_log_output = 0 " }}} " airline " commentary {{{ map x :Commentary " }}} " denite {{{ autocmd VimEnter * call denite#custom#map('insert', '', '') autocmd VimEnter * call denite#custom#map('insert', '', '') if executable('ag') autocmd VimEnter * call denite#custom#var('file_rec', 'command', ['ag', '--hidden', '-l', '.']) autocmd VimEnter * call denite#custom#var('grep', 'command', ['ag']) autocmd VimEnter * call denite#custom#var('grep', 'default_opts', ['--hidden']) autocmd VimEnter * call denite#custom#var('grep', 'recursive_opts', []) autocmd VimEnter * call denite#custom#var('grep', 'pattern_opt', []) autocmd VimEnter * call denite#custom#var('grep', 'separator', []) endif nnoremap t :Denite -direction=dynamictop buffer file_rec nnoremap b :Denite -direction=dynamictop buffer nnoremap ff :Denite -direction=dynamictop grep:.::! nnoremap fh :Denite -direction=dynamictop help nnoremap ft :Denite -direction=dynamictop filetype nnoremap f/ :Denite -direction=dynamictop line " }}} " ft-bzl " git " go " gundo {{{ if has("python") nnoremap U :silent GundoToggle let g:gundo_help = 0 let g:gundo_preview_bottom = 1 else let g:gundo_disable = 1 endif " }}} " ledger " multiple-cursors " neosnippet {{{ let g:neosnippet#snippets_directory = '~/.vim/snippets' let g:neosnippet#disable_runtime_snippets = { '_' : 1 } let i_tab = maparg("", "i", 0, 1) let i_stab = maparg("", "i", 0, 1) let s_tab = maparg("", "s", 0, 1) imap \ neosnippet#expandable_or_jumpable() ? \ "\(neosnippet_expand_or_jump)" : \ i_tab["expr"] ? eval(i_tab["rhs"]) : eval("\"\\" . i_tab["rhs"] . "\"") imap \ neosnippet#expandable_or_jumpable() ? \ "\(neosnippet_expand_or_jump)" : \ i_stab["expr"] ? eval(i_stab["rhs"]) : eval("\"\\" . i_stab["rhs"] . "\"") smap \ neosnippet#expandable_or_jumpable() ? \ "\(neosnippet_expand_or_jump)" : \ s_tab["expr"] ? eval(s_tab["rhs"]) : eval("\"\\" . s_tab["rhs"] . "\"") " }}} " perl " puppet " rainbow {{{ let g:rainbow = 1 let g:rainbow_paren = 1 let g:rainbow_brace = 1 " }}} " rust " startify {{{ let g:startify_list_order = ['dir', 'bookmarks', 'commands'] let g:startify_files_number = 7 let g:startify_commands = [ \ {'t': ['Open file', 'Denite -direction=dynamictop buffer file_rec']}, \ {'ff': ['Grep', 'Denite -direction=dynamictop grep:.::!']}, \ {'fh': ['Help', 'Denite -direction=dynamictop help']}, \ ] let g:startify_change_to_vcs_root = 1 let g:startify_custom_indices = [ \'!', '@', '#', '$', '%', '^', '&', '*', '(', ')' \] let g:startify_custom_header = [] let fortune = system('fortune -n200 -s ~/.fortune | grep -v -E "^$"') let g:startify_custom_footer = [''] + map(split(fortune, '\n'), '" ".v:val') let g:startify_skiplist = ['^/usr/share/vim', '/.git/'] for file in [ '.gitignore', expand('~/.gitignore') ] if filereadable(file) for line in readfile(file) let line = substitute(line, '#.*', '', '') if line != '' && line[0] != '!' let line = substitute(line, "[~.]", "\\\\&", 'g') let line = substitute(line, "\\*\\*", ".*", 'g') let line = substitute(line, "\\*", "[^/]*", 'g') let line = substitute(line, "?", ".", 'g') call add(g:startify_skiplist, line) endif endfor endif endfor " }}} " terraform {{{ let g:terraform_fmt_on_save = 1 " }}} " textobj {{{ let g:Textobj_defs = [ \['/', 'Textobj_paired', '/'], \['\|', 'Textobj_paired', '\|'], \['f', 'Textobj_fold'], \[',', 'Textobj_arg'], \] " }}} " Load plugins that don't use vim's format {{{ runtime macros/matchit.vim " just loading this directly from the plugin directory fails because language " syntax files override the highlighting " using BufWinEnter because that is run after modelines are run (so it catches " modelines which update highlighting) autocmd BufWinEnter,FileType * runtime plugin/rainbow_paren.vim " }}} " }}} " things that should be plugins {{{ " update zsh history when editing a new file - see 'vim' wrapper in .zshrc {{{ if $SHELL =~ 'zsh' && exists('g:_zsh_hist_fname') let s:initial_files = {} autocmd VimEnter * call init_zsh_hist() autocmd BufNewFile,BufRead * call zsh_hist_append() autocmd BufDelete * call remove_initial_file(expand("")) autocmd VimLeave * call reorder_zsh_hist() function! s:remove_initial_file (file) if has_key(s:initial_files, a:file) unlet s:initial_files[a:file] endif endfunction function! s:get_buffer_list_text () redir => output ls! redir END return output endfunction function! s:get_buffer_list () silent let output = get_buffer_list_text() let buffer_list = [] for buffer_desc in split(output, "\n") let name = bufname(str2nr(buffer_desc)) if name != "" call add(buffer_list, name) endif endfor return buffer_list endfunction function! s:init_zsh_hist () for fname in get_buffer_list() let s:initial_files[fname] = 1 call histadd(":", "e " . fname) endfor call delete(g:_zsh_hist_fname) endfunction function! s:zsh_hist_append () let to_append = expand("%:~:.") " XXX these set buftype too late to be caught by this... " this is broken, but not sure what a better fix is if &buftype == '' && to_append !~ '^\(__Gundo\|Startify\|\[denite\]\)' if !has_key(s:initial_files, to_append) if filereadable(g:_zsh_hist_fname) let hist = readfile(g:_zsh_hist_fname) else let hist = [] endif call add(hist, to_append) call writefile(hist, g:_zsh_hist_fname) endif endif endfunction function! s:reorder_zsh_hist () let current_file = expand("%:~:.") if filereadable(g:_zsh_hist_fname) let hist = readfile(g:_zsh_hist_fname) if !has_key(s:initial_files, current_file) call filter(hist, 'v:val != current_file') endif call add(hist, current_file) call writefile(hist, g:_zsh_hist_fname) endif endfunction endif " }}} " diff between current file and its original state {{{ let s:foldmethod = &foldmethod let s:foldenable = &foldenable let s:diffwindow = 0 function! s:diffstart(read_cmd) if s:diffwindow != 0 return endif let s:foldmethod = &foldmethod let s:foldenable = &foldenable let filetype = &filetype vert new let s:diffwindow = winnr() set bt=nofile try exe a:read_cmd catch /.*/ echohl ErrorMsg echo v:exception echohl NONE call s:diffstop() return endtry let &filetype = filetype diffthis wincmd p diffthis " why does this not happen automatically? normal zM endfunction function! s:diffstop() if s:diffwindow == 0 return endif diffoff! exe s:diffwindow . 'wincmd w' bdelete let &foldmethod = s:foldmethod let &foldenable = s:foldenable if &foldenable normal zv endif let s:diffwindow = 0 endfunction function! s:vcs_orig(file) " XXX: would be nice to use a:file rather than # here... let dir = expand('#:p:h') if filewritable(dir . '/.svn') == 2 return system('svn cat ' . a:file) elseif filewritable(dir . '/CVS') == 2 return system("AFILE=" . a:file . "; MODFILE=`tempfile`; DIFF=`tempfile`; cp $AFILE $MODFILE && cvs diff -u $AFILE > $DIFF; patch -R $MODFILE $DIFF 2>&1 > /dev/null && cat $MODFILE; rm $MODFILE $DIFF") elseif finddir('_darcs', dir . ';') =~ '_darcs' return system('darcs show contents ' . a:file) elseif finddir('.git', dir . ';') =~ '.git' let prefix = system('git rev-parse --show-prefix') let prefix = substitute(prefix, '\n', '', 'g') let cmd = 'git show HEAD:'.prefix.a:file return system(cmd) else throw 'No VCS directory found' endif endfunction nnoremap ds :call diffstart('read # normal ggdd') nnoremap dc :call diffstart('call append(0, split(s:vcs_orig(expand("#:.")), "\n", 1)) normal Gdddd') nnoremap de :call diffstop() " }}} " nopaste {{{ function! s:nopaste(visual) if a:visual silent exe "normal gv:!nopaste\" else let pos = getpos('.') silent exe "normal :%!nopaste\" endif silent normal "+yy let @* = @+ silent undo " can't restore visual selection because that will overwrite "* if !a:visual call setpos('.', pos) endif echo @+ endfunction nnoremap p :call nopaste(0) xnoremap p :call nopaste(1) " }}} " better version of keywordprg {{{ function! Help(visual, iskeyword, command) let iskeyword = &iskeyword for kw in a:iskeyword exe 'set iskeyword+=' . kw endfor if a:visual let oldreg = @a " XXX this seems to not include the end of the selection - why? normal `<"ay`>gv let word = @a let @a = oldreg else let word = expand('') endif let &iskeyword = iskeyword exe &helpheight . 'new' set modifiable exe 'call ' . a:command . '("' . word . '")' try silent %s/\%x1b\[\d\+m//g catch /.*/ endtry try silent %s/.\%x08//g catch /.*/ endtry normal ggdd set bt=nofile set nobuflisted set nomodifiable endfunction function! s:man(word) exe 'silent read! man -Pcat ' . a:word set ft=man endfunction nnoremap K :call Help(0, [], 'man') xnoremap K :call Help(1, [], 'man') " }}} " }}} " vim: fdm=marker