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