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