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