-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathvimrc
More file actions
519 lines (423 loc) · 15.6 KB
/
vimrc
File metadata and controls
519 lines (423 loc) · 15.6 KB
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
"
" Keith Smiley's (http://keith.so) vimrc, do with what you will
"
"
" This must be first, because it changes other options
if &compatible
set nocompatible
endif
" Source files before plugins
source ~/.vim/before/*.vim
" Plugin setup
filetype off
if !has('nvim')
set packpath^=~/.vimplugins
endif
filetype plugin indent on " Re-enable after setup
syntax enable " Enable vim syntax highlighting as is (enable != on)
" Use space as leader!
let g:mapleader="\<Space>"
" I - Disable the startup message
" a - Shorten some messsages to avoid pressing enter in some cases
" t - Truncate if the message is too wide instead of making you 'hit enter to continue'
set shortmess=Iat
set shell=$SHELL " Set the default shell
set encoding=utf-8 " Set the default encodings just in case $LANG isn't set
set autoindent " Indent the next line matching the previous line
set smartindent " Smart auto-indent when creating a new line
set tabstop=2 " Number of spaces each tab counts for
set shiftwidth=2 " The space << and >> moves the lines
set softtabstop=2 " Number of spaces for some tab operations
set shiftround " Round << and >> to multiples of shiftwidth
set expandtab " Insert spaces instead of actual tabs
set smarttab " Delete entire shiftwidth of tabs when they're inserted
set history=1000 " The number of history items to remember
set backspace=indent,eol,start " Backspace settings
set nostartofline " Keep cursor in the same place after saves
set showcmd " Show command information on the right side of the command line
set isfname-== " Remove characters from filenames for gf
" Create a directory if it doesn't exist yet
function! s:EnsureDirectory(directory)
if !isdirectory(expand(a:directory))
call mkdir(expand(a:directory), 'p')
endif
endfunction
let s:subdir = "vim"
if has('nvim')
let s:subdir = "nvim"
endif
" https://stackoverflow.com/a/15317146/902968
" Save backup files, storage is cheap, losing changes is sad
set backup
let &backupdir=$HOME . '/.tmp/' . s:subdir . '/backup//'
call s:EnsureDirectory(&backupdir)
" Write undo tree to a file to resume from next time the file is opened
if has('persistent_undo')
set undolevels=2000 " The number of undo items to remember
set undofile " Save undo history to files locally
let &undodir=$HOME . '/.tmp/' . s:subdir . '/undo//'
call s:EnsureDirectory(&undodir)
endif
let &directory=$HOME . '/.tmp/' . s:subdir . '/swap//'
call s:EnsureDirectory(&directory)
let &viewdir=$HOME . '/.tmp/' . s:subdir . '/view'
call s:EnsureDirectory(&viewdir)
" On quit reset title
let &titleold=getcwd()
let &background=system("get-appearance")
silent! colorscheme solarized
set ttyfast " Set that we have a fast terminal
set t_Co=256 " Explicitly tell Vim that the terminal supports 256 colors
set lazyredraw " Don't redraw vim in all situations
set synmaxcol=500 " The max number of columns to try and highlight
set noerrorbells " Don't make noise
set autoread " Watch for file changes and auto update
set showmatch " Set show matching parenthesis
set matchtime=2 " The amount of time matches flash
set display=lastline " Display super long wrapped lines
set number " Shows line numbers
set nrformats-=octal " Never use octal notation
set nrformats+=unsigned " Ignore '-' denoting negative numbers
set nojoinspaces " Don't add 2 spaces when using J
set mouse=a " Enable using the mouse if terminal emulator
set mousehide " Hide the mouse on typing
set hlsearch " Highlight search terms
set incsearch " Show searches as you type
set wrap " Softwrap text
set linebreak " Don't wrap in the middle of words
set ignorecase " Ignore case when searching
set smartcase " Ignore case if search is lowercase, otherwise case-sensitive
set title " Change the terminal's title
set updatetime=2000 " Set the time before plugins assume you're not typing
set scrolloff=5 " Lines the cursor is to the edge before scrolling
set sidescrolloff=5 " Same as scrolloff but horizontal
set gdefault " Adds g at the end of substitutions by default
set virtualedit=block " Allow the cursor to move off the side in visual block
set nofoldenable
set foldmethod=indent " Decide where to fold based
set foldnestmax=5 " Set deepest fold to x levels
set exrc " Source local .vimrc files
set secure " Don't load autocmds from local .vimrc files
set tags^=.tags,.git/tags " Add local .tags file
set signcolumn=yes
" https://kinbiko.com/vim/my-shiniest-vim-gems/
" Remove comments when joining lines with J
set formatoptions+=j
" Don't remove % on J
set comments-=:%
" Completion options
set complete=.,w,b,u,t,kspell
set completeopt=menuone,noselect
set wildmenu " Better completion in the CLI
set wildmode=longest:full,full " Completion settings
" Ignore these folders for completions
set wildignore+=.hg,.git,.svn " Version control
set wildignore+=*.jpg,*.bmp,*.gif,*.png,*.jpeg " binary images
set wildignore+=*.o,*.obj,*.exe,*.dll,*.manifest,*.pyc " compiled object files
set wildignore+=*.resolved " package manager lock files
set wildignore+=tags,.tags
" Dictionary for custom words
set dictionary+=/usr/share/dict/words
set spellfile=$HOME/.vim/custom-words.utf-8.add
set spelloptions=camel
" Set mapping and key timeouts
set timeout
set timeoutlen=1000
set ttimeoutlen=100
" Setting to indent wrapped lines
if exists('+breakindent')
set breakindent
set breakindentopt=shift:2
endif
" Check for file specific vim settings in the last 3 lines of the file
set modeline
set modelines=2
" Functions for status line config since these functions aren't loaded
" when the vimrc is sourced
function! CurrentTag(...)
if &ft != 'objc' && exists('g:tagbar_iconchars')
return call('tagbar#currenttag', a:000)
else
return ''
endif
endfunction
function! GitStatus()
" From ':h gitsigns-statusline'
return get(b:,'gitsigns_status','')
" From ':h gitgutter'
" let [a, m, r] = GitGutterGetHunkSummary()
" return printf('+%d ~%d -%d', a, m, r)
endfunction
" Status line setup (without plugins)
set laststatus=2 " Always show the statusline
" Left Side
set statusline=%#IncSearch#%{&paste?'\ \ PASTE\ ':''}%*
set statusline+=\ %f
set statusline+=\ %m
set statusline+=\ %r
set statusline+=%=
" Right Side
" TODO: use treesitter here first if enabled? nvim_treesitter#statusline
set statusline+=%{CurrentTag('%s\ <\ ','','')}
set statusline+=%y
set statusline+=\ %{GitStatus()}
set statusline+=\ \ %P
set statusline+=-%l
set statusline+=-%c
if has('clipboard') " If the feature is available
set clipboard=unnamed " copy to the system clipboard
if has('unnamedplus')
set clipboard+=unnamedplus
endif
endif
nnoremap Q :quit<CR>
" Easier save mapping
nnoremap W :write<CR>
" Disable K
vnoremap K <Nop>
" Reselect visual blocks after movement
vnoremap < <gv
vnoremap > >gv
" Don't copy the contents of an overwritten selection.
vnoremap p P
" Move as expected on wrapped lines
noremap j gj
noremap gj j
noremap <Down> gj
inoremap <Down> <C-o>gj
noremap k gk
noremap gk k
noremap <Up> gk
inoremap <Up> <C-o>gk
nnoremap <C-u> 10k
nnoremap <C-d> 10j
" https://www.reddit.com/r/vim/comments/4jy1mh/slightly_more_subltle_n_and_n_behavior/
" Keep search matches in the middle of the window unless the next match is in
" the same viewport
function! s:NextAndCenter(cmd)
let l:view = winsaveview()
try
execute 'normal! ' . a:cmd
catch /^Vim\%((\a\+)\)\=:E486/
" Fake a 'rethrow' of an exception without causing a 3 line error message
echohl ErrorMsg
echo 'E486: Pattern not found: ' . @/
echohl None
endtry
if l:view.topline != winsaveview().topline
normal! zzzv
endif
endfunction
nnoremap <silent> n :set hlsearch \| call <SID>NextAndCenter('n')<CR>
nnoremap <silent> N :set hlsearch \| call <SID>NextAndCenter('N')<CR>
" Remap capital y to act more like other capital letters
nnoremap Y y$
" Force root permission saves
cnoremap w!! w !sudo tee % >/dev/null
command! -bang Q q<bang>
command! -bang -nargs=* -complete=file W w<bang> <args>
" Change the way splits open by default
set splitbelow
set splitright
" Better movement
nnoremap H ^
vnoremap H ^
" Switch to the last file
nnoremap <leader><leader> <C-^>
" Move to last edit location and put it in the center of the screen
nnoremap <C-o> <C-o>zz
" Remove the last search thus clearing the highlight
" This clears the search register denoted by @/
nnoremap <silent> <leader>4 :let @/=""<CR>
" Don't automatically jump on search
" https://vi.stackexchange.com/a/4055/123
" - enable hlsearch without 'n' (I think '*' does that by default)
" - search case sensitively even though 'ignorecase' is set
" - don't move the cursor, don't jump to the next result
nnoremap <silent> * :set hlsearch \| let @/='\C\<' . expand('<cword>') . '\>'<CR>:let v:searchforward=1<CR>
nnoremap <silent> # :set hlsearch \| let @/='\C\<' . expand('<cword>') . '\>'<CR>:let v:searchforward=0<CR>
" Highlight VCS conflict markers
match ErrorMsg '^\(<\|=\|>\)\{7\}\([^=].\+\)\?$'
augroup trailing_highlight
autocmd!
autocmd BufWinEnter * match trailing_whitespace /\s\+$/
autocmd InsertEnter * match trailing_whitespace /\s\+\%#\@<!$/
autocmd InsertLeave * match trailing_whitespace /\s\+$/
augroup END
hi! link trailing_whitespace ErrorMsg
" Close all the lists
nnoremap <silent> <leader>q :call <SID>CloseLists()<CR>
function! s:CloseLists()
lclose
cclose
pclose
silent! TagbarClose
endfunction
command! ClearWhitespace call s:ClearWhitespace()
function! s:ClearWhitespace()
let l:line = line('.')
let l:column = col('.')
keepjumps silent! %s/\s\+$//e
call cursor(l:line, l:column)
call histdel('search', -1)
endfunction
command! FormatShellCommand call s:FormatShellCommand()
function! s:FormatShellCommand()
let l:line = line('.')
let l:column = col('.')
let l:content = getline('.')
let l:output = systemlist("print -l -- " . l:content)
call setline(".", l:output)
keepjumps silent! 1,$-1s/\n/ \\\r /e
call cursor(l:line, l:column)
call histdel('search', -1)
endfunction
" Fixup my screen
nnoremap U :syntax sync fromstart<CR>:redraw!<CR>
" Running as diff
if &diff
set modifiable
set noreadonly
if tabpagenr('$') == 1
nnoremap ZZ :wqall<CR>
endif
endif
" Jump to next/previous merge conflict marker
nnoremap <silent> ]c /\v^(\<\|\=\|\\|\|\>){7}([^=].+)?$<CR>zz
nnoremap <silent> [c ?\v^(\<\|\=\|\\|\|\>){7}([^=].+)\?$<CR>zz
" Position resume
function! s:PositionRecall()
if &filetype =~? 'gitcommit\|gitrebase'
return
endif
if line("'\"") > 0 && line("'\"") <= line('$')
execute "normal g`\"zz"
endif
endfunction
function! s:SetColorColumn()
if &textwidth == 0
" Set colorcolumn specifically to 80 unless textwidth is set
set colorcolumn=80
else
" Show a line past the text width
set colorcolumn=+1
endif
endfunction
" Window sizes
augroup window_sizes
autocmd!
" Set the minimum window width for vertical splits
autocmd VimEnter * silent! set winwidth=80
autocmd VimEnter * silent! set winminwidth=20
augroup END
augroup theme
autocmd!
autocmd FocusGained * silent! let &background=system("get-appearance")
augroup END
" Various filetype settings
augroup ft_settings
autocmd!
" Comment string settings
if empty(&commentstring) | setlocal commentstring=#\ %s | endif
autocmd FileType c,cpp,go,objc,php setlocal commentstring=//\ %s
" Treat more extensions as .zip files
autocmd BufReadCmd *.aar call zip#Browse(expand("<amatch>"))
autocmd BufReadCmd *.apk call zip#Browse(expand("<amatch>"))
autocmd BufReadCmd *.conda call zip#Browse(expand("<amatch>"))
autocmd BufReadCmd *.ipa call zip#Browse(expand("<amatch>"))
autocmd BufReadCmd *.whl call zip#Browse(expand("<amatch>"))
" Save files on some focus lost events, like switching splits
autocmd BufLeave,FocusLost * silent! wall
" Don't auto insert a comment when using O/o for a newline
autocmd VimEnter,BufRead,FileType * set formatoptions-=o
" Break lines based on 'textwidth' even if the line was longer before
" starting insert mode. This is useful for pasting long lines, and
" then continuing at the end of them.
autocmd VimEnter,BufRead,FileType * set formatoptions-=l
" Set color column based on textwidth setting
autocmd FileType * call s:SetColorColumn()
" Return to the same position you left the file in
autocmd BufRead * call s:PositionRecall()
autocmd FocusGained,BufEnter,CursorHold <buffer> checktime
" Create the binary spell file when opening vim
autocmd VimEnter * execute 'silent mkspell! ' . &spellfile
" Close location list when closing the window it belongs to
autocmd QuitPre * nested if &filetype != 'qf' | silent! lclose | endif
autocmd FileType fugitiveblame nnoremap <buffer> x :call system("git open " . expand("<cword>"))<CR>
autocmd BufNewFile *.sh 0r ~/.vim/templates/skeleton.sh | normal! G
autocmd BufNewFile *.sh let b:is_new = 1
autocmd BufWritePost *.sh
\ if get(b:, 'is_new', 0)
\| silent execute '!chmod +x %'
\| let b:is_new = 0
\| endif
augroup END
" ObjC curly brace error fix
let g:c_no_curly_error = 1
" https://github.com/thoughtbot/dotfiles/pull/471
let g:is_posix = 1
command -nargs=? Gqf silent cexpr systemlist('git diff-qf <args>') | copen
command! -bang Delete
\ let s:file = expand('%:p') |
\ execute 'bdelete!' |
\ if delete(s:file) != 0 |
\ echoerr 'Failed to delete "' . s:file . '"' |
\ endif |
\ unlet s:file
command! -nargs=* Chmod
\ let s:file = expand('%:p') |
\ call system('chmod +x ' . shellescape(s:file)) |
\ if v:shell_error |
\ echoerr 'Failed to chmod +x "' . s:file . '"' |
\ endif |
\ unlet s:file
command! Helptags helptags ALL
cnoremap DIR <c-r>=expand('%:p:h') . '/'<CR>
command! CDC cd %:p:h
function! s:EditBuildFile() abort
let s:start = '%:p:h'
" If you're already editing a BUILD file, jump to the next one up the tree
let s:current = expand('%:t')
if s:current == "BUILD" || s:current == "BUILD.bazel"
let s:start = '%:p:h:h'
endif
let s:dir = expand(s:start)
let s:path = 'nothing'
while v:true
let s:path = s:dir . '/BUILD.bazel'
if filereadable(s:path)
break
endif
let s:path = s:dir . '/BUILD'
if filereadable(s:path)
break
endif
let s:dir = resolve(s:dir . '/..')
if s:dir == "/"
break
endif
endwhile
if filereadable(s:path)
execute 'edit ' . s:path
endif
endfunction
nnoremap <silent> <leader>2 :call <SID>EditBuildFile()<CR>
function! MoveFile(newspec, bang) abort
let s:old = expand('%')
" could be improved:
if (s:old == a:newspec)
return 0
endif
if isdirectory(a:newspec)
let s:new = a:newspec . '/' . fnamemodify(s:old, ':t')
else
let s:new = a:newspec
endif
let s:oldperms = getfperm(s:old)
exe 'sav' . a:bang fnameescape(s:new)
call setfperm(s:new, s:oldperms)
call delete(s:old)
endfunction
command! -nargs=1 -complete=file -bang MoveFile call MoveFile('<args>', '<bang>')
nnoremap <silent> + :let @/ .= '\\|\<'.expand('<cword>').'\>'<cr>:call histadd('search', @/)<cr>n
vnoremap <silent> p :<C-u>let @* = substitute(@*, '\n$', '', '')<CR>gvc<C-R>*<ESC>