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