~gpanders/dotfiles

ref: 200677f03dea04ce273737952e11eed8900ad338 dotfiles/.vim/vimrc -rw-r--r-- 10.1 KiB
200677f0 — Gregory Anders vim: update guard for popup settings a month 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
" Normalization {{{
if !has('nvim')
  " The following settings are the defaults in nvim. Set them also in vim for
  " a consistent experience between the two
  set autoindent
  set autoread
  set backspace=indent,eol,start
  silent! set belloff=all
  set complete-=i
  set display=lastline
  set nofsync
  set formatoptions+=j
  set history=10000
  set hlsearch
  set incsearch
  set langnoremap
  silent! set nolangremap
  set laststatus=2
  set nrformats-=octal
  set ruler
  set sessionoptions-=options
  silent! set shortmess+=F
  set showcmd
  set sidescroll=1
  set smarttab
  set tabpagemax=50
  set tags=./tags;,tags
  set ttimeout
  set ttimeoutlen=50
  set ttyfast
  set viminfo^=!
  set wildmenu

  if has('multi_byte')
    set encoding=utf-8
    scriptencoding utf-8
  endif

  " Set cursor shape based on mode (:h termcap-cursor-shape)
  " 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"

  let s:datadir = vim#datadir()
  let &backupdir = s:datadir . '/backup//,' . &backupdir
  let &directory = s:datadir . '/swap//,' . &directory
  if has('persistent_undo')
      let &undodir = s:datadir . '/undo//,' . &undodir
  endif

  " Create directories if they don't exist
  if exists('*mkdir')
    let s:dirs = [&backupdir, &directory]
    if has('persistent_undo')
        let s:dirs += [&undodir]
    endif
    for s:opt in s:dirs
      let s:dir = simplify(split(s:opt, ',')[0])
      if !isdirectory(s:dir)
        call mkdir(s:dir, 'p')
      endif
    endfor
    unlet s:dirs s:opt s:dir
  endif
endif
" }}}

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

" Setup {{{
if has('autocmd')
  filetype plugin indent on
endif

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

" Set $VIMHOME
let $VIMHOME = split(&runtimepath, ',')[0]

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

" Colors {{{

" Try to make comments italic
autocmd vimrc ColorScheme * highlight Comment gui=italic cterm=italic

" Load colorscheme
silent! colorscheme base16-eighties

" Highlight trailing whitespace
highlight link TrailingWhitespace Error
" }}}

" Settings {{{
set backup
set backupdir-=.
set confirm
set cursorline
set expandtab
set hidden
set ignorecase
set lazyredraw
set listchars=tab:>\ ,trail:-,extends:>,precedes:<,nbsp:+
set matchpairs+=<:>
set pumheight=10
set scrolloff=2
set shell=/bin/sh
set shiftwidth=4
set showmatch
set sidescrolloff=5
set smartcase
set softtabstop=-1
set noswapfile
silent! set tagcase=match
set virtualedit+=block
set wildcharm=<Tab>
set wildignorecase
set wildmode=longest:full,full

if has('wildignore')
  set wildignore+=*.pyc,__pycache__,.DS_Store,*~,#*#
endif

if has('mouse')
  set mouse=a
endif

if has('folding')
  set foldlevelstart=99
endif

if has('persistent_undo')
  set undofile
endif

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

if has('linebreak')
  set linebreak
  set breakindent
endif

if has('popupwin')
  set completeopt+=popup
  set completepopup=highlight:Pmenu,align:menu,border:off
endif

" Set default path variables to blank defaults
setglobal path=.,,
setglobal include=
setglobal includeexpr=
setglobal define=
setglobal isfname+=@-@

" Use a better grepprg if available
set grepformat^=%f:%l:%c:%m
if executable('rg')
  set grepprg=rg\ --vimgrep\ --smart-case
elseif executable('ag')
  set grepprg=ag\ --vimgrep\ --smart-case
elseif executable('grep')
  set grepprg=grep\ --line-number\ --recursive\ -I\ $*\ *
else
  set grepprg=internal
endif

" Enable histogram diff
if has('nvim-0.3.2') || has('patch-8.1.0360')
  set diffopt=filler,internal,algorithm:histogram,indent-heuristic
endif
" }}}

" Mappings {{{

" Insert {{{

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

" Set undo points when deleting
inoremap <C-U> <C-G>u<C-U>
inoremap <C-W> <C-G>u<C-W>

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

" Copy line above/below (similar to <C-Y>/<C-E> but works on the whole line)
inoremap <expr> <C-G><C-Y> repeat('<C-Y>', len(getline(line('.')-1))-col('.')+1)
inoremap <expr> <C-G><C-E> repeat('<C-E>', len(getline(line('.')-1))-col('.')+1)

" Navigate through popup menu with Tab and S-Tab and use Enter to make a
" selection. If popup menu is not visible, Enter should set an undo point
" before inserting a newline
inoremap <expr> <Tab> pumvisible() ? "\<C-N>" : "\<Tab>"
inoremap <expr> <S-Tab> pumvisible() ? "\<C-P>" : "\<S-Tab>"
inoremap <expr> <CR> pumvisible() ? "\<C-Y>" : "\<C-]><C-G>u\<CR>"

inoremap <C-R>! <C-O>:r !
" }}}

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

" Normal {{{
nnoremap <expr> j (v:count == 0 ? 'gj' : 'j')
nnoremap <expr> k (v:count == 0 ? 'gk' : 'k')
nnoremap ^ g^
nnoremap $ g$
nnoremap 0 g0
nnoremap g^ ^
nnoremap g$ $
nnoremap g0 0

" Set Q and gQ to Nop to prevent accidentally entering command mode
nnoremap Q <Nop>
nnoremap gQ <Nop>

" Write file with <Space>w
nnoremap <Space>w :w<CR>

" List buffers and put :b on the command line
nnoremap <Space>b :ls<CR>:b<Space>
nnoremap <C-W><Space>b :ls<CR>:sb<Space>

" Search for a tag by regex
nnoremap <Space>] :tjump /
nnoremap <C-W><Space>] :stjump /
nnoremap <Space>} :ptjump /

" Find files in current buffer's directory
nnoremap <Space>e :e %:p:h/<Tab>
nnoremap <C-W><Space>e :sp %:p:h/<Tab>

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

" Format whole buffer with formatprg without changing cursor position
" See :h restore-position
nnoremap gq<CR> mzHmygggqG`yzt`z

" Allow [[ and friends to work when opening brace is not in the first column
noremap <silent> [[ ?{<CR>w99[{:let @/ = ''<CR>
noremap <silent> ][ /}<CR>b99]}:let @/ = ''<CR>
noremap <silent> ]] j0?{<CR>w99[{%/{<CR>:let @/ = ''<CR>
noremap <silent> [] k$/}<CR>b99]}%?}<CR>:let @/ = ''<CR>

" Unimpaired style mappings
nnoremap <expr> [a ":\<C-U>" . v:count1 . "prev\<CR>"
nnoremap <expr> ]a ":\<C-U>" . v:count1 . "next\<CR>"
nnoremap <expr> [b ":\<C-U>" . v:count1 . "bprev\<CR>"
nnoremap <expr> ]b ":\<C-U>" . v:count1 . "bnext\<CR>"
nnoremap <expr> [l ":\<C-U>" . v:count1 . "lprev\<CR>"
nnoremap <expr> ]l ":\<C-U>" . v:count1 . "lnext\<CR>"
nnoremap <expr> [q ":\<C-U>" . v:count1 . "cprev\<CR>"
nnoremap <expr> ]q ":\<C-U>" . v:count1 . "cnext\<CR>"
nnoremap <expr> [t ":\<C-U>" . v:count1 . "tprev\<CR>"
nnoremap <expr> ]t ":\<C-U>" . v:count1 . "tnext\<CR>"

nnoremap [A :<C-U>first<CR>
nnoremap ]A :<C-U>last<CR>
nnoremap [B :<C-U>bfirst<CR>
nnoremap ]B :<C-U>blast<CR>
nnoremap [L :<C-U>lfirst<CR>
nnoremap ]L :<C-U>llast<CR>
nnoremap [Q :<C-U>cfirst<CR>
nnoremap ]Q :<C-U>clast<CR>
nnoremap [T :<C-U>tfirst<CR>
nnoremap ]T :<C-U>tlast<CR>

nnoremap <expr> [e ":\<C-U>.move --" . v:count1 . "\<CR>"
nnoremap <expr> ]e ":\<C-U>.move +" . v:count1 . "\<CR>"
xnoremap <expr> [e ":move --" . v:count1 . "\<CR>gv"
xnoremap <expr> ]e ":move +" . (v:count1 + line("'>") - line("'<")) . "\<CR>gv"

nnoremap [<Space> :<C-U>put! =repeat(nr2char(10), v:count1)<CR><CR>:']+1<CR>
nnoremap ]<Space> :<C-U>put =repeat(nr2char(10), v:count1)<CR><CR>:'[-1<CR>

nnoremap <expr> yon ':setlocal ' . (&number ? 'no' : '') . "number\<CR>"
nnoremap <expr> yor ':setlocal ' . (&relativenumber ? 'no' : '') . "relativenumber\<CR>"
nnoremap <expr> yol ':setlocal ' . (&list ? 'no' : '') . "list\<CR>"
nnoremap <expr> yoc ':setlocal ' . (&cursorline ? 'no' : '') . "cursorline\<CR>"
nnoremap <expr> yo<Bar> ':setlocal ' . (&cursorcolumn ? 'no' : '') . "cursorcolumn\<CR>"
nnoremap <expr> yod ':' . (&diff ? 'diffoff' : 'diffthis') . "\<CR>"
nnoremap <expr> yos ':setlocal ' . (&spell ? 'no' : '') . "spell\<CR>"

nnoremap m<CR> :make<CR>
nnoremap m<Space> :make<Space>
nnoremap m? :set makeprg?<CR>

function! Substitute(type, ...) abort
    call feedkeys(":'[,']s/" . expand('<cword>') . "//g\<Left>\<Left>", 'n')
endfunction
nnoremap <silent> gS :set operatorfunc=Substitute<CR>g@

function! Sort(type, ...) abort
    '[,']sort
    call setpos('.', getpos("''"))
endfunction
nnoremap <silent> gs m':set operatorfunc=Sort<CR>g@
xnoremap <silent> gs :sort<CR>

nnoremap g/ :<C-U>g//#<Left><Left>
" }}}

" Terminal {{{
if exists(':tmap')
    tnoremap <Esc> <C-\><C-N>
    tnoremap <C-\><C-W> <C-\><C-N><C-W>
    if has('nvim')
        tnoremap <expr> <C-\><C-R> '<C-\><C-N>"' . nr2char(getchar()) . 'pi'
    else
        tnoremap <C-\><C-R> <C-W>"
        tnoremap <C-W> <C-W>.
    endif
endif
" }}}

" Command {{{
cnoremap <expr> <C-P> wildmenumode() ? "\<C-P>" : "\<Up>"
cnoremap <expr> <C-N> wildmenumode() ? "\<C-N>" : "\<Down>"

if has('nvim') && &wildoptions =~# 'pum'
  cnoremap <expr> <C-J> pumvisible() ? "\<Down>\<Tab>" : "\<C-J>"
  cnoremap <expr> <C-K> pumvisible() ? "\<Up>\<Tab>" : "\<C-K>"
else
  cnoremap <expr> <C-J> wildmenumode() ? "\<Down>\<Tab>" : "\<C-J>"
  cnoremap <expr> <C-K> wildmenumode() ? "\<Up>\<Tab>" : "\<C-K>"
endif
" }}}

" }}}

" Autocommands {{{
augroup vimrc

" Set marks by filetype for quick navigation
autocmd BufLeave *.h mark H
autocmd BufLeave *.{c,cc,cpp} mark C
autocmd BufLeave *.py mark P

" Close preview window with q
autocmd BufWinEnter * if &previewwindow | nnoremap <buffer> q <C-W>q | endif

" Start insert mode when entering terminal
if exists('##TermOpen')
  autocmd TermOpen * startinsert
  autocmd BufEnter term://* startinsert
endif

" Enable omnicompletion from syntax if no other option available
" See :h ft-syntax-omni
if exists('+omnifunc')
  autocmd FileType * if &omnifunc == '' | setlocal omnifunc=syntaxcomplete#Complete | endif
endif

if argc() == 0 && filereadable('Session.vim')
  if v:vim_did_enter
    source Session.vim
  else
    autocmd VimEnter * nested source Session.vim
  endif
endif

augroup END
" }}}