]> Tony Duckles's Git Repositories (git.nynim.org) - dotfiles.git/blob - .vimrc
.bashrc: Add docker-related helper aliases
[dotfiles.git] / .vimrc
1 " ---------------------------------------------------------------------------
2 " General
3 " ---------------------------------------------------------------------------
4
5 set nocompatible " essential
6 set history=1000 " lots of command line history
7 set confirm " error files / jumping
8 set fileformats=unix,dos,mac " support these files
9 set iskeyword+=_,$,@,%,#,- " none word dividers
10 set viminfo='1000,f1,:100,@100,/20
11 set modeline " make sure modeline support is enabled
12 set autoread " reload files (no local changes only)
13
14 " ---------------------------------------------------------------------------
15 " Plugins
16 " ---------------------------------------------------------------------------
17
18 call plug#begin('~/.vim/plugged')
19
20 " Colors
21 Plug 'tonyduckles/vim-colorschemes'
22
23 " Interface
24 Plug 'airblade/vim-gitgutter' " shows git diff in the gutter
25 Plug 'farmergreg/vim-lastplace' " intelligently reopen files at your last edit position
26 Plug 'godlygeek/csapprox' " make gui-only colorschemes work automagically in terminal vim
27 Plug 'junegunn/goyo.vim' " distraction-free writing
28 Plug 'junegunn/limelight.vim' " hyperfocus-writing
29 Plug 'kien/ctrlp.vim' " fuzzy file, buffer, mru, etc finder
30 Plug 'scrooloose/nerdtree', { 'on': 'NERDTreeToggle' } " file system explorer
31 Plug 'sjl/gundo.vim' " visualize undo tree
32 Plug 'vim-airline/vim-airline' " lean & mean status/tabline
33 Plug 'vim-airline/vim-airline-themes' " themes for vim-airline
34
35 " Integrations
36 Plug 'ludovicchabant/vim-gutentags' " automatic ctags
37 Plug 'mileszs/ack.vim' " Ack wrapper
38 Plug 'tpope/vim-fugitive' " Git wrappers
39
40 " Edit
41 Plug 'tpope/vim-surround' " quoting/parenthesizing made simple
42 Plug 'tpope/vim-unimpaired' " pairs of handy bracket mappings
43
44 " Language / Syntax
45 Plug 'tpope/vim-git' " filetype=gitcommit
46 Plug 'vim-pandoc/vim-pandoc'
47 Plug 'vim-pandoc/vim-pandoc-syntax' " filetype=markdown
48 Plug '~/.vim/bundle/mumps' " filetype=mumps
49
50 " Other
51 Plug 'kopischke/vim-fetch' " handle opening filenames with line+column numbers
52 Plug 'tpope/vim-scriptease' " misc collection of helper commands
53 Plug '~/.vim/bundle/airlinetheme-map' " override AirlineTheme for certain colorscheme's
54 Plug '~/.vim/bundle/autofolds' " folds for my *rc files
55
56 call plug#end()
57
58 " Automatic install
59 if empty(glob('~/.vim/plugged'))
60 autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
61 endif
62
63 " ----------------------------------------------------------------------------
64 " Plugin Settings
65 " ----------------------------------------------------------------------------
66
67 " NERDTree
68 let NERDTreeShowHidden=1 " show dotfiles by default
69 let NERDTreeMinimalUI=1 " disable 'Press ? for help' text
70
71 " Ctrl-P
72 let g:ctrlp_map = ''
73 let g:ctrlp_show_hidden = 1
74 let g:ctrlp_cache_dir = $HOME.'/.vim/.cache/ctrlp'
75 let g:ctrlp_user_command = ['.git', 'cd %s && git ls-files . -co --exclude-standard', 'find %s -type f']
76
77 " Airline
78 let g:airline_mode_map = {
79 \ '__' : '-',
80 \ 'n' : 'N',
81 \ 'i' : 'I',
82 \ 'R' : 'R',
83 \ 'c' : 'C',
84 \ 'v' : 'V',
85 \ 'V' : 'V',
86 \ '\16' : 'V',
87 \ 's' : 'S',
88 \ 'S' : 'S',
89 \ '\13' : 'S',
90 \ }
91 if !exists('g:airline_symbols')
92 let g:airline_symbols = {}
93 endif
94 if has("gui_running")
95 let g:airline_powerline_fonts = 1 " use powerline font symbols
96 else
97 let g:airline_symbols_ascii = 1 " use plain ascii symbols
98 let g:airline_symbols.branch = ''
99 endif
100 let g:airline_symbols.dirty = '*'
101 let g:airline_symbols.linenr = ''
102 let g:airline_symbols.maxlinenr = ''
103 let g:airline_symbols.notexists = '?'
104
105 " gitgutter
106 let g:gitgutter_enabled = 0 " disable by default
107
108 " goyo + limelight
109 let g:goyo_width = 80
110 let g:limelight_conceal_ctermfg = 240
111 let g:limelight_default_coefficient = 0.6
112 let g:limelight_paragraph_span = 1 " show adjacent paragraphs
113 let g:limelight_priority = -1 " don't overrule hlsearch
114
115 function! s:goyo_enter()
116 if exists('$TMUX')
117 silent !tmux list-panes -F '\#F' | grep -q Z || tmux resize-pane -Z
118 endif
119 set scrolloff=999
120 Limelight
121 endfunction
122
123 function! s:goyo_leave()
124 if exists('$TMUX')
125 silent !tmux list-panes -F '\#F' | grep -q Z && tmux resize-pane -Z
126 endif
127 set scrolloff=4
128 Limelight!
129 endfunction
130
131 autocmd! User GoyoEnter nested call <SID>goyo_enter()
132 autocmd! User GoyoLeave nested call <SID>goyo_leave()
133
134 " pandoc
135 let g:pandoc#syntax#conceal#use = 0 " disable conceal pretty display
136 let g:pandoc#folding#fdc = 0 " disable foldcolumn
137 let g:pandoc#folding#level = 6 " open all folds by default
138
139 " gutentags
140 let g:gutentags_enabled_user_func = 'GutentagsCheckEnabled'
141 function! GutentagsCheckEnabled(file)
142 let file_path = fnamemodify(a:file, ':p:h')
143
144 " enable gutentags if ctags file already exists
145 " tip: `touch tags` in project root to enable gutentags auto-updating
146 try
147 let gutentags_root = gutentags#get_project_root(file_path)
148 if filereadable(gutentags_root . '/tags')
149 return 1
150 endif
151 catch
152 endtry
153
154 return 0
155 endfunction
156
157 " ---------------------------------------------------------------------------
158 " Terminal Settings
159 " ---------------------------------------------------------------------------
160
161 if !has("gui_running") && !(&term =~ '256color')
162 if has("terminfo") && ! (&term == 'linux' || &term == 'Eterm' || &term == 'vt220' || &term == 'nsterm-16color' || &term == 'xterm-16color')
163 " Force these terminals to accept my authority! (default)
164 set t_Co=16
165 set t_AB=\e[%?%p1%{8}%<%t%p1%{40}%+%e%p1%{92}%+%;%dm " ANSI background
166 set t_AF=\e[%?%p1%{8}%<%t%p1%{30}%+%e%p1%{82}%+%;%dm " ANSI foreground
167 elseif &term == 'term' || &term == 'rxvt' || &term == 'vt100' || &term == 'screen'
168 " Less-Cool Terminals (no terminfo)
169 set t_Co=16
170 set t_AB=\e[%?%p1%{8}%<%t4%p1%d%e%p1%{32}%+%d;1%;m
171 set t_AF=\e[%?%p1%{8}%<%t3%p1%d%e%p1%{22}%+%d;1%;m
172 else
173 " Terminals that have trustworthy terminfo entries
174 if &term == 'vt220'
175 set t_Co=8
176 set t_Sf=\e[3%dm " foreground
177 set t_Sb=\e[4%dm " background
178 elseif $TERM == 'xterm'
179 set term=xterm-color
180 endif
181 endif
182 endif
183
184 " ---------------------------------------------------------------------------
185 " Colors / Theme
186 " ---------------------------------------------------------------------------
187
188 if &t_Co > 2 || has("gui_running")
189 set background=dark " dark background
190 syntax enable " syntax highligting
191
192 " override colors in solarized colorscheme
193 augroup vimrc_colorscheme_solarized
194 autocmd!
195 if !has("gui_running")
196 " override Normal ctermfg
197 autocmd ColorScheme solarized highlight Normal ctermfg=None
198 " override Comment ctermfg
199 if &t_Co == 256
200 autocmd ColorScheme solarized highlight Comment ctermfg=241
201 else
202 autocmd ColorScheme solarized highlight Comment ctermfg=DarkGrey
203 endif
204 " override visual block
205 autocmd ColorScheme solarized highlight Visual term=reverse cterm=reverse ctermfg=DarkGreen ctermbg=White
206 if &t_Co < 256
207 " override unprintable chars (listchars)
208 autocmd ColorScheme solarized highlight SpecialKey ctermfg=DarkGrey ctermbg=Black
209 endif
210 endif
211 augroup END
212
213 " colorscheme theme options
214 let g:solarized_termcolors=&t_Co " use 256 colors for solarized
215 let g:solarized_termtrans=1
216
217 if !exists('s:set_colorscheme')
218 colorscheme solarized
219 let s:set_colorscheme=1
220 endif
221
222 " airline theme options
223 let g:solarized16_termcolors=16 " always use 16 colors for 'solarized16' vim-airline theme
224 let g:colorscheme_airlinetheme_map = {
225 \ 'seoul256-light': 'zenburn',
226 \ 'solarized': 'solarized16',
227 \ }
228 let g:colorscheme_airlinetheme_default = 'distinguished'
229
230 endif
231
232 " ----------------------------------------------------------------------------
233 " Backups
234 " ----------------------------------------------------------------------------
235
236 set nobackup " do not keep backups after close
237 set nowritebackup " do not keep a backup while working
238 set backupcopy=yes " keep attributes of original file
239 set backupskip=/tmp/*,$TMPDIR/*,$TMP/*,$TEMP/*
240 set directory=.,~/tmp,~/.vim/swap " swap file directory-order
241 set updatetime=1000 " idle time before writing swap file to disk
242
243 " ----------------------------------------------------------------------------
244 " UI
245 " ----------------------------------------------------------------------------
246
247 set ruler " show the cursor position all the time
248 set showcmd " display incomplete commands
249 set nolazyredraw " turn off lazy redraw
250 set hidden " keep buffers loaded when hidden
251 set showmode " show mode at bottom of screen
252 set wildmenu " turn on wild menu
253 set wildmode=list:longest,full
254 set cmdheight=2 " command line height
255 set backspace=2 " allow backspacing over everything in insert mode
256 set whichwrap+=<,>,h,l,[,] " backspace and cursor keys wrap to
257 set shortmess=filtIoO " shorten messages
258 set report=0 " tell us about changes
259 set nostartofline " don't jump to the start of line when scrolling
260 set scrolloff=4 " vertical padding
261 set sidescroll=40 " side-scrolling increment (for nowrap mode)
262 set sidescrolloff=10 " horz padding
263 set tabpagemax=15 " open 15 tabs max
264 set splitright " put new vsplit windows to the right of the current
265
266 " ----------------------------------------------------------------------------
267 " Visual Cues
268 " ----------------------------------------------------------------------------
269
270 set showmatch " brackets/braces that is
271 set matchtime=5 " duration to show matching brace (1/10 sec)
272 set incsearch " do incremental searching
273 set laststatus=2 " always show the status line
274 set ignorecase " ignore case when searching
275 set smartcase " case-sensitive if search contains an uppercase character
276 set visualbell " shut the heck up
277 set hlsearch " highlight all search matches
278 set list listchars=trail:·,tab:▸\ ,precedes:<,extends:> " show trailing whitespace and tab chars
279
280 " ----------------------------------------------------------------------------
281 " Status Line
282 " ----------------------------------------------------------------------------
283
284 if !exists(':AirlineTheme')
285 set statusline=%1*\ %n%0*\ %<%f " buffer #, filename
286 set statusline+=\ %h%m%r " file-state flags
287 set statusline+=%= " left-right divider
288 set statusline+=%{strlen(&fenc)?&fenc:&enc},%{&ff}\ %y " file-encoding, format, type
289 set statusline+=\ %12.(%v,%l/%L%)\ \ %-4P " cursor position, % through file of viewport
290 endif
291
292 " ----------------------------------------------------------------------------
293 " Text Formatting
294 " ----------------------------------------------------------------------------
295
296 set autoindent " automatic indent new lines
297 set smartindent " be smart about it
298 set nowrap " do not wrap lines
299 set softtabstop=2 " yep, two
300 set shiftwidth=2 " ..
301 set tabstop=4
302 set expandtab " expand tabs to spaces
303 set smarttab " smarter softtab handling
304 set formatoptions+=n " support for numbered/bullet lists
305 set textwidth=0 " no line-wrapping by default
306 set virtualedit=block " allow virtual edit in visual block ..
307 set spelllang=en_us " spell-check dictionary
308
309 " ----------------------------------------------------------------------------
310 " Filename Exclusions
311 " ----------------------------------------------------------------------------
312
313 set wildignore+=.hg,.git,.svn " version control directories
314 set wildignore+=*.jpg,*.jpeg,*.bmp,*.gif,*.png " image files
315 set wildignore+=*.o,*.obj,*.exe,*.dll " compiled object files
316 set wildignore+=*.pyc " Python byte code
317 set wildignore+=*.sw? " Vim swap files
318 set wildignore+=.DS_Store " OSX junk
319
320 " ----------------------------------------------------------------------------
321 " Key Mappings
322 " ----------------------------------------------------------------------------
323
324 " movement based on display lines not physical lines (sane movement with wrap turned on)
325 nnoremap j gj
326 nnoremap k gk
327 vnoremap j gj
328 vnoremap k gk
329 nnoremap <Down> gj
330 nnoremap <Up> gk
331 vnoremap <Down> gj
332 vnoremap <Up> gk
333 inoremap <Down> <C-o>gj
334 inoremap <Up> <C-o>gk
335 " do not menu with left / right in command line
336 cnoremap <Left> <Space><BS><Left>
337 cnoremap <Right> <Space><BS><Right>
338
339 " reflow paragraph with Q in normal and visual mode
340 nnoremap Q gqap
341 vnoremap Q gq
342 " remap U to <C-r> for easier redo
343 nnoremap U <C-r>
344 " make Y consistent with C (c$) and D (d$)
345 nnoremap Y y$
346 " disable default vim regex handling for searching
347 nnoremap / /\v
348 vnoremap / /\v
349 " reselect visual block after indent/outdent
350 vnoremap < <gv
351 vnoremap > >gv
352
353 " <Space> to turn off highlighting and clear any message already displayed.
354 nnoremap <silent> <Space> :nohlsearch<Bar>:echo<CR>
355
356 " leader-based keyboard shortcuts
357 let mapleader = ","
358 " CtrlP
359 nmap <leader>b :CtrlPBuffer<CR>
360 nmap <leader>o :CtrlP<CR>
361 nmap <leader>O :CtrlPClearCache<CR>:CtrlP<CR>
362 " NERDTree
363 nmap <leader>d :NERDTreeToggle<CR>
364 nmap <leader>f :NERDTreeFind<CR>
365 " Gundo
366 nmap <leader>u :GundoToggle<CR>
367 " Fugitive (Git)
368 nmap <leader>gb :Gblame<CR>
369 nmap <leader>gs :Gstatus<CR>
370 nmap <leader>gd :Gdiff<CR>
371 nmap <leader>gl :Glog<CR>
372 nmap <leader>gc :Gcommit<CR>
373 nmap <leader>gp :Gpush<CR>
374 " cd to the directory containing the file in the buffer
375 nmap <leader>cd :lcd %:h<CR>
376 " toggle quickfix window
377 function! QuickfixToggle()
378 let wcnt_old = winnr("$")
379 cwindow
380 let wcnt_cur = winnr("$")
381 if wcnt_old == wcnt_cur
382 cclose
383 endif
384 endfunction
385 nmap <leader>q :call QuickfixToggle()<CR>
386 " toggle diffmode for a buffer
387 function! DiffToggle()
388 if &diff
389 diffoff
390 else
391 diffthis
392 endif
393 endfunction
394 nmap <leader>df :call DiffToggle()<CR>
395 " quickly edit/reload vimrc
396 nmap <leader>ev :edit $MYVIMRC<CR>
397 nmap <leader>sv :source $MYVIMRC<CR>
398 " find merge conflict markers
399 nmap <leader>fc <ESC>/\v^[<=>]{7}( .*\|$)<CR>
400 " markdown headings
401 nnoremap <leader>h1 m`yypVr=``
402 nnoremap <leader>h2 m`yypVr-``
403 nnoremap <leader>h3 m`^i### <esc>``4l
404 nnoremap <leader>h4 m`^i#### <esc>``5l
405 nnoremap <leader>h5 m`^i##### <esc>``6l
406 " toggle hlsearch
407 nmap <leader>hls :set hlsearch! hlsearch?<CR>
408 " upper/lower word
409 nmap <leader>wu mQviwU`Q
410 nmap <leader>wl mQviwu`Q
411 " upper/lower first char of word
412 nmap <leader>wU mQgewvU`Q
413 nmap <leader>wL mQgewvu`Q
414 " smart paste - enable paste-mode and paste contents of system clipboard
415 map <leader>p :set paste<CR>o<esc>"*]p:set nopaste<cr>
416 " toggle spell-check
417 nmap <leader>sp :setlocal spell! spell?<CR>
418 " set text wrapping toggles
419 nmap <leader>tw :set wrap! wrap?<CR>
420 " set list-whitespace-chars toggle
421 nmap <leader>ws :set list! list?<CR>
422 " open tag definition in a horz split
423 nmap <leader>tag :split <CR>:exec("tag ".expand("<cword>"))<CR>
424 " gitgutter
425 nmap <leader>ht :GitGutterToggle<CR>
426 nmap <leader>hp <Plug>GitGutterPreviewHunk
427 nmap <leader>hu <Plug>GitGutterUndoHunk
428 nmap <leader>hs <Plug>GitGutterStageHunk
429 " goyo
430 nnoremap <leader>G :Goyo<CR>
431
432 " --------------------------------------------------------------------------
433 " Functions
434 " --------------------------------------------------------------------------
435
436 " :Redir
437 " Run vim command, redirect output to a scratch buffer
438 function! s:redir(cmd)
439 redir => message
440 silent execute a:cmd
441 redir END
442 if empty(message)
443 echoerr "no output"
444 else
445 new
446 setlocal buftype=nofile bufhidden=wipe noswapfile nobuflisted nomodified
447 silent! put=message
448 silent! g/^s*$/d
449 endif
450 endfunction
451 command! -nargs=+ -complete=command Redir call s:redir(<q-args>)
452
453 " :ListLeaders
454 " Make a scratch buffer with all of the leader keybindings.
455 command! ListLeaders :call s:redir('nmap <leader>')
456
457 " :Todo
458 " Use `git grep` to search for to-do comments, add matches to qflist
459 function! s:todo() abort
460 let entries = []
461 for cmd in ['git grep -nI -e TODO -e FIXME -e XXX 2> /dev/null',
462 \ 'grep -rnI -e TODO -e FIXME -e XXX * 2> /dev/null']
463 let lines = split(system(cmd), '\n')
464 if v:shell_error != 0 | continue | endif
465 for line in lines
466 let [fname, lno, text] = matchlist(line, '^\([^:]*\):\([^:]*\):\(.*\)')[1:3]
467 call add(entries, { 'filename': fname, 'lnum': lno, 'text': text })
468 endfor
469 break
470 endfor
471
472 if !empty(entries)
473 call setqflist(entries)
474 copen
475 endif
476 endfunction
477 command! Todo call s:todo()
478
479 " :StripTrailingWhitespace
480 " Strip trailing whitespace
481 function! StripTrailingWhitespace()
482 " preparation: save last search, and cursor position.
483 let _s=@/
484 let l = line(".")
485 let c = col(".")
486 " do the business
487 %s/\s\+$//e
488 " clean up: restore previous search history, and cursor position
489 let @/=_s
490 call cursor(l, c)
491 endfunction
492 command! StripTrailingWhitespace call StripTrailingWhitespace()
493
494 " ---------------------------------------------------------------------------
495 " Auto Commands / File Types
496 " ---------------------------------------------------------------------------
497
498 " override Filetype settings
499 augroup vimrc_filetype
500 autocmd!
501 " sh config
502 au Filetype sh,bash setlocal ts=4 sts=4 sw=4 expandtab
503 let g:is_bash = 1
504 " git commit message: enable spell checking
505 au Filetype gitcommit setlocal spell
506 " gitconfig file: use real tabs
507 au Filetype gitconfig setlocal noexpandtab
508 " javascript: don't use cindent
509 au FileType javascript setlocal nocindent
510 " makefiles: use real tabs
511 au FileType make setlocal noexpandtab
512 " autofolds
513 au FileType vim setlocal foldmethod=expr foldexpr=autofolds#foldexpr(v\:lnum) foldtext=autofolds#foldtext() foldlevel=2
514 au FileType sh setlocal foldmethod=expr foldexpr=autofolds#foldexpr(v\:lnum,'sh') foldtext=autofolds#foldtext() foldlevel=2
515 augroup END
516
517 " --------------------------------------------------------------------------
518 " Local Settings
519 " --------------------------------------------------------------------------
520
521 if filereadable(glob("~/.vimrc.local"))
522 source ~/.vimrc.local
523 endif