-
Notifications
You must be signed in to change notification settings - Fork 2
/
vimrc
524 lines (402 loc) · 13.9 KB
/
vimrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
" vim: ts=2 sts=2 sw=2 expandtab:
"
" Customizations are organized into logical sections. Mappings are organized
" by section. Plugin customizations are located near the bottom.
"
" Thanks:
" @garybernhardt
" @tpope
" @carllerche
" @wycats
" @nelstrom
" @mislav
" @mathiasbynens
" =============================================================================
" Initialization
" =============================================================================
" Clear autocmds
autocmd!
" Use Vim settings, rather than Vi settings (default when a vimrc exists)
set nocompatible
" Load plugins with Pathogen
runtime bundle/pathogen/autoload/pathogen.vim
execute pathogen#infect()
" Enable file type detection and load plugin indent files
filetype plugin indent on
" Load vimrc from current directory and disable unsafe commands in them
set exrc
set secure
" Use UTF-8 without BOM
set encoding=utf-8 nobomb
" Respect modelines in files up to this number of lines
set modeline
set modelines=4
" Set comma as <leader> instead of default backslash
let mapleader=","
" =============================================================================
" Terminal Interaction
" =============================================================================
" Prevent Vim from clearing the scrollback buffer
" http://www.shallowsky.com/linux/noaltscreen.html
set t_ti= t_te=
" Clear PAGER if Vim's Man function is needed
let $PAGER=''
" =============================================================================
" Editing
" =============================================================================
""
"" Whitespace
""
set expandtab " Tab in insert mode will produce spaces
set tabstop=2 " Width of a tab
set shiftwidth=2 " Width of reindent operations and auto indentation
set softtabstop=2 " Set spaces for tab in insert mode
set autoindent " Enable auto indentation
" Backspace over everything in insert mode
set backspace=indent,eol,start
" Invisible characters
"set listchars=tab:▸\ ,nbsp:_
"set listchars=tab:\ \ ,trail:·,eol:¬,nbsp:_,extends:❯,precedes:❮
set listchars=tab:▸\ ,trail:·,eol:¬,nbsp:_,extends:❯,precedes:❮
" Don't show invisible characters (default)
set nolist
" Toggle set list
nnoremap <leader>l :set list!<cr>
""
"" Wrapping
""
set wrap " Enable wrapping
set showbreak=↪\ " Character to precede line wraps
" Always move down and up by display lines instead of real lines
" nnoremap <silent>j gj
" nnoremap <silent>k gk
""
"" Joining
""
" Delete comment character when joining commented lines
if v:version > 703 || v:version == 703 && has("patch541")
set formatoptions+=j
endif
" Use only 1 space after "." when joining lines instead of 2
set nojoinspaces
" Joining with indents is useless - instead join and delete spaces
nnoremap gJ Jdiw
""
"" Other
""
" Set jk to escape in insert mode
inoremap jk <esc>`^
" Don't reset cursor to start of line when moving around
set nostartofline
" Do not jump to the matching bracket upon bracket insert (default)
set noshowmatch
" Insert a hash rocket with <c-l>
imap <c-l> <space>=><space>
" Set <c-j> to underscore in insert mode
inoremap <c-j> _
" Disable number adding/subtracting with <c-a> and <c-x>
nnoremap <c-a> <nop>
nnoremap <c-x> <nop>
" =============================================================================
" Appearance
" =============================================================================
set cursorline " Highlight current line
set scrolloff=5 " Keep more buffer context when scrolling
set showtabline=2 " Always show the tab bar
set cmdheight=1 " Set command line height (default)
set title " Show the filename in the window titlebar
set t_Co=256 " 256 colors
set background=dark " Dark background
syntax on " Enable syntax highlighting
colorscheme monokai " Set the default colorscheme
set noerrorbells " Disable error bells
set shortmess=atI " Don't show the Vim intro message
set number " Show line numbers
""
"" Status Line
""
if has("statusline") && !&cp
set laststatus=2 " windows always have status line
set statusline=%f\ %y\%m\%r " filename [type][modified][readonly]
set stl+=%{fugitive#statusline()} " git via fugitive.vim
" buffer number / buffer count
set stl+=\[b%n/%{len(filter(range(1,bufnr('$')),'buflisted(v:val)'))}\]
set stl+=\ %l/%L[%p%%]\,%v " line/total[%],column
endif
" =============================================================================
" Command Line
" =============================================================================
" Display incomplete commands below the status line
set showcmd
" Default shell and shell syntax
set shell=bash
let g:is_bash=1
" Remember more commands and search history (default: 20)
set history=100
" Set <c-n> and <c-p> to act like Up/Down so will filter command history
" Practical Vim p.69
cnoremap <c-p> <up>
cnoremap <c-n> <down>
" <c-a> jumps to beginning of line to match <c-e>
cnoremap <c-a> <home>
" Open help in a vertical split instead of the default horizontal split
" http://vim.wikia.com/wiki/Replace_a_builtin_command_using_cabbrev
cabbrev h <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'vert h' : 'h')<cr>
cabbrev help <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'vert h' : 'help')<cr>
" Expand %% to current directory
" http://vimcasts.org/e/14
cnoremap %% <C-R>=expand('%:h').'/'<cr>
" Save with sudo and reload
command! WW :execute ':silent w !sudo tee % > /dev/null' | :edit!
""
"" Wildmode
""
" Make tab completion for files/buffers act like bash
set wildmenu
" Use emacs-style tab completion when selecting files, etc
set wildmode=longest,list
" Disable output and VCS files
set wildignore+=*.o,*.out,*.obj,.git,*.rbc,*.rbo,*.class,.svn,*.gem
" Disable archive files
set wildignore+=*.zip,*.tar.gz,*.tar.bz2,*.rar,*.tar.xz
" Ignore bundler and sass cache
set wildignore+=*/vendor/gems/*,*/vendor/cache/*,*/.bundle/*,*/.sass-cache/*
" Ignore rails temporary asset caches
set wildignore+=*/tmp/cache/assets/*/sprockets/*,*/tmp/cache/assets/*/sass/*
" Ignore node modules
set wildignore+=node_modules/*
" Disable temp and backup files
set wildignore+=*.swp,*~,._*
""
"" Search
""
set hlsearch " Highlight searches
set incsearch " Highlight dynamically as pattern is typed
set ignorecase " Make searches case-insensitive...
set smartcase " ...unless they contain at least one uppercase character
set gdefault " Use global search by default
" Clear last search highlighting with enter and clear the command line
function! MapCR()
nnoremap <cr> :nohlsearch<cr>:<backspace>
endfunction
call MapCR()
" Re-highlight last search pattern
nnoremap <leader>hs :set hlsearch<cr>
" =============================================================================
" Buffers
" =============================================================================
" Allow unsaved background buffers and remember marks/undo for them
set hidden
" Jump to the first open window that contains the specified buffer
set switchbuf=useopen
" Auto-reload buffers when files are changed on disk
set autoread
" Toggle current and alternate buffers
nnoremap <leader><leader> <c-^>
" Remember buffer count to show in status line
" au BufAdd * let g:zbuflistcount += 1
" au BufDelete * let g:zbuflistcount -= 1
au VimEnter * call UpdateZBufLC()
function UpdateZBufLC()
let lst = range(1, bufnr('$'))
call filter(lst, 'buflisted(v:val)')
let g:zbuflistcount = len(lst)
endfunction
" =============================================================================
" Windows
" =============================================================================
" Set window width that works with standard 15" screen
set winwidth=80
" Split windows below and right instead of above and left
set splitbelow splitright
" Move around splits with <c-h/j/k/l> - This is now handled by
" tmux_navigator.vim - https://gist.github.com/mislav/5189704
" nnoremap <c-h> <c-w>h
" nnoremap <c-j> <c-w>j
" nnoremap <c-k> <c-w>k
" nnoremap <c-l> <c-w>l
" =============================================================================
" Registers
" =============================================================================
" Use the OS clipboard by default
set clipboard=unnamed
" Copy to X11 primary clipboard
map <leader>y "*y
" Paste from unnamed register and fix indentation
nmap <leader>p pV`]=
" Repeat the last macro in the `q` register
nmap <leader>2 @q
" Delete to the blackhole register
nnoremap <leader>x "_x
nnoremap <leader>d "_dd
" =============================================================================
" Backup
" =============================================================================
" Store temporary files in a central location
"set backup
"set backupdir=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp
"set directory=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp
" Ignore tmp directories for backup
"set backupskip=/tmp/*,/private/tmp/*
" Don't make a backup before overwriting a file
set nobackup
set nowritebackup
" Disable swap files
set updatecount=0
" =============================================================================
" Filetypes and Custom Autocmds
" =============================================================================
augroup vimrcEx
" Clear all autocmds for the current group
autocmd!
" Jump to last cursor position unless it's invalid or in an event handler or
" a git commit
au BufReadPost *
\ if &filetype !~ '^git\c' && line("'\"") > 0 && line("'\"") <= line("$") |
\ exe "normal! g`\"" |
\ endif
" Some file types use real tabs
au FileType {make,gitconfig} set noexpandtab sw=4
" Treat JSON files like JavaScript
au BufNewFile,BufRead *.json setf javascript
" Make Python follow PEP8
au FileType python set sts=4 ts=4 sw=4 tw=79
" Use 4 spaces for Java
au FileType java set sts=4 ts=4 sw=4
" Make sure all markdown files have the correct filetype
au BufRead,BufNewFile *.{md,markdown,mdown,mkd,mkdn,txt} setf markdown
" MultiMarkdown requires 4-space tabs
au FileType markdown set sts=4 ts=4 sw=4
" Use 4-space tabs for apache
au FileType apache set sts=4 ts=4 sw=4
" Leave the return key alone when in command line windows, since it's used
" to run commands there
au! CmdwinEnter * :unmap <cr>
au! CmdwinLeave * :call MapCR()
augroup END
" =============================================================================
" Multipurpose Tab Key
" =============================================================================
" Indent if at the beginning of a line, else do completion
function! InsertTabWrapper()
let col = col('.') - 1
if !col || getline('.')[col - 1] !~ '\k'
return "\<tab>"
else
return "\<c-p>"
endif
endfunction
inoremap <tab> <c-r>=InsertTabWrapper()<cr>
inoremap <s-tab> <c-n>
" =============================================================================
" Open urls in the default browser
" =============================================================================
function! OpenUrl()
let s:uri = matchstr(getline("."), '[a-z]*:\/\/[^ >,;]*')
echo s:uri
if s:uri != ""
silent exec "!open ".shellescape(s:uri, 1)
else
echo "No Url found in line."
endif
endfunction
map <leader>j :call OpenUrl()<cr>
" =============================================================================
" Performance
" =============================================================================
" See :help slow-terminal
" Optimize for fast terminal connections
set ttyfast
" Time out on key codes but not mappings
set notimeout
set ttimeout
set ttimeoutlen=100
" Update syntax highlighting for more lines increased scrolling performance
syntax sync minlines=512
" Don't syntax highlight long lines
set synmaxcol=512
" Don't redraw screen while executing macros, registers
" set lazyredraw
" Maximum number of lines to scroll the screen
" ttyscroll=3
" Jump by more lines when scrolling
" set scrolljump=2
" =============================================================================
" Plugin Settings and Mappings
" =============================================================================
""
"" Fugitive
""
nnoremap <leader>ga :Gwrite<cr>
nnoremap <leader>gb :Gblame<cr>
nnoremap <leader>gc :Gcommit<cr>
nnoremap <leader>gd :Gdiff<cr>
nnoremap <leader>gg :Gstatus<cr>
nnoremap <leader>gl :Glog<cr>
nnoremap <leader>gp :Git push<cr>
nnoremap <leader>gs :Git status -sb<cr>
""
"" Matchit
""
" Enable Matchit to use % to jump between def/end, if/else/end
runtime macros/matchit.vim
""
"" NERDTree
""
" Show hidden files in NERDTree
let NERDTreeShowHidden=1
let NERDTreeShowLineNumbers=1
" Toggle NERDTree
nnoremap <c-n> :NERDTreeToggle<cr>
""
"" Smooth Scroll
""
noremap <silent> <c-u> :call smooth_scroll#up(&scroll, 12, 2)<cr>
noremap <silent> <c-d> :call smooth_scroll#down(&scroll, 14, 2)<cr>
noremap <silent> <c-b> :call smooth_scroll#up(&scroll*2, 22, 4)<cr>
noremap <silent> <c-f> :call smooth_scroll#down(&scroll*2, 24, 4)<cr>
""
"" Surround
""
nmap <leader>` ysiw`
nmap <leader>' ysiw'
" =============================================================================
" Typos, Errors, and Typing Discipline
" =============================================================================
" Overwrite :Q Ex mode and :X encryption
command! W :w
command! Q :q
" http://vim.wikia.com/wiki/Replace_a_builtin_command_using_cabbrev
cabbrev X <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'x' : 'X')<cr>
" Fix common mistypes
cnoremap ww w
cnoremap wW w
cnoremap Ww w
" Don't save files named ":" or ";"
cnoremap w; w
cnoremap W; w
cnoremap x; x
cnoremap X; x
cnoremap w: w
cnoremap W: w
cnoremap x: x
cnoremap X: x
" Don't save files named ")" since this is a common mistake when shifts are
" externally mapped to parentheses
cnoremap w) w
cnoremap W) w
cnoremap x) x
cnoremap X) x
" Disable parentheses in normal mode since they are too easily triggered when
" shifts are externally mapped to parentheses
nnoremap ( <nop>
nnoremap ) <nop>
" Disable arrow keys in normal mode and insert mode
noremap <left> <nop>
noremap <right> <nop>
noremap <up> <nop>
noremap <down> <nop>
inoremap <left> <nop>
inoremap <right> <nop>
inoremap <up> <nop>
inoremap <down> <nop>