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