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