~gpanders/dotfiles

ref: 885174d3dacde7705891f7dc7934650f50697c87 dotfiles/vim/.vim/vimrc -rw-r--r-- 16.9 KiB
885174d3Greg Anders Update fzf configuration 3 years ago
                                                                                
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
" Greg Anders (gpanders)'s vimrc <https://github.com/gpanders/dotfiles.git>

" Clear all vimrc autocmds at the beginning
augroup vimrc | autocmd! | augroup END

" Detect OS {{{
if !exists('g:os')
  if has('win64') || has('win32') || has('win16')
    let g:os = "Windows"
  else
    let g:os = substitute(system('uname'), '\n', '', '')
  endif
endif
" }}}

" Set $MYVIMRUNTIME
if g:os ==# 'Windows'
  let $MYVIMRUNTIME = $HOME . '/vimfiles'
else
  let $MYVIMRUNTIME = $HOME . '/.vim'
endif

" Use vim-pathogen if native packages are not supported {{{
if !has('packages')
  " Use vim-pathogen to emulate native packaging behavior
  if empty(glob($MYVIMRUNTIME . '/autoload/pathogen.vim'))
    silent execute '!curl -LSso ' . $MYVIMRUNTIME . '/autoload/pathogen.vim https://tpo.pe/pathogen.vim'
  endif
  silent! execute pathogen#infect('pack/{}/start/{}', 'pack/{}/opt/{}')
endif
" }}}

if has('autocmd')
  filetype plugin indent on
endif

if has('syntax') && !exists('g:syntax_on')
  syntax enable
endif

" Set color scheme {{{
if has('termguicolors')
  let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
  let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
  set termguicolors
else
  let g:onedark_termcolors = 16
endif

" Colorscheme post-setup {{{
" Modify color schemes in this function. This function will be called after the colorscheme is set
" in the ColorScheme autocmd

" Set User1-9 highlight groups for statusline
" These are used by adding %N* to the statusline, where {N} is 1-9
function! s:colors()
  if !exists('g:colors_name')
    finish
  endif
  if g:colors_name ==# 'flattened'
    if &background ==# 'light'
      hi User1 ctermfg=15 ctermbg=11 guifg=#fdf6e3 guibg=#657b83
      hi User2 ctermfg=15 ctermbg=14 guifg=#fdf6e3 guibg=#93a1a1
      hi User3 ctermfg=11 ctermbg=7 guifg=#657b83 guibg=#eee8d5
      hi User4 ctermfg=15 ctermbg=14 guifg=#fdf6e3 guibg=#93a1a1
      hi User5 ctermfg=15 ctermbg=11 guifg=#fdf6e3 guibg=#657b83
      hi link StatuslineModeNormal Normal
      hi StatuslineModeInsert ctermfg=4 guifg=#61afef
      hi StatuslineModeReplace ctermfg=1 guifg=#e06c75
      hi StatuslineModeVisual ctermfg=5 guifg=#c678dd
    else
      hi User1 ctermfg=8 ctermbg=12 guifg=#002b36 guibg=#839496
      hi User2 ctermfg=8 ctermbg=10 guifg=#002b36 guibg=#586e75
      hi User3 ctermfg=12 ctermbg=0 guifg=#839496 guibg=#073642
      hi User4 ctermfg=8 ctermbg=10 guifg=#002b36 guibg=#586e75
      hi User5 ctermfg=8 ctermbg=12 guifg=#002b36 guibg=#839496
      hi link StatuslineModeNormal Normal
      hi StatuslineModeInsert ctermfg=4 guifg=#61afef
      hi StatuslineModeReplace ctermfg=1 guifg=#e06c75
      hi StatuslineModeVisual ctermfg=5 guifg=#c678dd
    endif
  elseif g:colors_name ==# 'base16-eighties'
    hi User1 term=bold,reverse ctermfg=12 ctermbg=11 guifg=#a09f93 guibg=#515151
    hi User2 term=bold,reverse ctermfg=8 ctermbg=0 guifg=#747369 guibg=#2d2d2d
    hi User3 term=bold,reverse ctermfg=8 ctermbg=0 guifg=#747369 guibg=#2d2d2d
    hi User4 term=bold,reverse ctermfg=12 ctermbg=11 guifg=#a09f93 guibg=#515151
    hi User5 term=bold,reverse ctermfg=12 ctermbg=11 guifg=#a09f93 guibg=#515151

    hi link StatuslineModeNormal Normal
    hi StatuslineModeInsert ctermfg=4 guifg=#6699cc
    hi StatuslineModeReplace ctermfg=1 guifg=#f2777a
    hi StatuslineModeVisual ctermfg=5 guifg=#cc99cc

    " These groups look really bad by default, so make them better
    hi SpellBad term=underline cterm=underline ctermfg=1 gui=underline guifg=#f2777a
    hi WildMenu ctermbg=11 ctermfg=15 guibg=#515151 guifg=#f2f0ec
    hi clear Underlined
    hi Underlined term=underline cterm=underline gui=underline
  elseif g:colors_name ==# 'base16-onedark'
    hi User1 cterm=reverse gui=reverse
    hi User2 ctermbg=10 guibg=#353b45
    hi User3 ctermfg=7 guifg=#abb2bf
    hi link User4 User2
    hi User5 cterm=reverse gui=reverse

    hi link StatuslineModeNormal Normal
    hi StatuslineModeInsert ctermfg=4 guifg=#61afef
    hi StatuslineModeReplace ctermfg=1 guifg=#e06c75
    hi StatuslineModeVisual ctermfg=5 guifg=#c678dd
  elseif g:colors_name ==# 'apprentice'
    hi User1 term=bold,reverse ctermfg=235 ctermbg=101 guifg=#262626 guibg=#87875f
    hi User2 ctermfg=242 ctermbg=235 guifg=#6c6c6c guibg=#262626
    hi User3 ctermfg=242 ctermbg=235 guifg=#6c6c6c guibg=#262626
    hi User4 term=bold,reverse ctermfg=235 ctermbg=101 guifg=#262626 guibg=#87875f
    hi User5 term=bold,reverse ctermfg=235 ctermbg=101 guifg=#262626 guibg=#87875f

    hi link StatuslineModeNormal Normal
    hi StatuslineModeInsert ctermfg=4 guifg=#5f87af
    hi StatuslineModeReplace ctermfg=1 guifg=#af5f5f
    hi StatuslineModeVisual ctermfg=5 guifg=#5f5f87
  endif
endfunction

autocmd vimrc ColorScheme * call s:colors()
" }}}
"
" Try to make comments italic
autocmd vimrc ColorScheme * highlight Comment gui=italic cterm=italic

" Load colorscheme
let base16colorspace = 256
silent! colorscheme base16-eighties

" If no other colorscheme, use vim default
if !exists('g:colors_name')
  set bg=dark
endif

" Highlight trailing whitespace
highlight TrailingWhitespace ctermbg=204 guibg=#E06C75

" }}}

" GUI settings {{{
if has('gui_running')
  if g:os ==# 'Windows'
    set guifont=Fira_Mono:h10:cANSI:qDRAFT
  elseif g:os ==# 'Darwin'
    set guifont=FiraMono-Regular:h11
  endif
  set guioptions=teMg!
  set lines=60
  set columns=180
endif
" }}}

" Set curor shape based on mode
if !has('nvim')
  " Vertical bar in insert mode
  let &t_SI = "\e[6 q"
  " Underline in replace mode
  let &t_SR = "\e[4 q"
  " Block in normal mode
  let &t_EI = "\e[2 q"
endif

" Use UTF-8 if we can and env LANG didn't tell us not to
if has('multi_byte') && !exists('$LANG') && &encoding ==# 'latin1'
  set encoding=utf-8
endif

" Sudo save
cnoreabbrev w!! w !sudo tee > /dev/null %

" Settings {{{
" Press K to open help docs for option under the cursor
set autoindent
set autoread
set backup
set confirm
set cursorline
set display+=lastline
set expandtab
set foldmarker=\ {{{,\ }}}
set hidden
set hlsearch
set ignorecase
set include=
set incsearch
set nolangremap
set lazyredraw
set linebreak
set magic
set matchpairs+=<:>
set matchtime=2
set modeline
set nrformats-=octal
set number
set pumheight=10
set scrolloff=2
set shiftwidth=4
set showmatch
set noshowmode
set sidescrolloff=5
set smartcase
set smarttab
set softtabstop=4
set splitbelow
set splitright
set nostartofline
set noswapfile
set tagcase=smart
set virtualedit+=block

" Enable undofile if possible
if has('persistent_undo')
  set undofile
endif

" Color the whole area outside of 'textwidth'
let &colorcolumn = '+' . join(range(1, 256), ',+')

" Indent wrapped lines
silent! set breakindent

" Add more completion options
set completeopt=menu,preview

if !has('nvim') && &ttimeoutlen == -1
  set ttimeout
  set ttimeoutlen=100
endif

" Allow backspacing over all whitespace
set backspace=indent,eol,start

" Specify whitespace chars
set listchars=tab:>\ ,trail:-,extends:>,precedes:<,nbsp:+

" Delete comment characters when joining commented lines
set formatoptions+=j

if has('path_extra')
  setglobal tags-=./tags tags-=./tags; tags^=./tags;
endif

" Increase Vim's command history
set history=1000

" Set max number of tab pages
set tabpagemax=50

" Keep and store UPPERCASE global variables
if !empty(&viminfo)
  set viminfo^=!
endif

" Don't save options in session files
set sessionoptions-=options

set path-=/usr/include     " Let C/C++ filetypes set this
set path+=**               " Search current directory's whole tree

set ruler
set wildmenu
set wildcharm=<C-Z>
set wildmode=longest:full,full
set wildignorecase
set wildignore+=*/.git/**/*,*/.hg/**/*,*/.svn/**/*
set wildignore+=tags
" }}}

" Statusline {{{
" Set the statusline

let s:statusline_separator = ' / '

function! StatuslineLineEnding()
  if &ff ==# 'unix'
    return 'LF' . s:statusline_separator
  elseif &ff ==# 'dos'
    return 'CRLF' . s:statusline_separator
  elseif &ff ==# 'mac'
    return 'CR' . s:statusline_separator
  endif
  return ''
endfunction

function! StatuslineFileType()
  if &ft ==# ''
    return 'none'
  else
    return &ft
  endif
endfunction

function! StatuslineGitBranch()
  if exists('*FugitiveHead')
    let branch = FugitiveHead()
    if branch != ''
      return branch . s:statusline_separator
    endif
  endif
  return ''
endfunction

function! StatuslineMode()
  let m = mode()
  if m ==# 'n'
    highlight! link StatuslineMode StatuslineModeNormal
    return 'N'
  elseif m ==# 'i'
    highlight! link StatuslineMode StatuslineModeInsert
    return 'I'
  elseif m ==# 'R'
    highlight! link StatuslineMode StatuslineModeReplace
    return 'R'
  elseif m ==? 'v' || m ==# ''
    highlight! link StatuslineMode StatuslineModeVisual
    return 'V'
  else
    highlight! link StatuslineMode StatuslineModeNormal
    return toupper(m)
  endif
endfunction

" Initialize StatuslineMode highlight group
highlight! link StatuslineMode StatuslineModeNormal

function! StatuslineFlags()
  let str = ''
  if &mod
    let str .= '+ '
  endif
  if &ro
    let str .= '[RO] '
  endif
  return str
endfunction

set laststatus=2
set statusline=                             | " Reset the statusline
set statusline+=%#StatuslineMode#           |
set statusline+=\ %{StatuslineMode()}\      | " Mode indicator
set statusline+=%*                          |
set statusline+=%1*\ %<%f\                  | " Filename
set statusline+=%{StatuslineFlags()}        | " Help/modified/RO markers
set statusline+=%2*                         | " Filetype
set statusline+=%=                          | " Break point for right justify

set statusline+=%3*                         | " Reset color
set statusline+=%{StatuslineGitBranch()}    | " Git branch (with icon)
set statusline+=%{StatuslineLineEnding()}   | " Line ending
set statusline+=%{StatuslineFileType()}\    | " Filetype
set statusline+=%4*\                      | " Color boundary
set statusline+=%(%l:%c%V%)                 | " Line and column number
set statusline+=\ %5*                       | " Color boundary 5
set statusline+=%P\                         | " Percent through file

" }}}

" Keep temporary files in a single location
if g:os ==# 'Windows'
  let cachedir = $HOME . '/vimfiles/cache'
else
  if exists('$XDG_CACHE_HOME')
    let cachedir = $XDG_CACHE_HOME . '/vim'
  else
    let cachedir = $HOME . '/.cache/vim'
  endif
endif

let &g:backupdir = cachedir . '/backup//' . ',' . &g:backupdir
let &g:undodir = cachedir . '/undo//' . ',' . &g:undodir
let &g:directory = cachedir . '/swap//' . ',' . &g:directory

" Set dictionary if it exists
if g:os !=# 'Windows' && filereadable('/usr/share/dict/words')
  set dictionary+=/usr/share/dict/words
endif

" Insert mode mappings {{{

" jk in Insert mode escapes to Normal mode
inoremap jk <Esc>l

" Set a new 'undo point' before undoing entire line
inoremap <C-U> <C-G>u<C-U>

" Auto close braces in insert mode
inoremap {<CR> {<CR>}<Esc>ko

" Insert current date with <C-\>d
inoremap <silent> <C-\>d <C-R>=strftime('%Y-%m-%d')<CR>

" }}}

" Normal mode mappings {{{

" Save by pressing <leader>w
nnoremap <silent> ,w :w<CR>

" Navigate through wrapped lines individually
nnoremap <silent> j gj
nnoremap <silent> k gk

" Map Q to gq
noremap Q gq

" Buffer shortcuts
nnoremap <silent> ,1 :b1<CR>
nnoremap <silent> ,2 :b2<CR>
nnoremap <silent> ,3 :b3<CR>
nnoremap <silent> ,4 :b4<CR>
nnoremap <silent> ,5 :b5<CR>
nnoremap <silent> ,6 :b6<CR>
nnoremap <silent> ,7 :b7<CR>
nnoremap <silent> ,8 :b8<CR>
nnoremap <silent> ,9 :b9<CR>
nnoremap <silent> ,0 :b10<CR>

" List buffers and put :b on the command line
noremap ,b :buffers<CR>:b<Space>

" Remap & to preserve substitution flags
nnoremap <silent> & :&&<CR>

" <Space><Tab> goes back to previous buffer
nnoremap <silent> <Space><Tab> <C-^>

" Map ,, to ,
nnoremap ,, ,

" ,f prefixes find files in path
nnoremap ,f :find *
nnoremap ,vf :vert sf *
nnoremap ,sf :sf *

" Search for a tag by regex
nnoremap ,t :tjump /
nnoremap ,p :ptjump /

" Find files in current buffer's directory
nnoremap ,e :e <C-R>=expand('%:p:h') . '/'<CR>
nnoremap ,ve :vs <C-R>=expand('%:p:h') . '/'<CR>
nnoremap ,se :sp <C-R>=expand('%:p:h') . '/'<CR>

" <Bslash>e opens .vimrc in new window
nnoremap <Bslash>ev :<C-U>tabe $MYVIMRC<CR>

" <Bslash>s sources .vimrc
nnoremap <Bslash>sv :<C-U>source $MYVIMRC<CR>

" Clear search buffer with <C-L>
nnoremap <silent> <C-L> :<C-U>nohlsearch<Bar>diffupdate<CR><C-L>
vnoremap <silent> <C-L> :<C-U>nohlsearch<Bar>diffupdate<CR>gv<C-L>

" \c changes directory to current buffer's directory
nnoremap <silent> \c :cd %:p:h<Bar>echo 'Changed directory to ' . getcwd()<CR>

" ,c changes local working directory to buffer's directory
nnoremap <silent> ,c :lcd %:p:h<Bar>echo 'Changed directory to ' . getcwd()<CR>

" Format whole buffer with formatprg without changing cursor position
nnoremap <Space>gq mzHmygggqG`yzt`z

" Show tag stack
nnoremap <Space>t :tags<CR>

" Navigate through tag stack
nnoremap <expr> >t ":\<C-U>" . v:count1 . "tag\<CR>"

" This is the same as CTRL-T, but it's nice to have symmetry
nnoremap <expr> <t ":\<C-U>" . v:count1 . "pop\<CR>"

nnoremap <expr> <q ":\<C-U>colder " . v:count1 . "\<CR>"
nnoremap <expr> >q ":\<C-U>cnewer " . v:count1 . "\<CR>"
nnoremap <expr> <l ":\<C-U>lolder " . v:count1 . "\<CR>"
nnoremap <expr> >l ":\<C-U>lnewer " . v:count1 . "\<CR>"

" Show registers
nnoremap <Space>r :registers<CR>

" Show marks
nnoremap <Space>m :marks<CR>:norm! `

" Show only user placed marks (A-Z and a-z)
let s:alphabet = join(map(
      \ range(char2nr('A'), char2nr('Z')) + range(char2nr('a'), char2nr('z')),
      \ 'nr2char(v:val)'), '')
execute 'nnoremap <Space>M :marks ' . s:alphabet . '<CR>'

" }}}

" Visual mode mappings {{{
vnoremap * y/\V<C-R>"<CR>
vnoremap # y?\V<C-R>"<CR>
" }}}

" Terminal mode mappings {{{
if exists(':tmap') == 2
  tnoremap <silent> <C-W> <C-\><C-N><C-W>
  tnoremap <C-W><C-W> <C-W>
endif
" }}}

" Commands {{{

" Grep {{{

" Use ripgrep or ag as grepprg if available
if executable('rg')
  " Use rg over grep
  set grepprg=rg\ --vimgrep\ --smart-case
  set grepformat^=%f:%l:%c:%m
elseif executable('ag')
  " Use ag over grep
  set grepprg=ag\ --vimgrep\ --smart-case
  set grepformat^=%f:%l:%c:%m
endif

" Define a custom command to wrap :grep with :silent, :copen, and :redraw
command! -nargs=+ -bang Grep execute 'silent botright grep<bang> ' . shellescape(<q-args>, 1) | cwindow | redraw!
nnoremap <Bslash><Bslash> :Grep!<space>
" }}}

" This comes from vimrc_example.vim. See :h :DiffOrig
command! DiffOrig vert new | set bt=nofile | r ++edit # | 0d_ | diffthis
      \ | wincmd p | diffthis

" Edit ftplugin file for current filetype
command! EditFtplugin
      \ if empty(&ft) |
      \   echohl Error | echo 'Error: This buffer has no filetype' |
      \ else |
      \   execute 'e ' . $MYVIMRUNTIME . '/after/ftplugin/' . &ft . '.vim' |
      \ endif

" }}}

" Pushing built-in commands beyond their limits {{{
" https://gist.github.com/Konfekt/d8ce5626a48f4e56ecab31a89449f1f0
function! <sid>CCR()
  if getcmdtype() isnot# ':'
    return "\<CR>"
  endif
  let cmdline = getcmdline()
  if cmdline =~# '\v^\s*(ls|files|buffers)!?\s*(\s[+\-=auhx%#]+)?$'
    " like :ls but prompts for a buffer command
    return "\<CR>:b"
  elseif cmdline =~# '\v/(#|nu%[mber])$'
    " like :g//# but prompts for a command
    return "\<CR>:"
  elseif cmdline =~# '\v^\s*(dli%[st]|il%[ist])!?\s+\S'
    " like :dlist or :ilist but prompts for a count for :djump or :ijump
    return "\<CR>:" . cmdline[0] . "j  " . join(split(cmdline, " ")[1:]) . "\<C-B>\<S-Right>\<Right>"
  elseif cmdline =~# '\v^\s*(cli|lli)%[st]!?\s*(\s\d+(,\s*\d+)?)?$'
    " like :clist or :llist but prompts for an error/location number
    return "\<CR>:sil " . repeat(cmdline[0], 2) . "\<Space>"
  elseif cmdline =~# '\v^\s*ol%[dfiles]\s*$'
    " like :oldfiles but prompts for an old file to edit
    set nomore
    return "\<CR>:sil se more|e #<"
  elseif cmdline =~# '^\s*changes\s*$'
    " like :changes but prompts for a change to jump to
    set nomore
    return "\<CR>:sil se more|norm! g;\<S-Left>"
  elseif cmdline =~# '\v^\s*ju%[mps]'
    " like :jumps but prompts for a position to jump to
    set nomore
    return "\<CR>:sil se more|norm! \<C-o>\<S-Left>"
  elseif cmdline =~ '\v^\s*marks\s*(\s\w+)?$'
    " like :marks but prompts for a mark to jump to
    return "\<CR>:norm! `"
  elseif cmdline =~# '\v^\s*undol%[ist]'
    " like :undolist but prompts for a change to undo
    return "\<CR>:u "
  else
    return "\<c-]>\<CR>"
  endif
endfunction
cnoremap <expr> <CR> <sid>CCR()
" }}}

" Autocommands {{{
augroup vimrc
" Have Vim jump to the last position when reopening a file
autocmd BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$")
      \ | exe "normal! g`\"" | endif

" Set marks by filetype for quick navigation
autocmd BufLeave *.h normal! mH
autocmd BufLeave *.c,*.cc,*.cpp normal! mC
autocmd BufLeave *.py normal! mP

" Start insert immediately when terminal opens
if exists('##TermOpen')
  autocmd TermOpen * setlocal nonumber | startinsert
endif

if has('nvim')
  autocmd BufEnter term://* startinsert
endif

" Create syntax group for trailing whitespace in each new buffer
autocmd Syntax * syn match TrailingWhitespace /\s\+\%#\@<!$/ containedin=ALL

augroup END
" }}}

" Enable per-project configuration {{{
set exrc
set secure
" }}}