Repository navigation
Expand file tree
/
Copy path.vimrc
More file actions
803 lines (694 loc) · 26.1 KB
/
Copy path.vimrc
File metadata and controls
803 lines (694 loc) · 26.1 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
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
scriptencoding utf-8
set nocompatible
filetype indent on
"call ch_logfile(expand('/tmp/ch.log'), 'w')
" {{{ PLUGINS
" vim-markdown
let g:vim_markdown_folding_disabled = 1
let g:vim_markdown_follow_anchor = 0
let g:vim_markdown_no_extensions_in_markdown = 1
let g:vim_markdown_fenced_languages = ['bash', 'c', 'cpp', 'diff', 'html', 'java', 'javascript', 'json', 'lua', 'ps1', 'python', 'rust', 'sh', 'toml', 'yaml', 'batch=dosbatch', 'bat=dosbatch', 'js=javascript', 'powershell=ps1']
" TODO: Parse buffer and conditional load additional languages
" - https://thoughtbot.com/blog/profiling-vim
"let g:vim_markdown_fenced_languages = ['asm', 'asm68k', 'awk', 'bash', 'basic', 'c', 'cmake', 'conf', 'cpp', 'css', 'diff', 'dot', 'fasm', 'gdb', 'glsl', 'hex', 'html', 'java', 'javascript', 'json', 'log', 'make', 'nasm', 'perl', 'php', 'ps1', 'python', 'r', 'rust', 'scala', 'sh', 'sql', 'strace', 'svg', 'toml', 'typescript', 'vim', 'xml', 'xxd', 'yaml', 'batch=dosbatch', 'bat=dosbatch', 'csharp=cs', 'js=javascript', 'powershell=ps1']
let g:markdown_fenced_languages = []
augroup markdown_fenced_languages_group
autocmd!
autocmd BufNewFile,BufRead *.{md,mdx,mdown,mkd,mkdn,mkdown,markdown}
\ let g:markdown_fenced_languages = g:vim_markdown_fenced_languages
augroup END
" vim-polyglot
let g:polyglot_disabled = ['md', 'markdown']
set runtimepath+=~/vimfiles
if filereadable(expand('~/.vim/autoload/pathogen.vim'))
call pathogen#infect()
endif
" FZF
let g:fzf_colors = {
\ 'fg': ['fg', 'Normal'],
\ 'bg': ['bg', 'Normal'],
\ 'hl': ['fg', 'Comment'],
\ 'fg+': ['fg', 'CursorLine', 'CursorColumn', 'Normal'],
\ 'bg+': ['bg', 'CursorLine', 'CursorColumn'],
\ 'hl+': ['fg', 'Statement'],
\ 'info': ['fg', 'PreProc'],
\ 'border': ['fg', 'Ignore'],
\ 'prompt': ['fg', 'Conditional'],
\ 'pointer': ['fg', 'Exception'],
\ 'marker': ['fg', 'Keyword'],
\ 'spinner': ['fg', 'Label'],
\ 'header': ['fg', 'Comment'],
\ }
let g:fzf_launcher='urxvt -e bash -ic %s'
let g:fzf_layout = { 'window': { 'width': 1, 'height': 0.5, 'yoffset': 1, 'border': 'horizontal' } }
set runtimepath+=~/opt/fzf
" Ale
nmap <F3>] :ALEGoToDefinition<CR>
nmap <F3>f :ALEFix<CR>
nmap <F3>h :ALEHover<CR>
nmap <F3>r :ALEFindReferences<CR>
nmap <F3>t :ALEGoToTypeDefinition<CR>
"augroup ale_group
" autocmd!
" autocmd CursorMoved,CursorHold * if &filetype !=# 'java' && exists('*ale#engine#Cleanup') && mode() == 'n' | ALEHover | endif
"augroup END
let g:ale_hover_to_preview = 1
let g:ale_virtualtext_cursor = 'disabled'
let g:ale_cache_executable_check_failures = 1
let g:ale_completion_enabled = 1
let g:ale_completion_autoimport = 1
let g:ale_history_log_output = 1
let g:ale_keep_list_window_open = 1
let g:ale_lint_on_text_changed = 'never'
let g:ale_list_window_size = 6
let b:ale_warn_about_trailing_whitespace = 0
" TODO: add linters
" - asm (uses `gcc -x assembler` but too slow on large listings)
" - typescript-language-server
" - 'java': ['eclipselsp', 'javac'],
" TODO: fix scala completions
" - let g:ale_scala_metals_project_root = '.'
let g:ale_fixers = {
\ 'c': ['clangtidy'],
\ 'cpp': ['clangtidy'],
\ 'javascript': ['eslint'],
\ }
let g:ale_linters = {
\ 'asm': [],
\ 'go': ['gopls'],
\ 'java': ['javac'],
\ 'javascript': ['tsserver', 'eslint'],
\ 'python': ['ty', 'pylsp'],
\ 'rust': ['analyzer'],
\ 'scala': ['metals'],
\ 'sh': ['shellcheck'],
\ }
let g:ale_linter_alias = {
\ 'jsx': ['css', 'javascript'],
\ 'xsd': ['xsd', 'xml'],
\ 'xslt': ['xslt', 'xml'],
\ }
let g:ale_pattern_options = {
\ '\.min\.css$': {'ale_linters': [], 'ale_fixers': []},
\ '\.min\.js$': {'ale_linters': [], 'ale_fixers': []},
\}
" Debugging:
" all:
" :call ch_logfile(expand('~/tmp/channel.log'), 'w')
" c/cpp:
" bin/compile_commands.json
" java:
" project/build.gradle
" :echo ale#java#FindProjectRoot(bufnr(''))
let g:ale_c_parse_compile_commands = 1
let g:ale_css_csslint_options = '--ignore=important'
let g:ale_java_eclipselsp_path = expand('~/opt/eclipse.jdt.ls')
let g:ale_python_pylint_executable = 'run-pylint.sh'
let g:ale_sh_shellcheck_options = '--exclude=SC1090,SC2004,SC2164'
" Syntastic
let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 0
let g:syntastic_check_on_open = 1
let g:syntastic_check_on_wq = 1
let g:syntastic_loc_list_height = 6
let g:syntastic_enable_perl_checker = 1
let g:syntastic_perl_checkers = ['perl', 'podchecker']
let g:syntastic_css_csslint_args = '--ignore=important'
let g:syntastic_python_pylint_exe = 'run-pylint.sh'
let g:syntastic_sh_shellcheck_exe = 'shellcheck --exclude=SC1090,SC2004,SC2164'
" autoformat
nnoremap <F5> :Autoformat<CR>
let g:formatdef_autopep8 = '"autopep8 -".(g:DoesRangeEqualBuffer(a:firstline, a:lastline) ? " --range ".a:firstline." ".a:lastline : "")." ".(&textwidth ? "--max-line-length=".&textwidth : "")." --aggressive"'
let g:formatdef_shfmt = '"shfmt -bn -sr -i ".(&expandtab ? shiftwidth() : "0")'
let g:formatters_python = ['ruff', 'black', 'autopep8', 'yapf']
" Clang
let g:clang_auto_select=0
let g:clang_complete_auto=1
let g:clang_complete_copen=1
let g:clang_complete_macros=1
let g:clang_complete_patterns=1
let g:clang_jumpto_declaration_key='g>'
let g:clang_memory_percent=70
let g:clang_use_library=1
let g:clang_user_options='-I/usr/include -std=c++11'
"set conceallevel=2
"set concealcursor=vin
let g:clang_snippets=1
let g:clang_conceal_snippets=1
let g:clang_snippets_engine='clang_complete'
" Jedi
let g:jedi#auto_close_doc=1
let g:jedi#popup_on_dot=0
" vimtex
let g:tex_flavor = 'latex'
let g:vimtex_quickfix_open_on_warning = 0
let g:vimtex_view_method = 'zathura'
let g:vimtex_view_zathura_options = '&>/dev/null'
" web-indent
let g:js_indent_log=0
function! g:Undotree_CustomMap()
silent! unmap! <buffer> <CR>
nmap <buffer> <CR> <plug>UndotreeEnter <plug>UndotreeClose
endfunction
function! VimEnterPluginBehaviour()
if exists('*ale#linter#Define')
set omnifunc=ale#completion#OmniFunc
endif
if exists(':GundoToggle')
" Dependencies: python2
nnoremap <F4> :GundoToggle<CR>
let g:gundo_close_on_revert = 1
elseif exists(':UndotreeToggle')
nnoremap <F4> :UndotreeToggle<CR>
let g:undotree_SetFocusWhenToggle = 1
endif
set statusline=%t
set statusline+=%#warningmsg#
if exists('*ALEGetStatusLine')
set statusline+=%{ALEGetStatusLine()}
elseif exists('*SyntasticStatuslineFlag')
set statusline+=%{SyntasticStatuslineFlag()}
endif
set statusline+=%*
"set statusline+=%=\ \ %b(0x%B)\ \ %o(0x%O)-1,%l,%c\ %P
set statusline+=%=\ \ %b(0x%B)\ \ %l,%c\ %P
" Display keystrokes in statusline
set showcmd
endfunction
augroup vim_enter
autocmd!
autocmd VimEnter * call VimEnterPluginBehaviour()
augroup END
augroup file_changed_shell
" Ignore warnings
autocmd!
autocmd FileChangedShell * :
augroup END
" }}}
" {{{ FUNCTIONS
function! GetBufferList()
redir => l:buflist
silent! ls!
redir END
return l:buflist
endfunction
function! ToggleList(bufname, pfx)
let l:buflist = GetBufferList()
for l:bufnum in map(filter(split(l:buflist, '\n'), 'v:val =~ "'.a:bufname.'"'), 'str2nr(matchstr(v:val, "\\d\\+"))')
if bufwinnr(l:bufnum) != -1
exec(a:pfx.'close')
return
endif
endfor
if a:pfx ==? 'c' && len(getqflist()) == 0
echohl ErrorMsg
echomsg 'Quickfix List is Empty.'
echohl None
return
endif
if a:pfx ==? 'l' && len(getloclist(0)) == 0
echohl ErrorMsg
echomsg 'Location List is Empty.'
echohl None
return
endif
let l:winnr = winnr()
exec(a:pfx.'open')
if winnr() != l:winnr
wincmd p
endif
endfunction
nmap <silent> ,l :call ToggleList("Location List", 'l')<CR>
nmap <silent> ,q :call ToggleList("Quickfix List", 'c')<CR>
function! s:DiffSaved()
let l:filetype=&filetype
diffthis
vnew | r # | normal! 1Gdd
diffthis
exe 'setlocal bt=nofile bh=wipe nobl noswf ro ft=' . l:filetype
endfunction
command! DiffSaved call s:DiffSaved()
command! CompareSaved call s:DiffSaved()
" https://github.com/frioux/vim-lost
" https://git.savannah.gnu.org/cgit/diffutils.git/tree/src/diff.c?id=eaa2a24#n464
function! s:LineContext()
let l:re = get(b:, 'line_context_regex', '\v^[[:alpha:]#$_]')
let l:found = search(l:re, 'bn', 1, 100)
if l:found > 0
let l:line = getline(l:found)
return l:line
else
return '?'
endif
endfunction
command! LineContext echomsg s:LineContext()
command! Reload bufdo set eventignore-=Syntax | e
command! TagsRegenerate call job_start(['tags-regenerate.sh'])
command! Todo noautocmd vimgrep /TODO\|FIXME/j ** | cw
" }}}
" {{{ MAPPINGS
set pastetoggle=<F2>
noremap L Lzt
noremap H Hzb
nnoremap <Space> <C-f>
nnoremap <C-x>] <Esc>:exe "ptjump " . expand("<cWORD>")<Esc>
vmap <Space> "xy:@x<CR>
function! VisualPositions()
if mode() ==# 'v'
let [line_start, column_start] = getpos('v')[1:2]
let [line_end, column_end] = getpos('.')[1:2]
else
let [line_start, column_start] = getpos("'<")[1:2]
let [line_end, column_end] = getpos("'>")[1:2]
end
if (line2byte(line_start)+column_start) > (line2byte(line_end)+column_end)
let [line_start, column_start, line_end, column_end] =
\ [line_end, column_end, line_start, column_start]
end
return [line_start, column_start, line_end, column_end]
endfunction
function! VisualSelection()
let [line_start, column_start, line_end, column_end] =
\ VisualPositions()
let lines = getline(line_start, line_end)
if len(lines) == 0
return ''
endif
let lines[-1] = lines[-1][: column_end - 1]
let lines[0] = lines[0][column_start - 1:]
return lines
endfunction
function! VisualSelectionToString()
return join(VisualSelection(), "\n")
endfunction
" Open selected text as a URI
" Dependencies: +job
" Otherwise use system()
" References:
" - https://github.com/Carpetsmoker/xdg_open.vim/blob/master/plugin/xdg_open.vim
function! OpenURI(...)
let l:uri = substitute('' . join(a:000), '^\s*\(.\{-}\)\s*$', '\1', '')
if empty(l:uri)
return
endif
" Handle filenames with anchors
if l:uri !~? '^[a-z]\+:\/\/.*' && l:uri =~? '.*#.*'
let l:filename = substitute(l:uri, '#[^#]*$', '', '')
let l:anchor = substitute(l:uri, '^.*#\([^#]*\)$', '\1', '')
let l:anchor_header_pattern = '#[ \t]\+' . substitute(l:anchor, '-', '[ \\t-]\\+', 'g')
let l:anchor_tag_pattern = '<a.* name="' . l:anchor . '".*>'
let l:anchor_pattern = '\(' . l:anchor_header_pattern . '\)\|\(' . l:anchor_tag_pattern . '\)'
silent! execute 'edit' l:filename
silent! execute search(l:anchor_pattern)
return
endif
" Handle brackets being interpreted as invalid character range
" References:
" - [Issues with \`:file\` and square brackets / unexpected pattern matching · Issue \#2749 · vim/vim · GitHub](https://github.com/vim/vim/issues/2749)
try
let l:uriExpanded = expand(l:uri)
catch /E944:/
let l:uri = substitute(l:uri, '^\s*\([^\[]*\)\[\(\([^-]*\)\-\)\+\([^\]]*\)\]\(.*\)$', '\1\\[\2\4]\5', 'g')
let l:uriExpanded = expand(l:uri)
endtry
if empty(l:uriExpanded)
let l:uriExpanded = l:uri
endif
" If expanded pattern isn't a file, try glob
if !filereadable(l:uriExpanded)
let l:files = glob(l:uri)
if !empty(l:files)
let l:uriExpanded = split(l:files, "\n")[0]
endif
endif
" Handle relative paths
if l:uriExpanded =~# '^\.'
let l:uriExpanded = expand('%:p:h') . '/' . l:uri
endif
" Use current editor instance for plaintext files
if system('file -ib ' . shellescape(l:uriExpanded)) =~# '^text/'
silent! execute 'edit' l:uriExpanded
return
endif
echomsg 'Opening: ' . l:uriExpanded
call job_start(['env', 'XDG_CURRENT_DESKTOP=X-Generic', 'xdg-open', l:uriExpanded])
endfunction
nnoremap go :call OpenURI(expand("<cfile>"))<CR>
" We use our own function to get visual lines, so remove range '<,'> to avoid using the same line more than once.
" Reference: https://stackoverflow.com/questions/36406366/function-is-called-several-times-in-vimscript
vnoremap go :<C-u>call map(VisualSelection(), 'OpenURI(v:val)')<CR>
function! VisualGX(line)
let l:site = substitute(a:line, '^\s*\(.\{-}\)\s*$', '\1', '')
if l:site !~? '^https\?:\/\/'
let l:site = 'https://' . l:site
endif
call netrw#BrowseX(l:site, netrw#CheckIfRemote())
endfunction
nnoremap ,x :call VisualGX(getline('.'))<CR>
vnoremap ,x :call VisualGX(getline(getpos('v')[1]))<CR>
" TODO: use text object
" - https://github.com/kana/vim-textobj-user
" - https://github.com/coachshea/vim-textobj-markdown
vnoremap iL :normal 0f(lvt)<CR>
" Sane pasting
nnoremap "+p :set paste<CR>"+p:set nopaste<CR>:silent! TrimWhite<CR>
nnoremap "*p :set paste<CR>"*p:set nopaste<CR>:silent! TrimWhite<CR>
" Sane completion menu
"inoremap <expr> <CR> pumvisible() ? "\<C-Y>" : "\<CR>"
"autocmd InsertLeave,CompleteDone * pclose
" Conserve split on buffer delete
nnoremap <C-c> :bp\|bd #<CR>
" Remove trailing whitespace, jump to previous cursor position
command! RemoveWhite execute "normal! mw"|%s/\s\+$//|execute "normal! 'w"
command! TrimWhite RemoveWhite
" Buffers
if empty(mapcheck(',b'))
map ,b :ls<CR>:buffer<Space>
endif
if empty(mapcheck(',o'))
map ,o :b#<CR>
endif
map <C-j> :bn<CR>
map <C-k> :bp<CR>
silent! unmap! <C-i>
imap <C-i> <C-x><C-o>
silent! unmap! <C-l>
map <C-l> <Esc>:silent! pc<CR>:noh<CR>:redraw!<CR>
" Prevent omnicomplete on tab
function! InsertTabWrapper()
return "\<tab>"
endfunction
inoremap <tab> <c-r>=InsertTabWrapper()<cr>
" }}}
" {{{ BEHAVIOUR
set autochdir
set autoread
set tags=tags;
if isdirectory($HOME . '/tmp')
set backupdir=~/tmp directory=~/tmp
set tags+=~/tmp/tags
endif
set number
set cindent
set wildmenu
set tabstop=4 softtabstop=4 shiftwidth=4 expandtab
set completeopt=menu,menuone,preview,noselect,noinsert
set viminfo='200,<50,s10,h
set diffopt+=iwhite
if v:version >= 802
set diffopt+=algorithm:patience
endif
set hlsearch
" Match case-insensitive file patterns in an autocmd
set fileignorecase
" Search down into subdirectories
set path+=**
" Case insensitive search for lowercase chars
set ignorecase
set smartcase
" Match as you type
set incsearch
" Switch buffers without saving
set hidden
" }}}
" {{{ APPEARANCE
syntax on
set redrawtime=5000
" References:
" - https://stackoverflow.com/questions/3828606/vim-markdown-folding
" - https://github.com/tpope/vim-markdown/blob/master/ftplugin/markdown.vim
" TODO:
" - Fork script, ignore header tokens after a line with `mkdCodeStart` or contained in `mkdCode`
let g:markdown_folding=0
let g:markdown_folding_override=1
" Referenced by:
" - $VIMRUNTIME/syntax/markdown.vim
let g:markdown_minlines=200
set belloff=all
set previewheight=6
set showbreak=╚\ breakindent linebreak
set cmdheight=2
set laststatus=2
set scrolloff=0
set shortmess=a
" Preview window consistent with quickfix windows
set splitbelow
set splitright
function! InitColorScheme()
try
colorscheme paper
catch /^Vim\%((\a\+)\)\=:E/
colorscheme koehler
endtry
syntax on
endfunction
call InitColorScheme()
if has('gui_running')
try
set termguicolors
catch /^Vim\%((\a\+)\)\=:E/
endtry
set guioptions=aci
set guicursor=a:blinkwait750-blinkon750-blinkoff750
set lines=40 columns=84
endif
command! -range FormatShellCmd <line1>!format_shell_cmd.py
augroup filetype_group
autocmd!
autocmd BufEnter *
\ syntax sync minlines=500 |
\ if &filetype !=# 'markdown' && line('$') < 100 |
\ for i in range(1, line('$')) |
\ if strlen(getline(i)) >= 1200 |
\ setlocal syntax=off |
\ break |
\ endif |
\ endfor |
\ endif
set synmaxcol=1200
autocmd BufWritePost *
\ if getline(1) =~ "^#!" |
\ if getline(1) =~ "/bin/" |
\ silent execute "!chmod a+x <afile>" |
\ redraw! |
\ endif |
\ endif
" Alternative:
" redir @">|silent echo '# ' . expand('%:t:r')|redir END|put
autocmd BufNewFile *.{md,mdx,mdown,mkd,mkdn,mkdown,markdown} put = '# ' . expand('%:t:r') | normal! ggdd2o
if isdirectory(expand('~/code/snippets/recipes/'))
autocmd BufNewFile *.awk 0r ~/code/snippets/recipes/awk | normal! Gdd
autocmd BufNewFile *.java 0r ~/code/snippets/recipes/java |
\ for i in range(1, line('$')) |
\ call setline(i, substitute(getline(i), '%%%', expand('%:t:r'), '')) |
\ endfor |
\ normal! 6gg
autocmd BufNewFile *.py 0r ~/code/snippets/recipes/py | normal! Gdd
autocmd BufNewFile *.sh 0r ~/code/snippets/recipes/sh | normal! Gdd
autocmd BufNewFile *.zsh 0r ~/code/snippets/recipes/zsh | normal! Gdd
autocmd BufNewFile *.yml,*.yaml 0r ~/code/snippets/recipes/yaml | normal! Gdd
autocmd BufNewFile Makefile* 0r ~/code/snippets/recipes/Makefile | normal! Gddgg
autocmd BufNewFile package.json 0r ~/code/snippets/recipes/package.json | normal! Gddgg
autocmd BufRead,BufNewFile *.{diz,DIZ,nfo,NFO} setlocal filetype=nfo
autocmd BufRead,BufNewFile fonts.conf setlocal filetype=xml
autocmd BufNewFile pom.xml 0r ~/code/snippets/recipes/pom.xml | normal! Gdd
endif
" Open folds
autocmd FileType markdown normal! zR
autocmd FileType vim setlocal keywordprg=:help foldmethod=marker | normal! zR
autocmd FileType xml,html,xhtml,json setlocal foldmethod=manual | normal! zR
" Documentation, Search
autocmd FileType markdown noremap K :<C-u>call job_start(['xdg-open', "https://google.com/search?q=" . expand("<C-r><C-w>")])<CR>
autocmd FileType python noremap K :<C-u>terminal ++close pydoc <C-r><C-w><CR>
" Quickfix window height
autocmd FileType qf 6wincmd_
" Git
autocmd FileType gitcommit set colorcolumn=73 textwidth=72
" Langs
autocmd BufNewFile,BufRead *.sh,*.zsh
\ setlocal tabstop=2 softtabstop=2 shiftwidth=2 |
\ setlocal omnifunc=bashcomplete#Complete
autocmd FileType ruby
\ setlocal tabstop=2 softtabstop=2 shiftwidth=2
autocmd BufNewFile,BufRead *.jsx
\ setlocal filetype=javascript.jsx
autocmd BufNewFile,BufRead,BufEnter *.ttx
\ :syntax off<CR>
" Make
autocmd FileType make set noexpandtab
autocmd BufRead,BufNewFile *.cmake,CMakeLists.txt,*.cmake.in runtime! indent/cmake.vim
autocmd BufRead,BufNewFile *.cmake,CMakeLists.txt,*.cmake.in setf cmake
autocmd BufRead,BufNewFile *.ctest,*.ctest.in setf cmake
" Poor man's version of https://github.com/vim/vim/pull/12131
autocmd FileType xxd
\ syntax match MyF1 /\(\( \)\@<=\([0-1][0-9a-f]\)\)\|\([0-1][0-9a-f]\( \)\@=\)/ |
\ syntax match MyF2 /\(\( \)\@<=\([8-9a-f][0-9a-f]\)\)\|\([8-9a-f][0-9a-f]\( \)\@=\)/ |
\ syntax match MyF3 /\(\( \)\@<=\([2-7][0-9a-f]\)\)\|\([2-7][0-9a-f]\( \)\@=\)/ |
\ syntax match My0 /\(\( \)\@<=00\)\|\(00\( \)\@=\)/ |
\ syntax match My4 /\(\( \)\@<=ff\)\|\(ff\( \)\@=\)/
augroup END
" Reference: https://github.com/travisjeffery/vim-auto-mkdir
augroup auto_mkdir
autocmd!
autocmd BufWritePre * call s:auto_mkdir(expand('<afile>:p:h'))
function! s:auto_mkdir(dir)
if !isdirectory(a:dir)
call mkdir(iconv(a:dir, &encoding, &termencoding), 'p')
endif
endfunction
augroup END
function! HighlightedSynGroup()
let l:s = synID(line('.'), col('.'), 1)
echo synIDattr(l:s, 'name') . ' -> ' . synIDattr(synIDtrans(l:s), 'name')
endfunction
command! HighlightedSynGroup call HighlightedSynGroup()
" Highlight regex pattern matches with distinct colors
" Usage (native):
" match My0 /foo\|bar/
" match none
" Usage (command):
" Match foo\|bar
" call matchdelete(w:matches.My0)
" Usage (cli):
" printf '%s\n' 1 2 3 123 | vim -c 'Match 2\(3\)\@!' -
" References:
" - https://superuser.com/questions/211916/setting-up-multiple-highlight-rules-in-vim
" - https://vim.fandom.com/wiki/Highlight_long_lines
" - https://gist.github.com/romainl/379904f91fa40533175dfaec4c833f2f
" Alternatives:
" - https://github.com/MattesGroeger/vim-bookmarks
function! MyHighlights() abort
if !exists("g:terminal_ansi_colors")
let g:terminal_ansi_colors = []
endif
execute printf("hi My0 ctermbg=magenta guibg=%s ctermfg=black guifg=white", get(g:terminal_ansi_colors, 5, 'magenta'))
execute printf("hi My1 ctermbg=red guibg=%s ctermfg=black guifg=white", get(g:terminal_ansi_colors, 1, 'red'))
execute printf("hi My2 ctermbg=blue guibg=%s ctermfg=black guifg=white", get(g:terminal_ansi_colors, 4, 'blue'))
execute printf("hi My3 ctermbg=green guibg=%s ctermfg=black guifg=white", get(g:terminal_ansi_colors, 2, 'green'))
execute printf("hi My4 ctermbg=yellow guibg=%s ctermfg=black guifg=white", get(g:terminal_ansi_colors, 3, 'yellow'))
execute printf("hi MyF0 ctermfg=magenta guifg=%s cterm=bold gui=bold", get(g:terminal_ansi_colors, 5, 'magenta'))
execute printf("hi MyF1 ctermfg=red guifg=%s cterm=bold gui=bold", get(g:terminal_ansi_colors, 1, 'red'))
execute printf("hi MyF2 ctermfg=blue guifg=%s cterm=bold gui=bold", get(g:terminal_ansi_colors, 4, 'blue'))
execute printf("hi MyF3 ctermfg=green guifg=%s cterm=bold gui=bold", get(g:terminal_ansi_colors, 2, 'green'))
execute printf("hi MyF4 ctermfg=yellow guifg=%s cterm=bold gui=bold", get(g:terminal_ansi_colors, 3, 'yellow'))
endfunction
augroup MyColors
autocmd!
autocmd BufEnter * call MyHighlights()
augroup END
function! Match(...)
if !exists('w:matches')
let w:matches = {}
endif
if empty(a:000)
call clearmatches()
let w:matches = {}
else
let l:words = join(a:000)
let l:size = len(keys(w:matches))
let l:key = 'My' . ((l:size + 0) % 5)
let l:id = matchadd(l:key, l:words, -1)
let w:matches[l:key] = l:id
endif
endfunction
command! -nargs=* M :call Match(<f-args>)
function! MatchFG(...)
if !exists('w:matches_fg')
let w:matches_fg = {}
endif
if empty(a:000)
call clearmatches()
let w:matches_fg = {}
else
let l:words = join(a:000)
let l:size = len(keys(w:matches_fg))
let l:key = 'MyF' . ((l:size + 0) % 5)
let l:id = matchadd(l:key, l:words, -1)
let w:matches_fg[l:key] = l:id
endif
endfunction
command! -nargs=* MF :call MatchFG(<f-args>)
" Alternatives:
" - https://github.com/mortie/lograt
" - https://glogg.bonnefon.org/
function! GrepMatch(...)
let l:words = join(a:000)
call Match(l:words)
let @/=''.l:words
exec 'vimgrep /' . l:words . '/ %'
botright copen
endfunction
command! -nargs=* G :call GrepMatch(<f-args>)
" References:
" - http://vimdoc.sourceforge.net/htmldoc/pattern.html#/\%%3El
" - https://vim.fandom.com/wiki/Highlight_current_line
function! VisualMatch(positions, lines, key)
if !exists('w:matches')
let w:matches = {}
endif
if empty(a:lines)
call clearmatches()
let w:matches = {}
else
let l:size = len(keys(w:matches))
if empty(a:key)
let l:key = 'My' . ((l:size + 0) % 4)
else
let l:key = a:key
endif
let l:i = 0
while l:i < len(a:lines)
let a:lines[l:i] = escape(a:lines[l:i], '^$.*?/\[]')
let l:i += 1
endwhile
" Match across multiple lines in provided range,
" skipping chars up to first target column.
let l:text = join(a:lines, "\\n")
let l:pattern =
\ '\%>' . (a:positions[0]-1) . 'l' .
\ '\%<' . (a:positions[2]+1) . 'l' .
\ '\(^.\{' . (a:positions[1]-1) . '\}\)\@<=' . l:text
let l:id = matchadd(l:key, l:pattern, -1)
let w:matches[l:key] = l:id
endif
endfunction
nmap <silent> ,v :call VisualMatch(VisualPositions(), [], "")<CR>
vmap <silent> ,v :call VisualMatch(VisualPositions(), VisualSelection(), "")<CR>
vmap <silent> ,1 :call VisualMatch(VisualPositions(), VisualSelection(), "My4")<CR>
vmap <silent> ,2 :call VisualMatch(VisualPositions(), VisualSelection(), "My3")<CR>
vmap <silent> ,3 :call VisualMatch(VisualPositions(), VisualSelection(), "My2")<CR>
vmap <silent> ,4 :call VisualMatch(VisualPositions(), VisualSelection(), "My1")<CR>
vmap <silent> ,5 :call VisualMatch(VisualPositions(), VisualSelection(), "My0")<CR>
" Reference: https://vi.stackexchange.com/questions/2165/folding-by-regex-search-pattern
function! ZOverride() abort
let prevline = getline(v:lnum - 1)
let line = getline(v:lnum)
if get(split(prevline, w:z_delimiter), w:z_index, '') != get(split(line, w:z_delimiter), w:z_index, '')
return '>1'
endif
return '='
endfunction
" Fold clusters by column index and delimiter regex
function! Z(...)
let w:z_index = 0
if len(a:000) > 0 && !empty(a:1)
let w:z_index = a:1
endif
let w:z_delimiter = '\s\+'
if len(a:000) > 1 && !empty(a:2)
let w:z_delimiter = a:2
endif
setlocal foldexpr=ZOverride()
setlocal foldmethod=expr
endfunction
command! -nargs=* Z :call Z(<f-args>)
" }}}
" Load all plugins now.
" Plugins need to be added to runtimepath before helptags can be generated.
packloadall
" Load all of the helptags now, after plugins have been loaded.
" All messages and errors will be ignored.
silent! helptags ALL
if filereadable(expand('~/.local/share/vim/matches.vim'))
source ~/.local/share/vim/matches.vim
endif