~gpanders/dotfiles

ref: 2da1dde5ea566f82ebe6532bc6d3b8670fd77ce8 dotfiles/vim/.vim/vimrc -rw-r--r-- 9.6 KiB
2da1dde5Greg Anders Note 'open' subcommand opens editor in NOTES_DIR when no argument given 1 year, 3 months 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
" Greg Anders (gpanders)'s vimrc <https://github.com/gpanders/dotfiles.git>

" 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  " Allow vim to delete whitespace
  silent! set belloff=all
  set complete-=i
  set display=lastline
  set nofsync
  set formatoptions+=j  " Remove comment character when joining lines
  set history=10000
  set hlsearch
  set incsearch
  set langnoremap
  silent! set nolangremap
  set laststatus=2
  set nrformats-=octal  " Don't interpret numbers starting with 0 as octal
  set ruler
  set sessionoptions-=options  " Don't save options in session files
  silent! set shortmess+=F
  set showcmd
  set sidescroll=1
  set smarttab
  set tabpagemax=50  " Increase maximum number of tab pages allowed
  set tags=./tags;,tags
  set ttimeout
  set ttimeoutlen=50
  set ttyfast
  set viminfo^=!  " Keep and store UPPERCASE global variables
  set wildmenu

  " Use UTF-8
  if has('multi_byte')
    set encoding=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
if has('unix')
  let $VIMHOME = $HOME . '/.vim'
else
  let $VIMHOME = $HOME . '/vimfiles'
endif

" 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 confirm
set cursorline
set expandtab
set hidden
set ignorecase
set lazyredraw
set listchars=tab:>\ ,trail:-,extends:>,precedes:<,nbsp:+
set matchpairs+=<:>
set matchtime=2
set pumheight=10
set scrolloff=2
set shell=bash
set shiftwidth=4
set showmatch
set sidescrolloff=5
set smartcase
set softtabstop=4
set noswapfile
silent! set tagcase=match
set tags^=./.git/tags;
set virtualedit+=block
set wildcharm=<Tab>
set wildignorecase
set wildmode=longest:full,full

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

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

" Neovim specific settings
if has('nvim')
  set inccommand=nosplit
  set backupdir-=.
endif

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

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

" Indent wrapped lines
if has('linebreak')
  set linebreak
  set breakindent
  set showbreak=>\ 
endif

if has('textprop') && has('patch-8.1.1880')
  set completeopt+=popup
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

if filereadable($VIMHOME . '/tags')
  let &tags .= ',' . $VIMHOME . '/tags'
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 a new 'undo point' before undoing entire line
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>ko

" 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)

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-R>=system(input(':!'))<CR>

" }}}

" Normal {{{

" Map Q to gq
noremap Q gq

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

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

" Search for a tag by regex
nnoremap <Space>] :tjump /
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 <Space>gq 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
for s:ch in ['a', 'b', 'l', 'q', 't']
    let s:up = toupper(s:ch)
    exe 'nnoremap <expr> [' . s:ch . ' bracket#left(''' . s:ch . ''')'
    exe 'nnoremap <expr> [' . s:up . ' bracket#left(''' . s:up . ''')'
    exe 'nnoremap <expr> ]' . s:ch . ' bracket#right(''' . s:ch . ''')'
    exe 'nnoremap <expr> ]' . s:up . ' bracket#right(''' . s:up . ''')'
endfor
unlet s:ch s:up

nnoremap <expr> [e ":\<C-U>.move --" . v:count1 . "\<CR>"
nnoremap <expr> ]e ":\<C-U>.move +" . v:count1 . "\<CR>"

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>"

" }}}

" Terminal {{{

" Make neovim terminal behave like the vim terminal
if has('nvim')
  tnoremap <C-W> <C-\><C-N><C-W>
  tnoremap <C-W>: <C-\><C-N>:
  tnoremap <C-W>. <C-W>
  tnoremap <C-W><C-\> <C-\>
  tnoremap <C-W>N <C-\><C-N>
  tnoremap <expr> <C-W>" '<C-\><C-N>"' . nr2char(getchar()) . 'pi'
  tnoremap <C-W><C-C> <C-D>
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

cnoremap <expr> <Tab> cmdline#expand("\<Tab>")
" }}}

" }}}

" 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 mark H
autocmd BufLeave *.{c,cc,cpp} mark C
autocmd BufLeave *.py mark P

" 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
let g:trailing_whitespace_blacklist = ['markdown', 'text']
autocmd Syntax * if !&readonly && &modifiable && &filetype != '' && index(g:trailing_whitespace_blacklist, &filetype) == -1
            \ | syn match TrailingWhitespace /\\\@<!\s\+\%#\@<!$/ containedin=ALL | 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
" }}}

" vim: fdl=0