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