-
Notifications
You must be signed in to change notification settings - Fork 14
/
_vimrc
executable file
·1873 lines (1712 loc) · 56.3 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
" nocapatible with vi
set nocompatible
" ============================ favorite files and direcotries {{{2
let $favorites=''
if has("win32")
let $favorites='E:/Material/Linux/Vim/config/favorites.vim'
endif
if $favorites != ""
so $favorites
endif
" ============================ appearance =============================== {{{2
" ---------- indent {{{3
set autoindent " ai
set sw=2 " shift width, indent width
set tabstop=2 " ts, tabstop width
set tw=80
" set et " extendtab by default
set modelines=5
" --------- display as more lines as possible, do not use @@ {{{3
set display=lastline " dy=lastline
set backspace=indent,eol,start
set number
set encoding=utf-8
set fencs=utf8,gbk,gb2312,cp936,gb18030
" scrolling {{{3
set scrolloff=3 "margin of moving to top or bot of current screen
set sidescroll=1 " horizontal scroll step, continous scroll
" ---------- folding option {{{3
" marker manual indent
set foldmethod=marker
set foldenable
set fdl=8
" foldlevel
" --------- status line, command line {{{3
" ATTN: statusline configuration is overriden by plugin statusline.vim
set statusline=%<%F\ %y\ %m%r%=0x%B\ %l/%L,%c%V\ [b%nw%{winnr()}]
" always display status line, 0 never, 1 more than 2 windows, 2 always
set laststatus=2
" ruler, command line appearance, if laststatus == 2, ruler is useless
set noruler " ru
set rulerformat =%30(%<%y\ %m%r%=0x%B\ %l,%c%V\ [%n]\ %P%)
set showcmd " show command
" ---------- using system clipboard {{{3
set clipboard+=unnamed
" if has("win32")
" set ff=dos
".proceed else
" set ff=unix
" endif
" set right margin, only > 704 the cc option is available {{{3
if version >= 704
set cc=80 " colorcolumn=80 " cc=80
endif
" hilight current line
set cursorline
" hilight column
set cursorcolumn
" netrw explorer {{{3
let g:netrw_winsize = 20
let g:netrw_liststyle= 3 " archive
let g:netrw_menu = 0 " no menu
let g:netrw_preview = 1 " preview in vertical new window
let g:netrw_banner = 0 " no banner
let g:netrw_browse_split = 5 " when browsing, <cr> will open the file as "p"
let g:netrw_cursor = "ctags"
let g:netrw_chgwin = 2 " specifies a window number where file edits will take place
let g:netrw_use_errorwindow = 0 " dont use an extra window for errors
let g:netrw_indicate_current_file = 1 " auto change cursor of the tree in netrw
let g:netrw_winsize_ratio = 0.166666 " auto resize netrw width when change window
let g:netrw_show_path = 1
" menu bar {{{3
if has("gui")
" set window(gui) size
if has("win32")
" ~x max ~n min ~r restore
autocmd GUIEnter * simalt ~x
" set lines=25
" set columns=100
endif
" toggle menu bar, tool bar, scroll bar
" guioptions == go
set guioptions-=T " += is on, -= is off
set guioptions-=m " menu bar
set go-=M " system menu
set go-=R " right scroll
set go-=r " do not show right scroll
set go-=L " left
set go-=l
" set selectmode+=mouse
endif
" ---------- disable mouse under unix system {{{3
if has("unix")
set mouse=
endif
" theme {{{3
if &term == 'vt100' || &term == 'xterm'
set term=xterm-256color
endif
" if &term == 'screen'
set t_Co=256
" end
colorscheme Monokai_Gavin
set guifont=Consolas:h13
" echo &t_Co
syntax enable
syntax on
" show white spaces {{{3
set listchars=tab:>-,trail:-
" set listchars=tab:\|\ ,trail:-
set list
" set tablabel as 'number filename' for gui{{{3
if has("gui")
function! GuiTabLabel()
" Add '*' if one of the buffers in the tab page is modified
" let bufnrlist = tabpagebuflist(v:lnum)
" for bufnr in bufnrlist
" if getbufvar(bufnr, "&modified")
" let label = '*'
" break
" endif
" endfor
let bufnrlist = tabpagebuflist(tabpagenr())
" define some "useless" window whose title should not display in tablable
let uselessWindow = ["__Tagbar__", "NetrwTreeListing", "__Tag_List__"]
let fileName = bufname(bufnrlist[winnr() - 1])
for each in uselessWindow
if fileName =~ each
if !exists('t:label')
let t:label = tabpagenr() . ' '
endif
return t:label
endif
endfor
let t:winCount = tabpagewinnr(tabpagenr(), '$')
for i in range(0, t:winCount - 1)
for each in uselessWindow
let eachFileName = bufname(bufnrlist[i])
if eachFileName =~ each
let t:winCount = t:winCount - 1
endif
endfor
endfor
if t:winCount > 1
let t:winCount = " " . t:winCount
else
let t:winCount = ''
endif
" add '*' if the current window(file) has been modified
if getwinvar(winnr(), "&modified")
let t:label = '*'
else
let t:label = ''
endif
let t:name = " " . expand("%:t")
if t:name == ' '
let t:name = ' [No Name]'
endif
let t:label = t:label . tabpagenr() . t:name . t:winCount
return t:label
endfunction
set guitablabel=%{GuiTabLabel()}
" equivalent, but no *
" set guitablabel=%N\ %t
endif
" ---------- custom tabs for terminal version {{{3
function! MyTabLabel(n)
let label = ''
let bufnrlist = tabpagebuflist(a:n)
" Add '*' if one of the buffers in the tab page is modified
for bufnr in bufnrlist
if getbufvar(bufnr, "&modified")
let label = '*'
break
endif
endfor
" define some "useless" window whose title should not display in tablable
let uselessWindow = ["__Tagbar__", "NetrwTreeListing", "__Tag_List__"]
" get window count of tabpage n
let winCount = tabpagewinnr(a:n, '$')
" dont count the "useless" window
for i in range(1, winCount)
for j in uselessWindow
let fileName = bufname(bufnrlist[i - 1])
if fileName =~ j
let winCount = winCount - 1
endif
endfor
endfor
" current window (winnr) of tab page n, get filename from winnr
let winnr = tabpagewinnr(a:n)
let fileName = bufname(bufnrlist[winnr - 1])
" filter "useless" filename, use other file name instead
let realWinCount = tabpagewinnr(a:n, '$')
for i in uselessWindow
if realWinCount < 2
break
endif
if fileName =~ i
for j in range(0, realWinCount - 1)
let tmpFileName = bufname(bufnrlist[j])
let foundProperName = 1
for k in uselessWindow
if tmpFileName =~ k
let foundProperName = 0
break
endif
endfor
if foundProperName
let fileName = tmpFileName
break
endif
endfor
break
endif
endfor
" get the filename, not the full path
let lastOccur = strridx(fileName, "/")
if (lastOccur > 0)
let fileName = strpart(fileName, lastOccur + 1, strlen(fileName))
endif
" if the current filename is netrw, delete the number in it, that's ugly
if fileName =~ "NetrwTreeListing"
let fileName = "NetrwTreeListing"
endif
let label = label . a:n . " " . fileName . (winCount > 1 ? " ".winCount : "")
return label
endfunction
function! MyTabLine()
let s = ''
for i in range(tabpagenr('$'))
" select the highlighting
if i + 1 == tabpagenr()
let s .= '%#TabLineSel#'
else
let s .= '%#TabLine#'
endif
" set the tab page number (for mouse clicks)
let s .= '%' . (i + 1) . 'T'
" the label is made by MyTabLabel()
let s .= ' %{MyTabLabel(' . (i + 1) . ')} %#TabLine#|'
endfor
" after the last tab fill with TabLineFill and reset tab page nr
let s .= '%#TabLineFill#%T'
" right-align the label to close the current tab page
if tabpagenr('$') > 1
let s .= '%=%#TabLine#%999Xclose'
endif
return s
endfunction
" %! means the things following it will be evaluated as expression
set tabline=%!MyTabLine()
augroup tabline
autocmd InsertEnter * if exists("g:StatusLineInsert") |
\ exe "hi TabLineSel " . g:StatusLineInsert |
\endif
autocmd InsertLeave * if exists("g:StatusLineNormal") |
\ exe "hi TabLineSel " . g:StatusLineNormal |
\endif
augroup end
" ============================== key mapping ============================ {{{2
" tab mappings {{{3
nmap <M-1> <ESC>1gt
nmap <M-2> <ESC>2gt
nmap <M-3> <ESC>3gt
nmap <M-4> <ESC>4gt
nmap <M-5> <ESC>5gt
nmap <M-6> <ESC>6gt
nmap <M-7> <ESC>7gt
nmap <M-8> <ESC>8gt
nmap <M-9> <ESC>9gt
nmap <M-t> <ESC>:tabnew<CR>
nmap <M-w> <ESC>:tabclose<CR>
map <C-Tab> <ESC>gt
imap <C-Tab> <ESC>gt
imap <C-F4> <ESC>:tabc<CR>
map <C-F4> <ESC>:tabc<CR>
" Use CTRL-S for saving, also in Insert mode {{{3
noremap <C-S> :update<CR>
vnoremap <C-S> <C-C>:update<CR>
inoremap <C-S> <C-O>:update<CR>
" copy current file full path
nnoremap yp :let fullPath = expand('%:p')<BAR>
\ if has('clipboard') <BAR> let register = "*" <BAR> else <BAR> let register = '"' <BAR>endif <BAR>
\ call setreg(register, fullPath)<BAR>
\ echo "copied full path: " . fullPath<CR>
" copy current directory
nnoremap yd :let fullPath = expand('%:p')<BAR>
\ if has('win32') <BAR> let slash = '\'<BAR> else <BAR> let slash = '/' <BAR> endif<BAR>
\ let lastSlash = strridx(fullPath, slash) <BAR>
\ if lastSlash > 0 <BAR> let path = strpart(fullPath, 0, lastSlash) <BAR> else <BAR> let path = fullPath <BAR> endif<BAR>
\ if has('clipboard') <BAR> let register = "*" <BAR> else <BAR> let register = '"' <BAR>endif <BAR>
\ call setreg(register, path)<BAR>
\ echo "copied directory: " . path . slash<CR>
" comment {{{3
let g:COMMENT_1 = 1 " add comment string at the begin of the line
let g:COMMENT_2 = 2 " add comment string before the first word of the line
let g:UNCOMMENT = 3 " uncomment
function! CommentImpl(commentStr, mode)
" echo a:commentStr
" execute "echo " . '"' . a:commentStr . '"'
let l:cs = ""
let l:cse = ""
if a:commentStr == "vim"
let l:cs = '"'
elseif a:commentStr == "xml"
let l:cs = "<!--"
let l:cse = " -->"
else
let l:cs .= a:commentStr
endif
let l:cs = escape(l:cs, '/\')
" comment
if a:mode == g:COMMENT_1
" comment codes, add commentStr at the begin of line
" exe 's!^!' . l:cs . ' !'
exe 's/\(^\s*\)\(.*\)/' . l:cs . ' \1\2' . l:cse . '/e'
elseif a:mode == g:COMMENT_2
" comment description, add commentStr before the first word
exe 's/\(^\s*\)\(.*\)/\1' . l:cs . ' \2' . l:cse . '/e'
elseif a:mode == g:UNCOMMENT " uncomment
exe 's/^\(\s*\)' . l:cs . ' \{0,1\}\(.*\)' . l:cse . '/\1\2/e'
endif
exe 'noh'
endfunction
function! Comment(mode)
" comment string is //
for tmp in ["cpp", "c", "java", "php", "javascript", "go", "scss", "proto",
\ "thrift", "yacc", "dot", "gv", "rust", "lex", "flex", "verilog",
\ "typescript", "groovy"]
if &ft == tmp
call CommentImpl("//", a:mode)
return "//"
endif
endfor
" comment string is "
if &ft == "vim"
call CommentImpl("vim", a:mode)
return "vim"
endif
" comment string is #
for tmp in ["python","sed","apache","bash","conf", "sh", "make", "cfg",
\ "gitignore", "zsh", "config", "jproperties", "properties", "yaml",
\ "cmake", "crontab", "awk", "expect", "gitconfig", "applescript", "perl",
\ "gnuplot", "terraform", "hcl"]
if &ft == tmp
call CommentImpl("#", a:mode)
return tmp
endif
endfor
" comment string is ;
if &ft == "dosini" || &ft == "ini" || &ft == "autohotkey"
call CommentImpl(";", a:mode)
return "dosini"
endif
" comment string is REM
if &ft == "dosbatch" || &ft == "cmd" || &ft == "bat"
let l:caseChanged = 0
let l:smartcaseChanged = 0
if &ignorecase == 0
set ignorecase
let l:caseChanged = 1
endif
if &smartcase == 1
set nosmartcase
let l:smartcaseChanged = 1
end
call CommentImpl("REM", a:mode)
if l:caseChanged == 1
set noignorecase
endif
if l:smartcaseChanged == 1
set smartcase
end
return "dosbatch"
endif
" comment string is '
if &ft == "vbs" || &ft == "vb"
call CommentImpl("'", a:mode)
return "vbs"
endif
" comment string is <!-- -->
for tmp in ["html", "xml", "axml", "htm", "xhtml", "ant", "svg"]
if &ft == tmp
call CommentImpl("xml", a:mode)
return "xml"
endif
endfor
" comment string is %
if &ft == "matlab" || &ft == "tex"
call CommentImpl("%", a:mode)
return "matlab"
endif
" comment string is --
if &ft == "lua" || &ft == "haskell" || &ft == "sql"
call CommentImpl("--", a:mode)
return &ft
endif
" lisp ;;
if &ft == "lisp"
call CommentImpl(";;", a:mode)
return "lisp"
endif
echohl Error
echo "No comment command support for " . &ft . " --- Gavin"
echohl None
endfunction
nmap <silent> cc :call Comment(COMMENT_1)<CR>
vmap <silent> cc :call Comment(COMMENT_1)<CR>
nmap <silent> CC :call Comment(COMMENT_2)<CR>
vmap <silent> CC :call Comment(COMMENT_2)<CR>
nmap <silent> cz :call Comment(UNCOMMENT)<CR>
nmap <silent> cx :call Comment(UNCOMMENT)<CR>
vmap <silent> cz :call Comment(UNCOMMENT)<CR>
vmap <silent> cx :call Comment(UNCOMMENT)<CR>
" executes building and running scripts according to the file type
function! Run()
if !exists('g:argv')
let g:argv = ''
endif
if type(g:argv) == type([])
let argv = ''
for i in g:argv
let argv = argv . " " . i
endfor
elseif type(g:argv) == type('')
let argv = g:argv
else
let argv = ''
endif
let cmd = ""
if &ft == "vim"
exe 'source %'
return "vim"
endif
for ext in ["cc", "cpp", "c", "cxx", "h", "hpp"]
if &ft == ext
if has("win32")
exe '!start cmd /c start "vim run cpp" g++.lnk "%:p"'
elseif has("unix") || has('linux') && executable('g++')
exe 'silent !clear; rm ~/tmp/vim.out 2>/dev/null;'
" 'isGdb="n";read -n 1 -t 3 -p "use gdb[yn]?" isGdb; echo "";' .
" let cmd = '!g++1000 -g -ggdb3 -Wall -latomic -pthread -std=c++2a "%:p" -o ~/tmp/vim.out;' .
let cmd = '!g++ -g -ggdb3 -Wall -pthread -std=c++2a "%:p" -o ~/tmp/vim.out;' .
\ 'if [ $? -eq 0 ]; then ' .
\ 'isGdb="n";read -t 3 "isGdb?use GDB?[y/n]? "; echo "";' .
\ 'if [ "x$isGdb" = "xy" ]; then '
" let cmd = '!g++ -std=c++17 -g -ggdb3 -Wall -pthread -lstdc++fs -static-libstdc++ -static-libgcc "%:p" -o ~/tmp/vim.out;' .
if has("unix")
let cmd = cmd . 'lldb -- ~/tmp/vim.out ' . argv . ';'
else
let cmd = cmd . 'gdb --args ~/tmp/vim.out ' . argv . ';'
endif
let cmd = cmd . 'else ~/tmp/vim.out ' . argv . ';fi;fi;'
exe cmd
endif
return &ft
endif
endfor
if (&ft == 'sh')
exe "!sh %;"
return "sh"
endif
if (&ft == 'bash')
exe "!bash %;"
return "bash"
endif
if (&ft == 'expect')
exe "!expect %;"
return "expect"
endif
if (&ft == 'markdown') " view in markdown previewer
if has('mac')
" exe '!killall Mou; mou %:p'
" exe '!killall MacDown; MacDown %:p'
exe '!killall Typora; sleep 0.3 && typora %:p'
elseif has('win32')
exe 'silent !start cmd /c start "markdown" markdown.lnk "%:p"'
endif
return "markdown"
endif
if (&ft == 'go')
if has('mac') || has('unix')
exe 'silent !clear; rm /tmp/go.out 2>/dev/null;'
exe '!export GOPATH=$GOPATH:`pwd`:`pwd`/..;' .
\ '(go build -o /tmp/go.out "%:p" && /tmp/go.out) || (clear; go test "%:p");'
elseif has("win32")
exe '!cls & del e:/temp/go.out 2>nul & ' .
\'go build -o e:/temp/go.out ' . expand("%:p") . ' && ' .
\'e:/temp/go.out & pause'
endif
return "go"
endif
if (&ft == 'python')
if has('linux') || has('unix')
exe '!python "%:p";'
endif
return &ft
endif
for tmp in ["html", "xml", "axml", "htm", "xhtml", "js", "javascript", "svg"]
if (&ft == tmp)
if has('unix')
exe '!chrome "file://%:p";'
elseif has('win32')
exe '!chrome.lnk "file://%:p";'
endif
return tmp
endif
endfor
if &ft == "haskell"
if executable('ghc') || has('linux') || has('unix')
exe 'silent !clear; rm /tmp/haskell.out /tmp/Main.o 2>/dev/null;'
exe '!ghc -o /tmp/haskell.out -odir /tmp/ -hidir /tmp/ "%:p" && /tmp/haskell.out;'
endif
return &ft
endif
if &ft == "java"
if executable("javac") && executable("java") && (has('linux') || has('unix'))
let mainclass = expand("%:r")
" let mainclass = input("Class to run: ")
let outdir = '/tmp'
let javafile = expand("%:p")
let classfile = outdir . '/' . mainclass . '.class'
" classpath is directory which contains jar files for classes neede
let classpath = outdir . ':' . getcwd()
let jarfile = outdir . '/java.jar'
exe 'silent !clear; rm ' . classfile . ' 2>/dev/null;'
exe '!javac ' . javafile . ' -d ' . outdir .
\ ' -classpath ' . classpath .
\ ' -sourcepath ' . getcwd() .
\ ' -encoding UTF8 ' .
\ ' && echo "Build successfully: ' . classfile . '"'
\ ' && cd ' . outdir .
\ ' && java -classpath ' . classpath .
\ ' ' . mainclass .
\ ' ' . g:argv
else
echohl Error
echo "Support linux/unix env with binary javac and java only!"
echohl None
endif
endif
if &ft == "awk"
if !executable("awk")
echohl Error
echo "No executable awk found"
echohl None
endif
exe '!awk -f ' . expand("%:p")
return 'awk'
endif
if &ft == "dot" || &ft == "gv"
if !HasRequiredCmd("dot")
return &ft
endif
if g:argv == "0"
let cmd='!dot -o ~/tmp/tmp.svg -Tsvg ' . expand("%:p") . ' && chrome ~/tmp/tmp.svg'
else
let cmd='!callgraph ' . expand("%:p") . ' ~/tmp/tmp.svg && chrome ~/tmp/tmp.svg'
endif
call RunWithPlat(cmd, '', '')
return 'dot'
endif
if &ft == "tex"
if !HasRequiredCmd('xelatex')
return &ft
endif
let cmd = '!xelatex ' . expand("%:p") . " -output-directory ~/tmp/" .
\ ' && mv ' . expand('%:r') . '.pdf ~/tmp/tmp.pdf && open ~/tmp/tmp.pdf'
call RunWithPlat(cmd, '', '')
return &ft
endif
if &ft == "gnuplot"
if !HasRequiredCmd("gnuplot")
return &ft
endif
" the output tmp path is set in gnuplot scirpt file, that's a contract
" between vim script and gnuplot script
let cmd='!gnuplot -c ' . expand("%:p") . ' && chrome ~/tmp/tmp.svg'
call RunWithPlat(cmd, '', '')
return &ft
endif
echohl Error
echo "Run() does not support " . &ft
echohl None
return ""
endfunction
" TODO: cmd is a list of commands
function! HasRequiredCmd(cmd)
if executable(a:cmd)
return 1
endif
echohl Error
echo "No executable " . a:cmd . " found"
echohl None
return 0
endfunction
function! RunWithPlat(mac, linux, win)
if has("mac")
exe a:mac
elseif has("unix") && has("linux")
exe a:linux
elseif has("win32")
exe a:win
else
echohl Error
echo "This plat is not supported by Run(), command not run"
echohl None
endif
endfunction
map <F5> :call Run()<CR>
let g:default_surround_mark='`'
nmap <F4> :call MakeSurround("normal")<CR>
vmap <F4> <ESC>:call MakeSurround("visual")<CR>
nmap <F8> :call MakeSurround("normal", g:default_surround_mark)<CR>
vmap <F8> <ESC>:call MakeSurround("visual", g:default_surround_mark)<CR>
" taglist, make tag file {{{3
function! MakeTags()
if &ft == "cpp"
" exe '!ctags -R --langmap=.h.inl.cxx.cc --c++-kinds=+p --fields=+iaSK --extra=+q --languages=c++'
exe '!ctags -R --exclude=.git --exclude=.svn --langmap=c++:+.inl+.cc+.h+.cxx -h +.inl --c++-kinds=+p --fields=+iaSK --extra=+q --languages=c++'
elseif &ft == "java"
exe '!ctags -R --exclude=.git --exclude=.svn --java-kinds=+p --fields=+iaS --extra=+q --languages=java'
elseif &ft == "php"
exe '!ctags -R --exclude=.git --exclude=.svn --php-kinds=+cidfvj --fields=+iaSK --fields=-k --extra=+q --languages=php'
elseif &ft == "javascript"
exe '!ctags -R --exclude=.git --exclude=.svn --javascript-kinds=+cfv --fields=+iaSK --fields=-k --extra=+q --languages=javascript'
endif
endfunction
map <F12> :call MakeTags()<CR>
" select all
" nmap <C-A> ggVG
" ---------- redirect for ESC {{{3
" imap fds <ESC>
" map fds <ESC>
" omap fds <ESC>
" imap FDS <ESC>:echo "CAPS_LOCK!"<CR><ESC>
" map FDS <ESC>:echo "CAPS_LOCK!"<CR><ESC>
" omap FDS <ESC>:echo "CAPS_LOCK!"<CR><ESC>
" cnoremap fds <C-U><ESC>
" cnoremap FDS <C-U><ESC>:echo "CAPS_LOCK!"<CR><ESC>
" deletion, use backspace as backspace key, using black hole register {{{3
nmap <silent> <BS> h"_x
vmap <silent> <BS> h"_x
" ---------- open selected file {{{3
vnoremap <M-g> "zy:!start cmd /c start npp_open_document.lnk "<C-R>z" "%:p" "vim"<CR>
vmap <M-g> "zy:!start cmd /c start npp_open_document.lnk "<C-R>z" "%:p" "vim"<CR>
" ---------- using * and # search for selected content in visual mode {{{3
vnoremap <silent> * :<C-U>
\let old_reg=getreg('"')<Bar>let old_regtype=getregtype('"')<CR>
\gvy/<C-R><C-R>=substitute(
\escape(@", '/\.*$^~['), '\_s\+', '\\_s\\+', 'g')<CR><CR>
\gV:call setreg('"', old_reg, old_regtype)<CR>
vnoremap <silent> # :<C-U>
\let old_reg=getreg('"')<Bar>let old_regtype=getregtype('"')<CR>
\gvy?<C-R><C-R>=substitute(
\escape(@", '?\.*$^~['), '\_s\+', '\\_s\\+', 'g')<CR><CR>
\gV:call setreg('"', old_reg, old_regtype)<CR>
" ---------- adjust the window size {{{3
" :resize n or :vertical resize n, where n is the size in height/width
" horizontal size, height
nmap <M-]> :resize +1<CR>
nmap <M-[> :resize -1<CR>
" vertical size, width
nmap <M-=> :vertical resize +1<CR>
nmap <M--> :vertical resize -1<CR>
" maximize window
nmap <F11> :simalt ~x<CR>
" ---------- auto complete key map {{{3
autocmd BufRead,BufEnter * call AutoCompletionKeyMap()
function! AutoCompletionKeyMap()
if &ft == "cpp"
imap <C-space> <C-x><C-o><C-p>
imap <C-l> <C-x><C-o><C-p>
else
imap <C-space> <C-n><C-p>
imap <C-l> <C-n><C-p>
endif
endfunc
imap <C-j> <C-n>
imap <C-k> <C-p>
" ---------- hjkl remap {{{3
nnoremap j gj
nnoremap k gk
nnoremap gj j
nnoremap gk k
vnoremap j gj
vnoremap k gk
vnoremap gj j
vnoremap gk k
" ---------- resize vertical explorer to width 30 {{{3
nmap <F3> :exe "vert res " . float2nr(g:netrw_winsize_ratio * g:max_win_width)<CR>
" ---------- build project {{{3
nmap <F7> :make clean; make -j7<CR>
" ---------- trim
nmap T :T<CR>
" ================================ misc ================================ {{{2
" where the swap file stored {{{3
if has('win32')
set dir=e:/temp/
elseif has('unix')
set dir=~/temp/,~/tmp/,/tmp/
endif
" ignore case while search {{{3
set ignorecase " noignorecase
set smartcase " if upper case letters are typed, case sensitive
" language
if has('win32')
language us
endif
set magic
set nobackup
set hlsearch
set incsearch "increase search, search when typing a pattern
filetype on
filetype plugin on
filetype indent on
if v:version >= 704
set noundofile
endif
" ---------- tags, taglist, file explorer {{{3
set tags=tags,./tags,../tags,../../tags,../../../tags
set tags+=~/cpp_stdlib.tags
let Tlist_Show_One_File = 1
let Tlist_Exit_OnlyWindow = 1
let Tlist_Use_Right_Window = 1
let Tlist_Enable_Fold_Column = 0
" php dictionary, not used anymore
" autocmd filetype php set dictionary-=E:/Material/Linux/Vim/config/php_function_list.txt
" autocmd filetype php set dictionary+=E:/Material/Linux/Vim/config/php_function_list.txt
"let g:winManagerWindowLayout='FileExplorer|TagList'
"nmap wm :WMToggle<cr>
" ---------- OmniCppComplete {{{3
" let g:OmniCpp_NamespaceSearch = 1
" let g:OmniCpp_GlobalScopeSearch = 1
" let g:OmniCpp_ShowAccess = 1
let g:OmniCpp_ShowPrototypeInAbbr = 1 " show function parameters
" let g:OmniCpp_MayCompleteDot = 1 " autocomplete after .
" let g:OmniCpp_MayCompleteArrow = 1 " autocomplete after ->
" let g:OmniCpp_MayCompleteScope = 1 " autocomplete after ::
let g:OmniCpp_DefaultNamespaces = ["std", "_GLIBCXX_STD", "__gnu_std"]
let g:OmniCpp_SelectFirstItem = 2 " select first popup item (without inserting it to the text)
" automatically open and close the popup menu / preview window
" au CursorMovedI,InsertLeave * if pumvisible() == 0|silent! pclose|endif
" set completeopt=menuone,menu,longest,preview
set completeopt=menuone,menu
au BufNewFile,BufRead,BufEnter *.cpp,*.hpp,*.h,*.cc set omnifunc=omni#cpp#complete#Main
" ---------- YouCompleteMe {{{3
let g:ycm_global_ycm_extra_conf = '~/.ycm_extra_conf.py'
let g:ycm_confirm_extra_conf = 0
" generate and use .cache folder in the working directory
let g:ycm_clangd_uses_ycmd_caching = 0
" steal from VS Code
" --background-index --clang-tidy --compile-commands-dir=/mnt/disk2/ftw/projects/incubator-doris/be/build_Release/ --completion-style=detailed -j=5 --all-scopes-completion --pch-storage=memory --pretty -log=verbose --query-driver=/mnt/disk2/ftw/tools/ldb_toolchain/bin/*
let g:ycm_clangd_args = ['-j=8', '--clang-tidy', '--completion-style=detailed', '--pch-storage=memory', '--pretty']
let g:ycm_max_num_candidates = 30
" mapping should be put to 'after' folder and check if exists('g:loaded_youcompleteme')
command! YcmOn if exists('g:loaded_youcompleteme') <BAR>
\let g:ycm_show_diagnostics_ui=1 <BAR>
\let g:ycm_auto_trigger = 1 <BAR>
\let g:ycm_auto_hover = 1 <BAR>
\let g:ycm_enable_diagnostic_signs = 1 <BAR>
\let g:ycm_enable_diagnostic_highlighting = 1 <BAR>
\set signcolumn=auto <BAR>
\let g:ycm_gavin_enable = 1 <BAR>
\exe 'nmap <c-]> :YcmCompleter GoTo<CR>'<BAR>
\exe 'YcmRestartServer' <BAR>
\endif
command! YcmOff if exists('g:loaded_youcompleteme') <BAR>
\let g:ycm_show_diagnostics_ui = 0 <BAR>
\set signcolumn=no <BAR>
\let g:ycm_auto_trigger = 0 <BAR>
\let g:ycm_auto_hover = 0 <BAR>
\let g:ycm_enable_diagnostic_signs = 0 <BAR>
\let g:ycm_enable_diagnostic_highlighting = 0 <BAR>
\let g:ycm_gavin_enable = 0 <BAR>
\exe 'nunmap <c-]>' <BAR>
\exe 'YcmRestartServer' <BAR>
\endif
command! YcmHover call youcompleteme#ToggleHover()
" ---------- virtual edit {{{3
set virtualedit=block
" ---------- text formatting option {{{3
" for multibyte text width line break, for text block: select lines, press Jgqgq
set formatoptions+=m
" ---------- set updatetime shorter, this may cause frequently disk writing,
" this opation is global only, may be changed by other plugins
set updatetime=200
" ---------- don't search cyclically
set nowrapscan
" ---------- show match () {} [] when typing them
set showmatch
" ---------- enter normal mode delay
set timeoutlen=1000 ttimeoutlen=5
" ---------- config spell
set spellsuggest=file:~/.vim/spell/spellsuggest.txt,best
set spelllang+=cjk
" ---------- vim 8.0+ may miss the helptags
" Built-in doc
if isdirectory($VIMRUNTIME . '/doc')
sil! helptags $VIMRUNTIME/doc
endif
" User defined help doc
if isdirectory($HOME . '.vim/doc')
sil! helptags $HOME/.vim/doc
endif
" ---------- path and find
set noautochdir " donot change dir
" with ** in path, we dont need to `:find **` manually for multi-level folder,
" or it may have performance defeacts
set path+=**
set wildmenu
set wildignorecase
set wildmode=longest:full,full
" fuzzy may mess-up matching calling command `:!`
set wildoptions=pum
" ================================ commands ============================ {{{2
" ---------- trim the heading/trailing whitespaces {{{3
function! RemoveTrailingWhitespace()
let lineNo = line(".")
let oldLine = getline(lineNo)
let newLine = substitute(oldLine, '\s\+$', '', 'e')
if strlen(oldLine) == strlen(newLine)
return
endif
call setline(lineNo, newLine)
endfunction
function! RemoveBeginningWhitespace()
let lineNo = line(".")
let oldLine = getline(lineNo)
let newLine = substitute(oldLine, '^\s\+', '', 'e')
if strlen(oldLine) == strlen(newLine)
return
endif
call setline(lineNo, newLine)
endfunction
command! -range Trim <line1>,<line2>call RemoveTrailingWhitespace()
command! -range T <line1>,<line2>call RemoveTrailingWhitespace()
command! -range TrimLeft <line1>,<line2>call RemoveBeginningWhitespace()
command! -range Tl <line1>,<line2>call RemoveBeginningWhitespace()
" ---------- rename current file {{{3
command! -nargs=+ -bang -complete=file Rename let newName = "<args>"<BAR>
\let curExt = expand("%:e")<BAR>
\let oldFullPath = expand("%:p")<BAR>
\exe "saveas<bang> " . newName<BAR>
\echo "renamed current file to: " . expand("%:t")<BAR>
\if newName == expand("%:t")<BAR>
\echo "suceeded to save the file " . newName<BAR>
\let delResult = delete(oldFullPath)<BAR>
\if delResult != 0<BAR>
\echo "fail to delete the old file: " . oldFullPath<BAR>
\endif<BAR>
\endif<BAR>
nmap <F2> :Rename
" ---------- make/load session {{{3
" command! -nargs=? -bang Mks mks<bang> $ses
command! -nargs=? -bang Mks silent echo "try to make session"<BAR>
\if "<args>" != ""<BAR>
\mks<bang> <args><BAR>
\echo "made session at: <args>"<BAR>
\else<BAR>
\mks<bang> ~/tmp/tmp.ses<BAR>
\echo "made session at: ~/tmp/tmp.ses"<BAR>
\endif
" command! -nargs=? -bang Loadsession so $ses <args>
command! -nargs=? -bang Loadsession silent echo "try to load session"<BAR>
\if "<args>" != ""<BAR>
\so <args><BAR>
\echo "loaded session from: <args>"<BAR>
\else<BAR>
\so ~/tmp/tmp.ses<BAR>
\echo "loaded session from: ~/tmp/tmp.ses"<BAR>
\endif<BAR>
" ---------- insert current time in the current position, after the cursor box {{{3
command! Time echo strftime("%Y-%m-%d-%a %H:%M:%S")<BAR>
" \"=strftime("%Y-%m-%d %H:%M:%S")<CR><BAR>
" \gP
" = register is the expression output register, 6. Expression register "= can only be used onece
" normal mode insert current time, and enter insert mode
nnoremap time "=strftime("%Y-%m-%d-%a %H:%M:%S")<CR>pa
nnoremap timelog "="\n## " . strftime("%Y-%m-%d-%a %H:%M:%S") . "\ntag: \n"<CR>PjjA
nnoremap todo gg"="\n## [ ] \ncreate: " . strftime("%Y-%m-%d-%a %H:%M:%S") . "\nstart: \nend: \ndesc: \n"<CR>PjA
" ---------- grep current keyword, the silver searcher
" The Silver Searcher
if executable('ag')
" Use ag over grep
set grepprg=ag\ --nogroup\ --nocolor
" Use ag in CtrlP for listing files. Lightning fast and respects .gitignore
let g:ctrlp_user_command = 'ag %s -l --nocolor -g ""'
" ag is fast enough that CtrlP doesn't need to cache
let g:ctrlp_use_caching = 0
endif
" add ! to override "prevent vim grep from opening first matching file"
" and "press Enter to continue"
nnoremap K :grep! "<C-R><C-W>"<CR>:rightbelow cw<CR>
vmap K :grep! "<C-R><C-W>"<CR>:rightbelow cw<CR>
" ---------- auto change IME to en {{{3
" for some type of files auto ime is needed
" autocmd! InsertLeave *.txt,*.md,*.tex,*.log call ChangeIme(g:autoChangeIme)
autocmd! InsertLeave *.txt,*.md,*.tex,*.log,* call ChangeIme(g:autoChangeIme)
let g:autoChangeIme = 1
function! ChangeIme(autoChangeIme)
let found = 0
for tmp in ["markdown", "md", "txt", "log", "tex", "plaintex", "latex"]
if &ft == tmp
let found = 1
break
endif
endfor
if has('win32') && a:autoChangeIme && found
exe '!start /MIN cmd /c start "change ime" /MIN changeVimIme2En.lnk'