-
Notifications
You must be signed in to change notification settings - Fork 0
/
.vimrc
1383 lines (1181 loc) · 28.5 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
" vimrc
" General -------------------- {{{
se nocp
se bs=2
se noswf
se ffs=unix,dos
se enc=utf-8
se fencs=ucs-bom,utf-8,sjis,gb18030,latin1
se rtp^=~/.vim
se rtp+=~/.vim/after
se vi+=n~/.viminfo
" Wait forever for mappings
se noto
" Wait 1ms for key codes
" Ideally waiting 0ms is just OK, but there is a problem where vim
" occasionally receives uninterpreted key codes when launched from wsltty
se ttimeout
se ttm=1
se kp=
se nosol
se mmp=2000000
let s:win = has('win32') || has('win64')
let s:gui = has('gui_running')
let s:win_gui = s:win && s:gui
let s:vim8 = v:version >= 800
if !empty($VIM_FENCS)
let &fencs = $VIM_FENCS
end
aug detect_stdin
au!
au StdinReadPost * let b:stdin = 1 | setl nomod
au BufWrite * unl! b:stdin
aug END
" ---------------------------- }}}
" Global Mappings ------------ {{{
" Be careful not to type spaces in front of '|', or the spaces will become
" part of the mapping.
com! -bar -nargs=+ Map nn <args>|vn <args>
" Unmapping {{{
sil! vu <C-x>
nn <C-q> <Nop>
nn <Space> <Nop>
" }}}
" Basic keys {{{
nn <expr> : TryFakeCmdLine()
nn U <C-r>
aug local_map
au!
" It would have been enough with BufWinEnter, but NERDTree overrides the
" mapping anyway in some cases, so we override it back in FileType.
au BufWinEnter,FileType * Map <buffer> <silent> q :call Quit()<CR>
aug END
Map <silent> Q :call QuitAll()<CR>
Map [q q
Map [Q Q
nn <silent> <Space>w :up<CR>
Map <Space>v V
vn x "_d
vn <silent> p :<C-u>call VPut()<CR>
vn P p
vn gp "0p
nn gg gg0
nn G G0
" }}}
" Moving {{{
Map <Space>h gT
Map <Space>l gt
Map <Space>j <C-d>
Map <Space>k <C-u>
Map <Space>u <C-f>
Map <Space>i <C-b>
Map <silent> <Space>f :<C-u>call ViewingMode()<CR>
Map <C-h> <C-w>h
Map <C-j> <C-w>j
Map <C-k> <C-w>k
Map <C-l> <C-w>l
" }}}
" Search & Replace {{{
nn <silent> & :&&<CR>
nn gs :%s//g<Left><Left>
nn g* :%s/\<<C-r><C-w>\>//g<Left><Left>
no g/ /^\s*\zs
" vn <silent> s :call VSub('g')<CR>
vn s :s//g<Left><Left>
nn <silent> n :call CenterAfter('n')<CR>:call ShowSearch('n')<CR>
nn <silent> N :call CenterAfter('N')<CR>:call ShowSearch('N')<CR>
nn <Space>n n
nn <Space>N N
" }}}
" Text objects {{{
func! TextObjMap(lhs, rhs)
exe printf('vm a%s a%s', a:lhs, a:rhs)
exe printf('om a%s a%s', a:lhs, a:rhs)
exe printf('vm i%s i%s', a:lhs, a:rhs)
exe printf('om i%s i%s', a:lhs, a:rhs)
endf
call TextObjMap('r', ']')
call TextObjMap('a', '>')
call TextObjMap('v', '}')
nn <silent> co :sil call ExpandTextObj()<CR>
nn <silent> cd :sil call CollapseTextObj()<CR>
nn <silent> csfv :sil call ChangeSurround('f', ['{', '}'])<CR>
nn <silent> csvf :sil call ChangeSurround('v', ['do', 'end'])<CR>
" }}}
" Utils {{{
nn ga <C-a>
vn ga g<C-a>
nn gx <C-x>
vn gx g<C-x>
nn <C-a> ga
nn <silent> <Space>r :redraw!<CR>
nn <silent> <Space>p :let &paste=!&paste<CR>
nn <silent> <Space>. :so ~/.vimrc<CR>
ino <C-\><CR> <End><CR>
ino <C-\><BS> <Up><End><CR>
ino <C-\>e <CR><Up><End><CR>
" If <Esc> is used instead, the cursor will flash noticeably in gVim
ino <silent> <C-k> <C-\><C-o>:call MakeUpper()<CR>
cno ` <C-r>
cno `` `
vn <LeftMouse> "+ygV
vn <RightMouse> "+:<C-u>call VPut()<CR>
nn <silent> <Space>y :call ClipRange('1', '$', 1)<CR>
vn <silent> gy :<C-u>call ClipVisual()<CR>
nn <Space>; A;<Esc>
nn <silent> gJ :call SeamlessJoin('.', '.+1')<CR>
vn <silent> gJ :<C-u>call SeamlessJoin("'<", "'>")<CR>
Map [J gJ
Map <M-x> :
cno <M-j> <Down>
cno <M-k> <Up>
" }}}
" ---------------------------- }}}
" Plugins -------------------- {{{
let s:plug_path = expand('~/.vim/autoload/plug.vim')
func! GetPlug()
if s:win
return
end
let url = 'https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
sil exe printf('!curl --create-dirs -sLo %s %s', s:plug_path, url)
redraw!
return v:shell_error == 0
endf
com! -bar GetPlug call GetPlug()
let g:has_plug = filereadable(s:plug_path)
if g:has_plug
sil! call plug#begin('~/.vim/plugged')
else
com! -bar -nargs=* Plug
end
" Basic --------------------------
" Plug 'flazz/vim-colorschemes'
Plug 'morhetz/gruvbox'
Plug 'itchyny/lightline.vim'
Plug 'scrooloose/nerdtree'
" --------------------------------
" Syntax -------------------------
" Plug 'sheerun/vim-polyglot'
Plug 'vim-ruby/vim-ruby'
Plug 'vim-python/python-syntax'
Plug 'Vimjas/vim-python-pep8-indent'
Plug 'flosacca/vim-javascript'
Plug 'leafgarland/typescript-vim'
Plug 'rust-lang/rust.vim'
Plug 'udalov/kotlin-vim'
Plug 'gabrielelana/vim-markdown'
Plug 'vim-language-dept/css-syntax.vim'
Plug 'kevinoid/vim-jsonc'
Plug 'cespare/vim-toml'
Plug 'leafOfTree/vim-vue-plugin', { 'tag': 'v1.0.20200714' }
Plug 'MaxMEllon/vim-jsx-pretty'
Plug 'rhysd/vim-llvm'
Plug 'dylon/vim-antlr'
Plug 'flosacca/nginx.vim'
Plug 'isobit/vim-caddyfile'
Plug 'flosacca/Dockerfile.vim'
Plug 'gko/vim-coloresque'
if s:vim8
Plug 'ionide/Ionide-vim'
end
" --------------------------------
" Operation ----------------------
Plug 'easymotion/vim-easymotion'
Plug 'tpope/vim-surround'
Plug 'vim-scripts/tComment'
Plug 'godlygeek/tabular'
Plug 'dhruvasagar/vim-table-mode'
" Plug 'terryma/vim-multiple-cursors'
if s:vim8
Plug 'mg979/vim-visual-multi'
end
Plug 'tpope/vim-abolish'
Plug 'danro/rename.vim'
Plug 'mattn/emmet-vim'
Plug 'tpope/vim-rails'
" --------------------------------
" External -----------------------
Plug 'iamcco/markdown-preview.nvim'
" --------------------------------
Plug 'kana/vim-altr'
" Plug 'kana/vim-submode'
Plug 'kana/vim-textobj-user'
Plug 'kana/vim-textobj-syntax'
if g:has_plug
call plug#end()
end
ru! macros/matchit.vim
" EasyMotion {{{
let g:EasyMotion_keys = 'asdfghwertyuiopcvbnmlkj'
map gj <Plug>(easymotion-j)
map gk <Plug>(easymotion-k)
map gb <Plug>(easymotion-bl)
map gw <Plug>(easymotion-wl)
" map gB <Plug>(easymotion-B)
" map gW <Plug>(easymotion-W)
" map gh <Plug>(easymotion-bl)
" map gl <Plug>(easymotion-wl)
map go <Nop>
nm go <Plug>(easymotion-overwin-line)
" map gj <Nop>
" map gk <Nop>
"
" map gkj <Plug>(easymotion-b)
" map gjk <Plug>(easymotion-w)
"
" map gkl <Plug>(easymotion-ge)
" map gjl <Plug>(easymotion-e)
"
" map gjj <Plug>(easymotion-j)
" map gkk <Plug>(easymotion-k)
"
" map gjh <Plug>(easymotion-sol-j)
" map gkh <Plug>(easymotion-sol-k)
map gh <Nop>
map gl <Nop>
map ghj <Plug>(easymotion-b)
map ghk <Plug>(easymotion-ge)
map glj <Plug>(easymotion-w)
map glk <Plug>(easymotion-e)
map ghh <Plug>(easymotion-B)
map ghl <Plug>(easymotion-gE)
map glh <Plug>(easymotion-W)
map gll <Plug>(easymotion-E)
let g:EasyMotion_re_line_anywhere = '.'
map gH <Plug>(easymotion-linebackward)
map gL <Plug>(easymotion-lineforward)
" }}}
" NERDTree {{{
let g:NERDTreeDirArrowExpandable = '+'
let g:NERDTreeDirArrowCollapsible = '-'
let g:NERDTreeMapHelp = 'K'
let g:NERDTreeMapQuit = '<Space>q'
nn <silent> <Space>t :NERDTreeToggle<CR>
" This doesn't work when editing a new dir
let g:NERDTreeChDirMode = 2
" }}}
" Emmet {{{
" im <C-u> <Plug>(emmet-expand-abbr)
" nm <C-u> <Plug>(emmet-expand-abbr)
" vm <C-u> <Plug>(emmet-expand-abbr)
nn <C-t> <C-y>
ino <C-t> <C-y>
" }}}
" Text objects {{{
call TextObjMap('j', 'y')
let g:surround_118 = "{\r}" " v
let g:surround_109 = "$\r$" " m
let g:surround_100 = "$$ \r $$" " d
" Needed only when using noremaps in surround
" nm dsv ds}
" nm csv cs}
" nm dsm ds$
" nm csm cs$
" if exists('*textobj#user#plugin')
" This does not work
" end
sil! call textobj#user#plugin('latex', {
\ 'environment': {
\ 'pattern': ['\\begin{[^}]\+}\(\[[^]]*\]\)\?\({[^}]*}\)*\n\?', '\\end{[^}]\+}'],
\ 'select-a': 'ae',
\ 'select-i': 'ie',
\ },
\ 'math-a': {
\ 'pattern': '\$\_[^$]*\$',
\ 'select': ['a$', 'am'],
\ },
\ 'math-i': {
\ 'pattern': '\v\$[ \t\r\n]*\zs[^$ \t\r\n]+([ \t\r\n]+[^$ \t\r\n]+)*\ze[ \t\r\n]*\$',
\ 'select': ['i$', 'im'],
\ },
\ 'displaymath-a': {
\ 'pattern': '\$\$\_[^$]*\$\$',
\ 'select': 'ad',
\ },
\ 'displaymath-i': {
\ 'pattern': '\v\$\$[ \t\r\n]*\zs[^$ \t\r\n]+([ \t\r\n]+[^$ \t\r\n]+)*\ze[ \t\r\n]*\$\$',
\ 'select': 'id',
\ },
\ })
sil! call textobj#user#plugin('ruby', {
\ 'do-block': {
\ 'pattern': ['\<do\>', '\<end\>'],
\ 'select-a': 'af',
\ 'select-i': 'if',
\ },
\ 'brace-block-args': {
\ 'pattern': ['{\( *|[^|]\+|\)\?', '}'],
\ 'select-a': 'aV',
\ 'select-i': 'iV',
\ },
\ })
" }}}
" Others {{{
nm gn <Plug>TComment_gcc
vm gn <Plug>TComment_gc
let g:mkdp_auto_close = 0
let g:mkdp_preview_options = { 'disable_sync_scroll': 1 }
let g:markdown_enable_spell_checking = 0
let g:markdown_enable_mappings = 0
let g:markdown_enable_input_abbreviations = 0
let g:table_mode_corner = '|'
let g:table_mode_motion_up_map = '[-'
let g:table_mode_motion_down_map = ']-'
" let g:multi_cursor_select_all_word_key = 'g<C-n>'
" TODO: better mappings
let g:VM_maps = {
\ 'Skip Region': '<C-x>',
\ 'Remove Region': '<C-d>',
\ 'Find Subword Under': '',
\ 'Visual Cursors': '<C-n>',
\ }
nm <Space>[ <Plug>(altr-forward)
nm <Space>] <Plug>(altr-back)
" }}}
" ---------------------------- }}}
" View ----------------------- {{{
se nu
se ru
se nowrap
se fdm=marker
se ls=2
se nosmd
aug gui_config
au!
au GUIEnter * call GUIConfig()
aug END
func! GUIConfig()
se go=
sil! se gfn=mononoki_nf:h13
sil! se gfw=nsimsun:h14
se ambw=double
if !s:win
return
end
try
call VimTweak('EnableMaximize', 1)
catch
sim ~x
endt
endf
nn <silent> [j :se gfw=ms_mincho:h14<CR>
nn <silent> ]j :se gfw& gfn=ms_mincho:h14<CR>
com! -bar -nargs=1 Gfn se gfw& gfn=<args>
func! SetPrettyScheme()
try
let g:gruvbox_italic = 0
colo gruvbox
hi! link Operator GruvboxRed
let g:lightline = {
\ 'colorscheme': 'gruvbox',
\ 'mode_map': { 'c': 'NORMAL' },
\ 'tabline': { 'right': [] }
\ }
catch
return 0
endt
return 1
endf
let s:term_colored = !s:gui && $TERM =~# '\v<(256color|direct)>'
let s:enable_pretty_scheme = s:gui || s:term_colored
if s:term_colored && has('termguicolors')
\ && (empty($TMUX) || system('tmux info') =~# '\v<Tc>[^\n]*<true>')
let &t_8f = "\e[38;2;%lu;%lu;%lum"
let &t_8b = "\e[48;2;%lu;%lu;%lum"
se tgc
end
" In Vim 8 within the terminal, the file info message gets cleared when
" applying a color scheme (specifically, on `hi Normal`). We simply disable
" the file info message here and re-enable it later. To show it manually,
" use `:e`.
if s:vim8 && !s:gui
se shm+=F
end
se bg=dark
if !s:enable_pretty_scheme || !SetPrettyScheme()
se t_Co=256
se nocuc
se nocul
colo desert
end
if empty($VIM_CURSOR_BLINK)
let &t_ti .= "\e[2 q"
let &t_te .= "\e[4 q"
let &t_SI .= "\e[6 q"
let &t_EI .= "\e[2 q"
if s:vim8
let &t_SR .= "\e[4 q"
end
else
let &t_ti .= "\e[1 q"
let &t_te .= "\e[3 q"
let &t_SI .= "\e[5 q"
let &t_EI .= "\e[1 q"
if s:vim8
let &t_SR .= "\e[3 q"
end
end
aug move_help_window
au!
au BufRead * if &bt == 'help' | setl nu | winc L | end
aug END
" ---------------------------- }}}
" Format --------------------- {{{
se ts=2
se sw=0
se sts=-1
se et
se ai
se nojs
com! -bar -nargs=1 TS setl ts=<args> | setl sw=0 | setl sts=-1
aug add_file_type
au!
au BufRead,FileType * call AddFileType()
aug END
func! AddFileType()
let ext = expand('%:e')
if ext ==? 'pgf'
se ft=tex
elseif ext ==? 'ipynb'
se ft=json
elseif ext ==? 's'
se ft=ia64
elseif ext =~? '\v^(asm|inc)$'
se ft=masm
end
if &ft == 'json'
if s:vim8 && !empty(getcompletion('jsonc', 'filetype'))
se ft=jsonc
end
hi Error NONE
end
let base = expand('%:t')
if base ==# '.gemrc'
se ft=yaml
elseif base ==# 'pip.conf'
se ft=dosini
end
endf
let g:c_no_curly_error = 1
let g:is_bash = 1
let g:sh_no_error = 1
let g:tex_flavor = 'latex'
let g:tex_no_error = 1
let g:html_indent_script1 = 'zero'
let g:python_highlight_all = 1
let g:python_highlight_space_errors = 0
let g:ruby_indent_assignment_style = 'variable'
let g:cargo_shell_command_runner = 'bel vert term'
let g:go_highlight_trailing_whitespace_error = 0
let g:vim_vue_plugin_use_scss = 1
let g:vim_vue_plugin_highlight_vue_attr = 1
let g:vim_vue_plugin_highlight_vue_keyword = 1
hi link cErrInParen NONE
hi link jsParensError NONE
let s:indent4 = [
\ 'make',
\ 'c',
\ 'cpp',
\ 'rust',
\ 'java',
\ 'kotlin',
\ 'python',
\ 'cuda',
\ 'masm',
\ 'tex',
\ 'nginx',
\ 'antlr.*',
\ ]
func! FileTypeConfig()
if s:vim8 && !s:gui
se shm-=F
end
setl fo-=r
setl fo-=o
call LSMap('nn', '<Space>s', ['up', 'call Restart()'], 0)
call LSMap('nn', '<F8>', 'Run()', 1)
call LSMap('nn', '<F9>', ['up', 'call Run()'], 1)
if &ft =~ printf('\v^(%s)$', join(s:indent4, '|'))
setl ts=4
end
if &ft =~ '\v^(make|caddyfile|gitconfig)$'
setl noet
end
if &ft =~ '\v^(vue)$'
setl isk+=-
end
if &ft =~ '\v^(c|cpp|java)$'
setl cino=:0,g0,N-s,(s,ws,Ws,m1,j1
if &ft != 'c'
setl cino+=J1
end
end
if &ft == 'html'
setl indk+=0],0)
end
if &ft =~ '\v^(python|ruby)$'
com! -bar NextDef call search('\v^\s*(def|class)>')
com! -bar PrevDef call search('\v^\s*(def|class)>', 'b')
call LSMap('no', ']f', ['NextDef'], 0)
call LSMap('no', '[f', ['PrevDef'], 0)
end
if &ft == 'make'
call LSMap('nn', '<F7>', ['up', 'call Make()'], 1)
end
if &ft =~ '\v^(c|cpp)$'
call LSMap('nn', '<F5>', 'Debug()', 0)
call LSMap('nn', '<F6>', 'Compile(["-O2"])', 1)
call LSMap('nn', '<F7>', 'Compile(["-g3"])', 1)
call LSMap('nn', '<F9>', ['call Compile(["-g3"])', 'call Run()'], 1)
end
if &ft =~ '\v^(java|cuda|masm|tex)$'
call LSMap('nn', '<F7>', 'Compile()', 1)
call LSMap('nn', '<F9>', ['call Compile()', 'call Run()'], 1)
end
if &ft == 'rust'
call LSMap('nn', '<F7>', ['up', 'Ctest'], 0)
call LSMap('nn', '<F8>', ['up', 'exe "Crun " . g:run_args'], 0)
call LSMap('nn', '<F9>', ['up', 'exe "Crun " . g:run_args'], 0)
end
if &ft =~ '^s[ca]ss$'
call LSMap('nn', '<F7>', 'Compile()', 1)
end
if &ft == 'sh'
com! -bar -range Upper <line1>,<line2>s/\v\$\w+>|<\w+\=/\U&/g
end
if &ft == 'tex'
setl inde=
setl nocul
setl nocuc
setl wrap
ino <buffer> <Tab> \
ino <buffer> \ <Tab>
call LSMap('nn', '<F5>', 'InsertTeXEnv()', 0)
call LSMap('ino', '<F5>', 'InsertTeXEnv()', 0)
end
if &ft == 'markdown'
setl wrap
call LSMap('nn', '<F8>', 'Run()', 0)
call LSMap('ino', '<F8>', 'Run()', 0)
let &cms = '[%%]: # (%s)'
end
if &ft =~ '^eruby'
ino <buffer> <C-\>d <% %><Left><Left><Left>
ino <buffer> <C-\>f <%= %><Left><Left><Left>
end
endf
func! BinReadPost()
if expand('%:e') != 'txt' && &fenc == 'latin1'
let b:bin = 1
se ft=text
setl fdm=manual
setl bin
%!xxd
setl nobin
end
endf
func! BinWritePre()
if get(b:, 'bin', 0)
let b:cursor_pos = getpos('.')
%y c
%s/\v^(\x{8}:)? ?((\x\x)+( (\x\x)+)*)?( *| .*)$/\2/g
%!xxd -p -r
setl bin
end
endf
func! BinWritePost()
if get(b:, 'bin', 0)
setl nobin
%d _
put! c
$d _
call setpos('.', b:cursor_pos)
end
endf
aug binary_config
au!
au BufRead * sil call BinReadPost()
" au BufWrite * sil call BinWritePre()
" au BufWritePost * sil call BinWritePost()
aug END
func! PureTextConfig()
if &ft =~ '\v^(text)$' && &bt != 'help'
setl nocuc
setl nocul
setl wrap
if s:win_gui
" setl slm=mouse
" if wordcount()['chars'] == 0
" star
" end
end
end
endf
aug file_type_config
au!
au BufEnter,FileType * call FileTypeConfig()
au BufRead,BufNewFile * call PureTextConfig()
aug END
func! Chdir(dir)
let dir = a:dir
if s:win
let dir = substitute(dir, '\\', '/', 'g')
end
let dir = substitute(dir, '/\?$', '/', '')
exe 'cd' escape(dir, " \t\n\\")
endf
func! AutoChdir()
if exists('b:tarfile') || expand('%') =~ '^\w\+:'
return
end
let dir = expand('%:h')
if isdirectory(dir)
call Chdir(dir)
elseif !empty(dir) && !exists('b:no_dir')
let b:no_dir = 1
echoe printf("Directory '%s' does not exist", dir)
end
endf
func! NerdTreeChdir()
if !exists('b:NERDTree')
return
end
let path = b:NERDTree.root.path
if s:win
let dir = join([path.drive] + path.pathSegments, '\')
else
let dir = join([''] + path.pathSegments, '/')
end
call Chdir(dir)
endf
aug auto_chdir
au!
au BufEnter * call AutoChdir()
au FileType,BufEnter * call NerdTreeChdir()
aug END
" ---------------------------- }}}
" Functions ------------------ {{{
" Utils ---------------------- {{{
func! Strip(str, ...)
let pat = get(a:, 1, '\_s*')
return substitute(a:str, '\v%^' . pat . '(.{-})' . pat . '%$', '\1', '')
endf
func! LStrip(str, ...)
let pat = get(a:, 1, '\_s*')
return substitute(a:str, '\v%^' . pat . '(.{-})%$', '\1', '')
endf
func! RStrip(str, ...)
let pat = get(a:, 1, '\_s*')
return substitute(a:str, '\v%^(.{-})' . pat . '%$', '\1', '')
endf
" Works like finddir() or findfile() with `;`
func! FindUpward(filename, ...)
let dir = a:0 >= 2 ? a:2 : expand('%:h')
if !isdirectory(dir)
return
end
" On Linux, strangely the trailing `..` will be expanded to `../` instead of
" being resolved, but `../` will be resolved.
" So, expanding it twice should effectively resolve all `..`.
" Also, `:p:p` works the same as `:p`, therefore isn't useful.
let dir = fnamemodify(fnamemodify(dir, ':p'), ':p')
let max_level = get(a:, 1, -1)
let level = 0
while 1
let fullpath = dir . a:filename
if !empty(glob(fullpath))
return fullpath
end
if max_level >= 0 && level >= max_level
return
end
" Note that the result of `:h` isn't consistent. It usually doesn't have a
" trailing slash, but could be a single `/`.
let parent_dir = fnamemodify(dir . '../', ':p')
if parent_dir == dir
return
end
let dir = parent_dir
let level += 1
endw
endf
func! GetText(lp, rp)
let pos = getpos('.')
let reg = @"
" Set character-wise Visual mode
normal! vv
call setpos("'<", a:lp)
call setpos("'>", a:rp)
normal! gvy
let text = @"
let @" = reg
call setpos('.', pos)
return text
endf
func! EchoErr(msg)
echoh ErrorMsg
echom (a:msg =~# '^E\d\+:' ? '' : 'Error: ') . a:msg
echoh None
endf
com! -bar -nargs=1 EchoErr call EchoErr(<args>)
func! TryExec(cmd, ...)
let s:exception = ''
let pat = get(a:, 1, '.*')
try
exe a:cmd
catch
if v:exception !~# '^Vim\%((\a\+)\)\?:E' . pat
echoe v:exception
end
let s:exception = v:exception
EchoErr substitute(v:exception, '^Vim\%((\a\+)\)\?:', '', '')
endt
endf
func! Input(prompt)
call inputsave()
let value = input(a:prompt)
call inputrestore()
return value
endf
func! MakeUpper()
let pos = getpos('.')
let pos[2] -= 1
call setpos('.', pos)
normal! gUiw
let pos[2] += 1
call setpos('.', pos)
endf
func! Silent(cmd)
return printf("exe 'normal! :%s<C-v><CR>'", substitute(a:cmd, "'", "''", 'g'))
endf
func! LSMap(map, key, cmd, expect_pause)
if type(a:cmd) == 3
let cmd_list = a:cmd
elseif type(a:cmd) == 1
let cmd_list = ['call ' . a:cmd]
end
if a:expect_pause && !s:win_gui
call map(cmd_list, 'Silent(v:val)')
let cmd_list += ["sil exe \"!bash -c 'read -sN1'\"", 'redraw!']
end
let cmd = ':<C-u>' . join(cmd_list, '<Bar>') . '<CR>'
if a:map[0] ==# 'i'
let cmd = '<C-o>' . cmd
end
let cmd = printf('%s <buffer> <silent> %s %s', a:map, a:key, cmd)
if a:expect_pause && s:win_gui
let cmd .= '<CR>'
end
exe cmd
endf
" }}}
" Windows Only {{{
" Call run.exe written in AHK
func! WinOpen(name, ...)
let envs = [$VIMRUNTIME, $VIM, $MYVIMRC]
let [$VIMRUNTIME, $VIM, $MYVIMRC] = ['', '', '']
let cmd = '!run ' . a:name
if !get(a:, 1, 1)
exe cmd
else
sil exe cmd
if !s:win_gui
redraw!
end
end
let [$VIMRUNTIME, $VIM, $MYVIMRC] = envs
endf
func! VimTweak(func, arg)
try
call libcallnr('vimtweak64.dll', a:func, a:arg)
catch
call libcallnr('vimtweak.dll', a:func, a:arg)
endt
endf
com! -bar -nargs=1 SetAlpha call VimTweak('SetAlpha', <args>)
" }}}
" Compile & Run -------------- {{{
let g:makefile_level = -1
func! Make(...)
let makefile = FindUpward('Makefile', g:makefile_level)
if !empty(makefile)
let dir = fnamemodify(makefile, ':h:S')
let make = a:0 < 2 ? 'make' : shellescape(a:2)
let target = a:0 < 1 ? '' : shellescape(a:1)
exe printf('!cd %s && %s %s', dir, make, target)
return 1
end
endf
let g:cpp_std = 17
let g:c_std = 99
let g:cxxflags = []
let g:cflags = []
let g:ldlibs = []
if s:win
call add(g:ldlibs, '-Wl,--stack=268435456')
end
func! Compile(...)
up
if &ft =~ '\v^(c|cpp)$'
if !Make()
if &ft == 'cpp'
let bin = 'g++'
if !s:win && g:cpp_std == 20
let bin = 'g++-11'
end
let std = 'c++' . g:cpp_std
let flags = copy(g:cxxflags)
else
let bin = 'gcc'