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