-
Notifications
You must be signed in to change notification settings - Fork 0
/
.vimrc
1217 lines (1057 loc) · 36.1 KB
/
.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
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
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
if &compatible
set nocompatible " Be iMproved
endif
"filetype plugin indent on
"syntax enable
"au FileType c setlocal suffixesadd+=.c,.cpp,.h,.hpp,.lua
"au FileType cpp setlocal suffixesadd+=.c,.cpp,.h,.hpp,.lua
"au FileType c setlocal suffixesadd+=.c,.cpp,.h,.hpp,.lua
" Appearance
if !has("nvim")
set ttymouse=xterm2 " Allow mouse to drag splits
set t_Co=256
let $GIT_EDITOR = 'nvr -cc split --remote-wait'
endif
set termguicolors
" FZF Config ----------------
set runtimepath^=~/.fzf
function! s:handle_fzf_enter(cmd, lines) abort
try
let autochdir = &autochdir
set noautochdir
call reverse(a:lines)
if len(a:lines) >= 1
execute 'e' remove(a:lines, 0)
endif
for item in a:lines
execute a:cmd item
endfor
finally
let &autochdir = autochdir
silent! autocmd! fzf_swap
endtry
endfunction
let g:fzf_action = {
\ 'ctrl-t': 'tab split',
\ 'ctrl-x': function('s:handle_fzf_enter', ['split' ]),
\ 'ctrl-v': function('s:handle_fzf_enter', ['vsplit']),
\ '': function('s:handle_fzf_enter', ['vsplit']),
\ }
function! s:shortpath()
let short = fnamemodify(getcwd(), ':~:.')
if !has('win32unix')
let short = pathshorten(short)
endif
let slash = '/'
return empty(short) ? '~'.slash : short . (short =~ escape(slash, '\').'$' ? '' : slash)
endfun
function! s:Handle_FZF_filelist(fn)
execute "e" a:fn
if s:line_number >= 0
execute s:line_number
endif
endfunc
function! E_command(...)
let opts = {
\ 'options': ['--multi', '--select-1', '--tiebreak=end,length'],
\ 'window': 'vertical aboveleft 100new'
\ }
let args = deepcopy(a:000)
let s:line_number = -1 " -1: no line number
if len(args)
let last_arg = args[-1]
" Let users paste in compiler errors like "Foo/Bar.cpp:10"
let offset = match(last_arg, ":")
if offset >= 0
if offset > 0
let args[-1] = last_arg[0:offset-1]
elseif len(args) > 1
let args = args[0:len(args)-2]
else
let args = []
end
let s:line_number = last_arg[offset+1:]
endif
endif
let prompt = s:shortpath()
if len(args)
let path = expand(args[0])
if isdirectory(path)
let opts.dir = substitute(substitute(remove(args, 0), '\\\(["'']\)', '\1', 'g'), '[/\\]*$', '/', '')
let prompt = opts.dir . "/"
else
let dir = fnamemodify(path, ":h")
let fn = fnamemodify(path, ":t")
if isdirectory(dir)
let opts.dir = dir
let prompt = opts.dir . "/"
let args[0] = fn
endif
endif
if len(args)
call add(opts.options, '--query='.join(args))
endif
endif
let prompt = strwidth(prompt) < &columns - 20 ? prompt : '> '
call extend(opts.options, ['--prompt', prompt])
let opts.sink = function('s:Handle_FZF_filelist')
call fzf#run(fzf#wrap('E', opts))
endfunc
command! -nargs=* -complete=file_in_path E call E_command(<f-args>)
command! -nargs=* -range Align call Tabularize(<f-args>)
"End FZF------------------------------------
function! s:RunShellCommand(cmdline)
let expanded_cmdline = a:cmdline
for part in split(a:cmdline, ' ')
if part[0] =~ '\v[%#<]'
let expanded_part = fnameescape(expand(part))
let expanded_cmdline = substitute(expanded_cmdline, part, expanded_part, '')
endif
endfor
tabnew
setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
execute 'read !'. expanded_cmdline
execute '1d'
setlocal nomodifiable
1
endfunc
command! -complete=shellcmd -nargs=+ Shell call s:RunShellCommand(<q-args>)
autocmd VimEnter * call VimEnter_Initialize()
" Called after plugins are loaded, which is needed to override mapping set by plugins (like vim-mark's)
function! VimEnter_Initialize()
let g:VimEnter_Initialize = 1
" cabbrev <expr> e (getcmdtype()==':' && getcmdpos()<=2 ? 'E' : 'e')
cmap <expr> <leader><Tab> CmdLineTab()
vmap <leader>a :Align /
" Easier macro recording and playback using register q
map <leader>q qq
map <leader>. @q
set wildcharm=<Tab>
function! CmdLineTab()
if getcmdtype() != ':'
return "\<Tab>"
end
let line = getcmdline()
if line !~ '^e\(d\(i\(t\)\?\)\?\)\?'
return "\<Tab>"
end
let args = split(line)
call remove(args, 0) " Discard :edit
let path = expand(args[0])
if filereadable(path)
return "\<Esc>:edit " . args[0] . "\<CR>"
endif
return "\<Esc>:call E_command('" . join(args). "')\<CR>"
endfunc
command! ClearTerminal call ClearTerminal()
function! ClearRegs()
let regs=split('abcdefghijklmnopqrstuvwxyz0123456789/-"', '\zs')
for r in regs
call setreg(r, [], 'c')
endfor
endfunction
command! ClearRegs :call ClearRegs()
" Disabled: this overrides the foreground in all rows set cursorcolumn
" Disabled: this overrides the foreground in all rows set cursorline
" Not implemnted yet in nvim: au OptionSet diff let &cul=v:option_new
"
augroup MyCursorLine
au!
" au FilterWritePost * if &diff | let &cursorline=1 | let &cursorcolumn=1 | endif
" au BufEnter * if !&diff | let &cursorline=0 | let &cursorcolumn=0 | endif
augroup end
if &diff
set cursorline
end
set diffopt+=algorithm:histogram " histogram: better performance than patience or myers
set diffopt+=indent-heuristic " indent-heuristic: better context vs. content differentiation, apparently
set diffopt+=context:3 " 3: allow more diffs to be seen than the default (6)
set diffopt+=linematch:100 " align similar lines; 100: allow a reasonable hunk size (for now)
set diffopt+=vertical " so :diffsplit does a vertical split
set foldopen-=hor " hor: horizonal movement
function! FoldNonmatchingLines()
set foldcolumn=3
set foldlevel=0
set foldmethod=expr
set foldexpr=(getline(v:lnum)=~@/)?0:(getline(v:lnum-1)=~@/)\|\|(getline(v:lnum+1)=~@/)?1:2
set foldenable
autocmd BufLeave <buffer> set nofoldenable | set foldcolumn=0
autocmd BufEnter <buffer> set foldenable | set foldcolumn=3
endfunction
command! FoldNonmatchingLines call FoldNonmatchingLines(<f-args>)
" Status Line Appearance
set fillchars=vert:\ ,fold:+,diff:\ " Remove pipe character from vertical splits, uses ' ' for deleted line in diff output
function! GetStatusLineRHS()
return "%=%9*%l,%3c%*\ %3p%% [%n]" " %=: switch to right; %9:*: User9 color; %l: line#; %c: col number; %p: percent
endfunction
set statusline=%f:%l\ " %f: Filename
set statusline+=%4*%m%5*%r%w%h%q%* " %4*: User4 color; %m: modified; %5*: User5Color; %r: readonlyflag; %w: preview flag%h: help; %q: quickfix list; %*: revert color
let &statusline=&statusline . GetStatusLineRHS()
if has("nvim")
autocmd! TermOpen
autocmd TermOpen * let &l:statusline="%{b:term_title}" . GetStatusLineRHS()
autocmd TermOpen * set winfixheight
autocmd TermOpen * set nonu
autocmd TermOpen * set norelativenumber
map <silent> <C-W>! <C-W><Insert>:term<CR>:startinsert<CR>
imap <silent> <C-W>! <Esc><C-W>!
vmap <silent> <C-W>! <Esc><C-W>!
omap <silent> <C-W>! <Esc><C-W>!
end
map <silent> <C-W><Insert> :split<CR><C-W><S-J>
imap <silent> <C-W><Insert> <Esc><C-W><Insert>
vmap <silent> <C-W><Insert> <Esc><C-W><Insert>
omap <silent> <C-W><Insert> <Esc><C-W><Insert>
if has("nvim")
set inccommand=nosplit
endif
" highlight tabs and trailing whitespace, put $ for extending offscreen
set list
set listchars=tab:→\ ,trail:·,extends:»,precedes:«,nbsp:☠
set showbreak=»
set linebreak
set display+=uhex " Display unprintables as <00> hex codes rather than ^@ control codes
set display+=lastline " Display partial last line
set hlsearch " highlight search terms
set completeopt=longest,menuone " longest: add longest common text, then show menu even if only one option, to show source of option
" State files
set dir=~/tmp/vim//
set bdir=~/tmp/vim//
set undodir=~/tmp/vim//
set undofile " Create undo files so you can undo past file close (>= 7.3)
"set spell
set spellfile=~/.vim/spellfile.en.add
set mouse=a
set updatetime=250 " Decrease swap file update (flush) time
set ttimeoutlen=10 " Make <ESC> out of insert mode fast
set nowrap " don't wrap lines
set expandtab
set tabstop=4 " a hard tab is eight spaces
set softtabstop=4 " indent w/ TAB key like so
set shiftwidth=4 " number of spaces to use for autoindenting
set noshiftround " Don't align all lines' indents when indenting/outdenting
set backspace=indent,eol,start
" allow backspacing over everything in insert mode
set autoindent " always set autoindenting on
set copyindent " copy the previous indentation on autoindenting
set cinoptions=(s,m1 " (s,m1: indent close paren under first in open paren's line
set showmatch " set show matching parenthesis
set sidescroll=1 " scroll by 1 char at a time instead of 1/2 screen width
set smarttab " insert tabs on the start of a line according to
" shiftwidth, not tabstop
set incsearch " show search matches as you type
if ! &diff
" This is disabled by default because, once vim has more than 3 or 4
" windows open, relaying out the whole screen makes intentionall
" small windows big again.
set noequalalways " Don't resize on :split or :close of a window
endif
set formatoptions-=ro " When wrapping paragraphs, don't end lines
set formatoptions+=1 " When wrapping paragraphs, don't end lines
" with 1-letter words (looks stupid)
set path=$PWD/** " :find files in ./...
autocmd BufEnter,BufNewFile,BufRead * setlocal formatoptions-=ro " an autocommand so C and other plugins don't clobber this setting
autocmd BufEnter,BufNewFile,BufRead * setlocal formatoptions+=1 " an autocommand so C and other plugins don't clobber this setting
set guioptions-=t " Remove toolbar
set scrolloff=0 " Don't force lines above and below cursor
set switchbuf=usetab,split " usetab: Search for open window or tab first. split: open in place
set virtualedit=block " block: allow selecting rectangles that end in short lines
set nowarn " Avoid [No write since last change] with :!
set winminwidth=0 " Allow windows to collapse completely
set winminheight=0 " Allow windows to collapse completely
set winwidth=1 " Don't open windows more than one column when selecting
set splitbelow
set splitright
set belloff=esc,wildmode " esc: quiet <esc> in Normal mode, <wildmode>: quiet cmdline-completion available
set keymodel-=stopsel " Don't let arrow keys exit visual mode
set whichwrap+=<,>,h,l,[,]
" Allow control-leader, so that I don't need to exit TERMINAL MODE before
" swapping tabs, etc.
nmap <C-\> <leader>
imap <C-\> <C-o><leader>
tmap <C-\> <C-\><C-n><leader>
" ^S to Save
map <silent> <C-s> :wa<CR>
imap <silent> <C-s> <Esc><C-s>
" Indent/dedent in visual mode
vmap <silent> <Tab> >gv
vmap <silent> <S-Tab> <gv
" Clear highlight on <Esc>, but only in normal mode
if has("nvim")
map <silent> <Esc> <Esc>:nohl<CR>
vnoremap <silent> <Esc> <Esc>
endif
" Tabs <leader>-Arrows, Home, etc: move between tabs
map <silent> <leader>! :tabnew<CR>:term<CR>:startinsert<CR>
map <silent> <leader><Insert> :tabnew<CR>
map <silent> <leader><Left> :tabprev<CR>
map <silent> <leader><Right> :tabnext<CR>
map <silent> <leader><Home> :tabfirst<CR>
map <silent> <leader><End> :tablast<CR>
" <F1>: Help for word under cursor or visual selection
map <silent><expr> <F1> ":\<C-u>tab help " . expand("<cword>") . "\<CR>"
vmap <silent><expr> <F1> ":\<C-u>tab help " . GetVisualSelection() . "\<CR>"
function! GetVisualSelection()
" Why is this not a built-in Vim script function?!
let [line_start, column_start] = getpos("'<")[1:2]
let [line_end, column_end] = getpos("'>")[1:2]
let lines = getline(line_start, line_end)
if len(lines) == 0
return ""
endif
let lines[-1] = lines[-1][: column_end - (&selection == 'inclusive' ? 1 : 2)]
let lines[0] = lines[0][column_start - 1:]
let s = join(lines, "\n")
if len(s) == 0
return ""
else
return s
endif
endfunction
" <F2>: Exit, like <Esc> but works in TERMINAL mode
imap <silent> <F2> <Esc>l
vmap <silent> <F2> <Esc><F2>
omap <silent> <F2> <Esc><F2>
if has("nvim")
tmap <silent> <F2> <C-\><C-n>
endif
" <F3>: Next search match.
" <S-F3>: Prev search match.
map <silent> <F3> /<CR>
map <silent> <F13> ?<CR>
nmap <silent> n /<CR>
nmap <silent> N ?<CR>
imap <silent> <F3> <Esc><F3>
imap <silent> <F13> <Esc><F13>
vmap <silent> <F3> <Esc><F3>
vmap <silent> <F13> <Esc><F13>
cmap <silent><expr> <F3> HandleCommandModeF3()
cmap <silent><expr> <F13> HandleCommandModeF13()
function! HandleCommandModeF3()
let cmdtype = getcmdtype()
if cmdtype == '/' || cmdtype == '?'
return "\<CR>\<F3>"
endif
endfunction
function! HandleCommandModeF13()
let cmdtype = getcmdtype()
if cmdtype == '/' || cmdtype == '?'
return "\<CR>\<F13>"
endif
endfunction
" <Alt-F>o: gui-menu-like File->Open emulation, with fuzzy matching
map <silent> <M-f>o :E<CR>
vmap <silent> <M-f>o <Esc><M-f>o
imap <silent> <M-f>o <Esc><M-f>o
cmap <silent> <M-f>o <Esc><M-f>o
" Open Other related file (Find other files with similar names and different
" extensions easily.
map <expr> <M-f>C OpenOtherRelatedFile(".cpp")
map <expr> <leader>C OpenOtherRelatedFile(".cpp")
map <expr> <M-f>H OpenOtherRelatedFile(".h")
map <expr> <leader>H OpenOtherRelatedFile(".h")
map <expr> <M-f>I OpenOtherRelatedFile(".ivcg")
map <expr> <leader>I OpenOtherRelatedFile(".ivcg")
map <expr> <M-f>O OpenOtherRelatedFile("")
map <expr> <leader>O OpenOtherRelatedFile("")
map <expr> <M-f>Q OpenOtherRelatedFile(".qml", "ViewModel")
map <expr> <leader>Q OpenOtherRelatedFile(".qml", "ViewModel")
map <expr> <M-f>T OpenOtherRelatedFile("Tests.cpp")
map <expr> <leader>T OpenOtherRelatedFile("Tests.cpp")
map <expr> <M-f>Y OpenOtherRelatedFile(".yaml")
map <expr> <leader>Y OpenOtherRelatedFile(".yaml")
function! OpenOtherRelatedFile(ext, ...) abort
let fn = expand("%:t")
let new_fn = substitute(fn, '.\zs\.[^.]\+', a:ext, '')
if a:0 >= 1
let new_fn = substitute(new_fn, a:1, '', '')
endif
let exclusion_prefix = "/"
if fn == expand("%") " Not in a dir
let exclusion_prefix = "^"
endif
" !...$ excludes files with the current file's name
return ":E !" . exclusion_prefix . fn . "$ /" . new_fn . "\<CR>"
endfunction
" <leader>r: replace
map <leader>r :%s/\<<c-R><c-W>\>//g<Left><Left>
vmap <leader>r "zy:%s/<c-R>z//g<Left><Left>
" <leader>R: windo replace
map <leader>R :windo %s/\<<c-R><c-W>\>//g<Left><Left>
vmap <leader>R "zy:windo %s/<c-R>z//g<Left><Left>
" Searching
set noignorecase " don't ignore case when searching
set nosmartcase " don't ignore case if search pattern is all lowercase, case-sensitive otherwise
if &diff
map <leader>H VxnVnx
map <leader>O VnxknVx
endif
" <F4>: sync with filesystem: load & save changes
map <silent> <F4> :checktime<CR>:wa<CR>:diffupdate<CR>
imap <silent> <F4> <Esc><F4>i
vmap <silent> <F4> <Esc><F4>gv
if has("nvim")
tmap <silent> <F4> <C-\><C-n><F4>i
endif
" <C-s>: save current file
map <silent> <C-s> :checktime<CR>:wa<CR>:diffupdate<CR>
imap <silent> <C-s> <Esc><C-s>i
vmap <silent> <C-s> <Esc><C-s>gv
if has("nvim")
tmap <silent> <C-s> <C-\><C-n><C-s>i
endif
" <C-S-s>: sync with filesystem: load & save changes
map <silent> <C-S-s> :checktime<CR>:w<CR>:diffupdate<CR>
imap <silent> <C-S-s> <Esc><C-S-s>i
vmap <silent> <C-S-s> <Esc><C-S-s>gv
if has("nvim")
tmap <silent> <C-S-s> <C-\><C-n>:%w!
endif
" shift-<F4>: close window
map <silent> <F14> :close<CR>
imap <silent> <F14> <Esc><F14>
vmap <silent> <F14> <Esc><F14>
if has("nvim")
tmap <silent> <F14> <C-\><C-n><F14>
endif
" <F5>: Search filesystem for word under cursor
map <silent> <F5> :exec('!grep -rw --exclude-dir cg "' . expand("<cword>") . '" .')<CR>
imap <silent> <F5> <Esc><F5>i
vmap <silent> <F5> :\<C-u>exec('!grep --exclude-dir cg -r "' . GetVisualSelection() . '" .')<CR>
if has("nvim")
tmap <silent> <F5> <C-\><C-n><F5>i
endif
map <expr> <F5> ":call GrepInTerm(\'" . expand("<cword>") . "')<CR><C-\><C-n>"
function! DeleteProcessExitedMessage(timer_id)
setlocal modifiable
execute '%s/\[Process exited 0\]//'
execute '%s/\($\n\s*\)\+\%$//'
endfunction
function! OnExitSuppressProcessExitedWith0(job_id, code, event)
call timer_start(10, 'DeleteProcessExitedMessage')
endfunction
function! GrepInTerm(pattern) abort
let buf = nvim_create_buf(v:false, v:true)
let opts = {'relative': 'editor', 'width': 200, 'height': 50, 'col': 10,
\ 'row': 1, 'anchor': 'NW'}
let win = nvim_open_win(buf, 0, opts)
" optional: change highlight, otherwise Pmenu is used
call nvim_win_set_option(win, 'winhl', 'Normal:MyHighlight')
call nvim_set_current_win(win)
let cmd = 'grep --exclude-dir cg -rw "' . a:pattern . '" *'
let opt = { 'on_exit': 'OnExitSuppressProcessExitedWith0' }
call termopen(cmd . ' ; sleep 0.1', opt) " sleep allows neovim to read all output
let b:term_title = cmd
endfunction
" <S-F5> (<F15>) to list matching files, which is useful prior to opening
" each.
map <silent> <F15> :exec('!grep -rwl "' . expand("<cword>") . '" .')<CR>
imap <silent> <F15> <Esc><F15>i
vmap <silent> <F15> :\<C-u>exec('!grep -rl "' . GetVisualSelection() . '" .')<CR>
if has("nvim")
tmap <silent> <F15> <C-\><C-n><F15>i
endif
" <F7>: Open command-line window, like cmd.com's <F7>
map <F7> :<F7>
vmap <F7> :<F7>
imap <F7> <Esc><F7>
cmap <F7> <C-f>
" <F8>, next error is inspired by Visual Studio
map <silent> <F8> :cnext<CR>
lmap <silent> <F8> <Esc><F8>
map <silent> <S-F8> :cprev<CR>
map <silent> <F18> :cprev<CR>
lmap <silent> <S-F8> <Esc>:cprev<CR>
lmap <silent> <F18> <Esc>:cprev<CR> " <F18> is <S-F8> on my laptop.
if has("nvim")
tmap <silent> <F8> <Esc>:cnext<CR>
tmap <silent> <S-F8> <Esc>:cprev<CR>
endif
" Show syntax stack (developer hack, easily moved)
function! SynStack()
if !exists("*synstack")
return
endif
return "[".join(map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")'),",")."]"
endfunc
function! SynGroup()
let l:s = synID(line('.'), col('.'), 1)
return "[" . synIDattr(l:s, 'name') . ' -> ' . synIDattr(synIDtrans(l:s), 'name') . "]"
endfun
"map <silent> <F10> :echo "hi<".synIDattr(synID(line("."),col("."),1),"name").'> trans<'.synIDattr(synID(line("."),col("."),0),"name")."> lo<".synIDattr(synIDtrans(synID(line("."),col("."),1)),"name")."> stack:".SynStack() . " " . SynGroup()<CR>
" <F10> Full screen the visual selection
" <F20> (<S-F10>): Narrow back.
" The extra <Esc> after the tab new is when coming from vterm's terminal
" vmap <silent> <F10> :<C-u><F10>
" map <silent> <F10> :tabclose<CR>
" <F11>: "full screen"
map <silent> <F11> :tab split \| setlocal laststatus=1<CR>100zH
imap <silent> <F11> <C-o><F11>
vmap <silent> <F11> <Esc><Fll>gv
if has("nvim")
tmap <silent> <F11> <C-\><C-n><F11>
endif
" <F12>: bottom window (usually my terminal window / previous window toggle)
" Trimming the scrollback like this prevents the terminal from scrolling
" when not scrolled all the way down. Until 50,000 more lines accumulate.
map <silent> <F12> <C-w>b<C-\><C-n>:set scrollback=50000<CR>:set scrollback=100000<CR>
imap <silent> <F12> <Esc><F12>
vmap <silent> <F12> <Esc><F12>
function! GetChar(at) abort
let l:pos = a:at
if type(l:pos) == 1 " 1: string
let l:pos = getpos(l:pos)
endif
let l:line = getline(l:pos[1])
let l:index = l:pos[2] - 1
if l:index < 0 || l:index >= strwidth(l:line)
return "\n"
end
return strcharpart(line, l:index, 1)
endfunction
function! CharType(c) abort
if a:c =~# '\w'
return 'word'
elseif a:c =~# '[(){}"''[\]]'
return 'pair'
elseif a:c == '\n'
return 'newline'
else
return 'other'
end
endfunction
function! GetLineTextObjectExpr(i_or_a) abort
let l:vpos = getpos('v')
let l:cpos = getpos('.')
let l:is_reversed = ComparePositions(l:vpos, l:cpos) > 0
echom col('$')
if !l:is_reversed
if a:i_or_a == 'a' || col('$') == 1
return 'o0o$'
else
return 'o0o$h'
end
else
if a:i_or_a == 'a' || col('$') == 1
return '0o$o'
else
return '0o$ho'
end
endif
endfunction
vnoremap <expr> il GetLineTextObjectExpr('i')
vnoremap <expr> al GetLineTextObjectExpr('a')
"let s:visual_stack = []
"let s:visual_stack_index = 0
"
"function! InitVisualRangeStack() abort
" let s:visual_stack = []
" let s:visual_stack_index = 0
"endfunction
"
"function! PushVisualRange() abort
" let s:visual_stack = s:visual_stack[:s:visual_stack_index] + [ [ getpos('v'), getpos('.') ] ]
" let s:visual_stack_index += 1
"endfunction
"
"function! StartVisualExpr() abort
" call InitVisualRangeStack()
"
" let l:c = GetChar('.')
"
" let l:t = CharType(l:c)
"
" if l:t == 'word'
" return 'viw'
" elseif l:t == 'pair'
" return 'v%'
" else
" return 'viW'
" endif
"
"endfunction
"
"function! ComparePositions(p0, p1) abort
" if a:p0[1] < a:p1[1]
" return -1
" endif
"
" if a:p0[1] > a:p1[1]
" return 1
" endif
"
" if a:p0[2] < a:p1[2]
" return -1
" endif
"
" if a:p0[2] > a:p1[2]
" return 1
" endif
"
" return 0
"endfunction
"
"lua package.loaded.text_objects = nil; -- debug only
"lua require("text_objects")
"
"function! MapQuotesOnLine(type) abort
" let l:pos = getpos('.')
" let l:line = getline(l:pos[1])
" let l:stack = []
" let l:region = 0
" for l:c in split(l:line, '\zs')
" endfor
"endfunction
"
"function! GetTextObject(type) abort
" if mode() == 'n'
" normal v
" endif
"
" let l:vpos = getpos('v')
" let l:cpos = getpos('.')
"
" let l:is_reversed = ComparePositions(l:vpos, l:cpos) > 0
" if l:is_reversed
" normal o
" let l:vpos = getpos('v')
" let l:cpos = getpos('.')
" endif
"
" let l:is_one_line = l:vpos[1] == l:cpos[1]
"
" if a:type == '"' || a:type == "'"
" if !l:is_one_line
" return []
" endif
"
" let l:m = MapQuotesOnLine(a:type)
" endif
"
"endfunction
"
"function! GetVisualModeText() abort
" let save_clipboard = &clipboard
" set clipboard= " Avoid clobbering the selection and clipboard registers.
" let save_reg = getreg('"')
" let save_regmode = getregtype('"')
" silent normal! ygv
" let res = getreg('"')
" call setreg('"', save_reg, save_regmode)
" let &clipboard = save_clipboard
" return res
"endfunction
"
"function! GrowVisual() abort
" normal gv
"
" if len(s:visual_stack) == 0
" call PushVisualRange()
" endif
"
" "echom "======================="
"
" let l:vpos = getpos('v')
" let l:cpos = getpos('.')
"
" let l:is_reversed = ComparePositions(l:vpos, l:cpos) > 0
" if l:is_reversed
" normal o
" let l:vpos = getpos('v')
" let l:cpos = getpos('.')
" endif
"
" "echom " =" join(l:vpos,':') join(l:cpos,':')
"
" let l:vp = l:vpos
" let l:cp = l:cpos
"
" let l:found = 0
"
" for l:o in [ 'i(', 'a(', 'i{', 'a{', 'i[', 'a[', 'i''', 'i"']
" if l:o == 'i'''
" call setpos(".", l:vp)
" normal o
" call setpos(".", l:cp)
"
" let l:text = GetVisualModeText()
" if match(l:text, '''') >= 0
" continue
" endif
" endif
"
" "echom " "
" call setpos(".", l:vpos)
" normal o
" call setpos(".", l:cpos)
"
" exe "normal " . l:o
"
" let l:v = getpos("v")
" let l:c = getpos(".")
"
" "echom l:o . '?' join(l:v, ':') join(l:c, ':') ComparePositions(l:v, l:vpos) ComparePositions(l:c, l:cpos) ComparePositions(l:v, l:vp) ComparePositions(l:c, l:cp)
" if (
" \ ComparePositions(l:v, l:vpos) < 0
" \ || ComparePositions(l:c, l:cpos) > 0
" \ )
" \ && (
" \ !l:found
" \ || (
" \ ComparePositions(l:v, l:vp) > 0
" \ && ComparePositions(l:c, l:cp) < 0
" \ )
" \ )
" let l:vp = l:v
" let l:cp = l:c
" let l:found = 1
" "echom l:o . '=' join(l:vp, ':') join(l:cp, ':')
" endif
" endfor
"
" if !l:found " Expand to line
" if l:vp[2] > 1
" let l:found = 1
" let l:vp[2] = 1
" endif
" let l:cend = col("$")
" if l:cp[2] < l:cend
" let l:found = 1
" let l:cp[2] = l:cend
" endif
" endif
"
" call setpos(".", l:vp)
" normal o
" call setpos(".", l:cp)
"
" if l:is_reversed
" normal o
" endif
"
" if l:found
" call PushVisualRange()
" endif
"
" "echom join(getpos("v"), ':') join(getpos("."), ':')
"endfunction
"
"function! ShrinkVisual() abort
" normal gv
" if s:visual_stack_index > 0
" let s:visual_stack_index -= 1
" call setpos(".", s:visual_stack[s:visual_stack_index][0])
" normal o
" call setpos(".", s:visual_stack[s:visual_stack_index][1])
" end
"endfunction
"
"function! GrowVisualLeft() abort
" normal gv
"
" if len(s:visual_stack) == 0
" call PushVisualRange()
" endif
"
" let l:v = getpos("v")
" let l:c = getpos(".")
" if ComparePositions(l:v, l:c) < 0
" normal o
" endif
"
" normal b
"
" call PushVisualRange()
"endfunction
"
"function! StartVisualGrowLeftExpr() abort
" call InitVisualRangeStack()
" return "vb"
"endfunction
"
"function! StartVisualGrowRightExpr() abort
" call InitVisualRangeStack()
" return "vw"
"endfunction
"
"function! GrowVisualRight() abort
" normal gv
"
" if len(s:visual_stack) == 0
" call PushVisualRange()
" endif
"
" let l:v = getpos("v")
" let l:c = getpos(".")
" if ComparePositions(l:v, l:c) > 0
" normal o
" endif
"
" normal w
"
" call PushVisualRange()
"endfunction
"
"nnoremap <expr> + StartVisualExpr()
"nnoremap <expr> ( StartVisualGrowLeftExpr()
"nnoremap <expr> ) StartVisualGrowRightExpr()
"
"vnoremap <silent> + :call GrowVisual()<CR>
"vnoremap <silent> v :call GrowVisual()<CR>
"vnoremap <silent> _ :call ShrinkVisual()<CR>
"vnoremap <silent> ( :call GrowVisualLeft()<CR>
"vnoremap <silent> ) :call GrowVisualRight()<CR>
"
"nnoremap <expr> <C-Up> StartVisualExpr()
"nnoremap <expr> <C-Left> StartVisualGrowLeftExpr()
"nnoremap <expr> <C-Right> StartVisualGrowRightExpr()
"vnoremap <silent> <C-Up> :call GrowVisual()<CR>
"vnoremap <silent> <C-Down> :call ShrinkVisual()<CR>
"vnoremap <silent> <C-Left> :call GrowVisualLeft()<CR>
"vnoremap <silent> <C-Right> :call GrowVisualRight()<CR>
"
"nnoremap <expr> <C-ScrollWheelUp> StartVisualExpr()
"vnoremap <silent> <C-ScrollWheelUp> :call GrowVisual()<CR>
"vnoremap <silent> <C-ScrollWheelDown> :call ShrinkVisual()<CR>
"
" [a and ]a are under development and not yet right, propably to be replaced
" with a treesitter-based approach.
" Syntax region text object "ia" and "aa" (inspired by SyntaxMotion.vim)
"
"function! MoveCursor(dir)
" let l:start_pos = getpos('.')
" if a:dir == 'h'
" normal! h
" else
" normal! l
" end
" return getpos('.') != l:start_pos
"endfunction
"
"function! SyntaxMotion(dir, mode, count)
" if a:mode == 'v'
" normal gv
" end
"
" let l:count = a:count
" if l:count == 0
" let l:count = 1
" endif
"
" let l:whichwrap = split(&whichwrap . ",h,l", ",")
" let l:whichwrap = filter(copy(l:whichwrap), 'index(l:whichwrap, v:val, v:key+1) == -1')
" let &whichwrap = join(l:whichwrap,",")
"
" while l:count > 0
" let l:syn_stack_0 = synstack(line('.'), col('.'))
"
" while 1
" let l:save_cursor = getpos(".")
"
" call MoveCursor(a:dir)
"
" if getpos('.') == l:save_cursor
" break
" end
"
" let l:syn_stack_1 = synstack(line('.'), col('.'))
"
" if l:syn_stack_1 != l:syn_stack_0
" call setpos('.', l:save_cursor)
" break
" endif
" endwhile
"
" let l:count = l:count - 1
" endwhile
"