-
Notifications
You must be signed in to change notification settings - Fork 0
/
dot_vimrc
427 lines (328 loc) · 12.1 KB
/
dot_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
" Necessary for lots of cool vim things
set nocompatible
" Support modelines in files that are open
set modeline
" Show what you are typing as a command
set showcmd
" Allow switching buffers without writing out first
set hidden
" Enable filetype detection, plugins, and syntax highlighting
filetype on
filetype plugin indent on
syntax on
" Go into paste mode to avoid e.g. autoindent
nnoremap <F3> :set paste!<CR>:set paste?<CR>
" Spellcheck (from https://major.io/2022/08/11/migrating-from-vscode-to-vim/)
noremap <silent><leader>S :set spell!<CR>
" Automatically indent new lines
set autoindent
" Uses spaces instead of tabs
set expandtab
set copyindent
set preserveindent
set softtabstop=0
set shiftwidth=4
set tabstop=4
"set cindent
"set cinoptions=(0,u0,U0
" Save to swap every second of idle, instead of 4s
set updatetime=1000
" Allow changing tabbing using F6 & F7
function! ChangeTabbing(newtab)
let &shiftwidth = a:newtab
let &softtabstop = a:newtab
if &shiftwidth == 8
set noexpandtab
else
set expandtab
endif
set softtabstop?
endfunction
nnoremap <F6> :call ChangeTabbing(&shiftwidth - 1)<CR>
nnoremap <F7> :call ChangeTabbing(&shiftwidth + 1)<CR>
nnoremap <F12> :syntax sync fromstart<CR>
" Use tabbing of 2 for yaml
autocmd BufRead,BufNewFile *.yaml,*.yml silent! :call ChangeTabbing(2)
" Don't care about casing when the query is all lowercase
set ignorecase
set smartcase
" Remaps kj/jk combinations in Insert Mode to <ESC>
inoremap kj <Esc>
inoremap kJ <Esc>
inoremap KJ <Esc>
inoremap Kj <Esc>
" Highlight search results
set hlsearch
" Always display a status bar
set laststatus=2
" Retrieves the number of buffers available
" This is used in the statusline after
" From http://superuser.com/questions/345520
" May delete if we don't use it in the statusline
function! NrBufs()
let i = bufnr('$')
let j = 0
while i > 0
if buflisted(i)
let j += 1
endif
let i -= 1
endwhile
return j
endfunction
" Formatting of the status bar
" set statusline=%F%m%r%h%w%<\ {%Y}\ %=[%l,%v][%p%%][%n/%{NrBufs()}]
set statusline=%F%m%r%h%w%<\ {%Y}\ %=[%l,%v][%p%%]
"set statusline=%F%m%r%h%w%<\ {%Y}\ %=%{fugitive#statusline()}[%l,%v][%p%%]
" Start searching as we type
set incsearch
nmap <C-W>e <C-W>1000+
" Show relative line numbers black on grey
set number " Shows absolute line number at cursor pos
set relativenumber " Shows relative line number elsewhere
highlight LineNr ctermfg=black ctermbg=grey
" Characters to use when :set list is turned on to display hidden chars
set listchars=tab:>-,trail:~,extends:>,precedes:<
" Start with list on
set list
" Show/hide hidden characters
nnoremap <F2> :set list!<CR>:set list?<CR>
" Keep cursor within 3 lines of top or bottom line when scrolling
set scrolloff=3
" Show visual mode selection with green background
highlight Visual ctermbg=Green
" This clears highlighting from a previous pattern search
" (But it does NOT clear the search pattern, so 'n' would
" still keep searching!)
nnoremap <silent> <CR> :noh<CR><CR>
" This actually clears the 'last search term' register so
" the advantage is that it does not move the cursor
map <silent> <C-N> :let @/=""<CR>
" Enable mouse support for all modes
set mouse=a
" Buffer switcher
noremap <F5> :buffers<CR>:buffer<Space>
nnoremap <C-J> :bnext<CR>
nnoremap <C-K> :bprev<CR>
nnoremap <C-H> :bd<CR>
nnoremap <C-G>u :MundoToggle<CR>
" Switch between 0, 60, 80, and 92 textwidth
function! ChangeTextWidth()
if &textwidth == 0
set textwidth=60
elseif &textwidth == 60
set textwidth=72
elseif &textwidth == 72
set textwidth=80
elseif &textwidth == 80
set textwidth=92
elseif &textwidth == 92
set textwidth=100
else
set textwidth=0
endif
if &textwidth == 0
set colorcolumn=
else
set colorcolumn=+1
endif
set textwidth?
endfunction
noremap <F4> :call ChangeTextWidth()<CR>
" If we're editing code, automatically turn on the 80 width
autocmd BufNewFile,BufRead *.js,*.java,*.yaml,*.yml set textwidth=80
autocmd BufNewFile,BufRead *.py set textwidth=79
autocmd BufNewFile,BufRead *.bu,*.fcc setf yaml
autocmd BufNewFile,BufRead *.ign setf json
" It defaults to ELF otherwise (wtf??)
autocmd BufNewFile,BufRead *.am setf automake
autocmd BufNewFile,BufRead *.Jenkinsfile,*.jenkinsfile setf groovy
" File types that don't necessarily have an extension
autocmd FileType bash,sh set textwidth=80
autocmd FileType python set textwidth=79
autocmd FileType go set noexpandtab
" Turn on coloured column (won't actually be on unless textwidth != 0)
set colorcolumn=+1
" Turn on/off highlighting of column textwidth+1
nnoremap <leader>l :set colorcolumn=+1<CR>
nnoremap <leader>L :set colorcolumn=<CR>
" Move by screen lines, not by real lines
nnoremap j gj
nnoremap k gk
xnoremap j gj
xnoremap k gk
" Insert new line without exiting normal mode
nnoremap <silent> <leader>o o<ESC>
nnoremap <silent> <leader>O O<ESC>
" Reselect visual block after indent/outdent
xnoremap < <gv
xnoremap > >gv
" Commands to open .vimrc and source it
command! VIMRC :e $MYVIMRC
command! SOURCE source $MYVIMRC
" Make :W analogous to :w
command! W :w
" Allow saving of files as sudo when I forgot to start vim using sudo.
cmap w!! w !sudo tee > /dev/null %
" Remember undo history
set undofile
set undodir=$HOME/.vim/undo
set undolevels=10000
set undoreload=100000
" Make backing up and swapping happen in centralized location
set backup
set backupdir=$HOME/.vim/backup/
set directory=$HOME/.vim/swap/
set writebackup
" Create dirs if they don't already exist
silent !mkdir -p $HOME/.vim/{undo,backup,swap} > /dev/null 2>&1
" Make text 60 chars wide for reports and markdown
autocmd BufRead,BufNewFile *.rpt set textwidth=60
autocmd BufRead,BufNewFile *.md set textwidth=60
autocmd BufRead,BufNewFile *.doc set textwidth=72
" For compabitily with tmux's xterm-style keys
if &term =~ '^screen'
execute "set <xUp>=\e[1;*A"
execute "set <xDown>=\e[1;*B"
execute "set <xRight>=\e[1;*C"
execute "set <xLeft>=\e[1;*D"
endif
" Start with no folding enabled so that when a file is
" opened it's never folded. It will get reenabled as soon as
" we zc a fold.
set nofoldenable
" Allow local projects to override settings
" https://andrew.stwrt.ca/posts/project-specific-vimrc/
set exrc
set secure
" Always put a vsplit on the right of current
set splitright
" Always put an hsplit under current
set splitbelow
" Rebuild cscope database with F9
if filereadable(expand("~/.cscope/cscope.out"))
silent cs add ~/.cscope/cscope.out
endif
nnoremap <silent> <F9> :!cs build<CR>:cs reset<CR><CR>
command GitPermalink execute ":!git permalink %:p " . line(".")
command! Cnext try | cnext | catch | cfirst | endtry
command! Cprev try | cprev | catch | clast | endtry
" Easy quickfix navigation
nnoremap <silent> ]q :Cnext<CR>
nnoremap <silent> [q :Cprev<CR>
" Same for tags
nnoremap <silent> ]t :tn<CR>
nnoremap <silent> [t :tp<CR>
command! Lnext try | lnext | catch | lfirst | endtry
command! Lprev try | lprev | catch | llast | endtry
" And for locations
nnoremap <silent> ]e :Lnext<CR>
nnoremap <silent> [e :Lprev<CR>
" Keep going up dirs looking for a tags file
set tags+=tags;$HOME
" CTRL-space once before C-] --> open tag in vsplit
nmap <C-@><C-]> :vsplit<CR><C-]>
" CTRL-space twice before C-] --> open tag in split (like C-W C-])
nmap <C-@><C-@><C-]> :split<CR><C-]>
" Trim trailing spaces in whole document
nnoremap <silent> <F10> :%s/\s\+$//e<CR>
" The default is "menu,preview", which will open up a
" preview window with extra info when auto-completing in
" insert mode. The annoyance is that it doesn't auto-close,
" and the dropdown menu already has all the info we want
" anyway.
set completeopt=menu
" Automatically re-read modified files from disk instead of asking.
set autoread
" XXX: make conditional on FZF existing
"set rtp+=~/Code/github.com/junegunn/fzf
nnoremap <C-T> :FZF<CR>
function! SetGrepPrg()
let l:bufdir = expand('%:p:h')
let l:gitroot = system("git -C " . l:bufdir . " rev-parse --show-toplevel")
if v:shell_error
" still use --exclude-dir; it might've failed because we don't even
" have git
let &l:grepprg = "grep -nr --color=never --exclude-dir=.git $* " . l:bufdir
else
" XXX: need to set this somewhere else, because the grepprg is already
" set by then... when opening the buffer maybe?
" XXX: use word separation
" XXX: add mapping
let &l:grepprg = "git grep -n --color=never $* -- " . gitroot
endif
endfunction
autocmd BufRead,BufNewFile * call SetGrepPrg()
"if executable("rg")
" command! -nargs=+ Rgv :grep -g !vendor <args>
" command! -nargs=+ Rgw :grep !worktrees <args>
" command! -nargs=+ Rgvw :grep -g !vendor -g !worktrees <args>
"endif
nnoremap <silent> gr :let @/ = expand('<cword>')<CR>:grep! '\b<cword>\b'<CR>:set hlsearch<CR>:cw<CR>:redraw!<CR>
nnoremap <silent> gR :let @/ = expand('<cword>')<CR>:grep '\b<cword>\b'<CR>:set hlsearch<CR>:cw<CR>:redraw!<CR>
nnoremap <silent> gv :let @/ = expand("<cword>")<CR>:grep! '\b<cword>\b' -- :!**/vendor/** :!vendor/**<CR>:set hlsearch<CR>:cw<CR>:redraw!<CR>
" set it to split so that even if no language client installed for consistency
nnoremap <silent> K :split<CR>:exec("tag ".expand("<cword>"))<CR>
" Now plugin configurations
if filereadable(expand("~/.vim/autoload/pathogen.vim"))
let g:pathogen_disabled = []
" EXTENSION: https://github.com/sheerun/vim-polyglot
if isdirectory(expand("~/.vim/bundle/vim-polyglot"))
" Disable YAML because it doesn't highlight e.g. XXX, TODO
" https://github.com/sheerun/vim-polyglot/issues/157
let g:polyglot_disabled = ['yaml']
endif
" EXTENSION: https://github.com/ctrlpvim/ctrlp.vim
if isdirectory(expand("~/.vim/bundle/ctrlp.vim"))
" if a file is already open in a buffer, don't switch to the buffer, open the
" file again in the current buffer
let g:ctrlp_switch_buffer = 0
" default to find buffer mode instead of file
let g:ctrlp_cmd = 'CtrlPBuffer'
" include current file in matches
let g:ctrlp_match_current_file = 1
endif
" EXTENSION: https://github.com/ludovicchabant/vim-gutentags
if isdirectory(expand("~/.vim/bundle/vim-gutentags"))
if !executable("ctags")
" There's a `gutentags_dont_load`, but that doesn't seem to work
let g:gutentags_modules = []
endif
" Only regen tags for files tracked by git
let g:gutentags_file_list_command = 'git ls-files'
let g:gutentags_exclude_project_root = [expand("~/.local/share/chezmoi")]
" Be able to tell when Gutentags is regenerating tags
set statusline+=%{gutentags#statusline('[',']')}
" But force a status line refresh otherwise it won't go away
augroup RefreshStatusLine
autocmd!
autocmd User GutentagsUpdating redrawstatus!
autocmd User GutentagsUpdated redrawstatus!
augroup END
endif
" EXTENSION: https://github.com/w0rp/ale
if isdirectory(expand("~/.vim/bundle/ale"))
" Don't lint when changing the text (it will then only lint
" when we open the file and when we save it).
let g:ale_lint_on_text_changed = 0
let g:ale_lint_on_enter = 1 " default
let g:ale_lint_on_save = 1 " default
let g:ale_completion_enabled = 1
" Only report actual errors
let g:ale_python_pylint_options = '-E'
let g:ale_yaml_yamllint_options = '-d relaxed'
let g:ale_linters = {'c': [], 'cpp': [], 'rust': ['analyzer']}
autocmd FileType rust nnoremap <silent> <C-]> :ALEGoToDefinition<CR>
autocmd FileType rust nnoremap <silent> K :ALEHover<CR>
endif
" EXTENSION: https://github.com/airblade/vim-gitgutter
if isdirectory(expand("~/.vim/bundle/vim-gitgutter"))
" Some sign highlighting customization
highlight GitGutterAdd ctermfg=black ctermbg=green
highlight GitGutterChange ctermfg=black ctermbg=yellow
highlight GitGutterChangeDelete ctermfg=black ctermbg=brown
highlight GitGutterDelete ctermfg=black ctermbg=red
endif
execute pathogen#infect()
execute pathogen#helptags()
endif