-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
zf_vimrc.vim
2984 lines (2835 loc) · 114 KB
/
zf_vimrc.vim
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
" ============================================================
" http://zsaber.com/blog/p/31
" ============================================================
" global configs:
" let g:zf_no_submodule = 1
" let g:zf_no_ext = 1
" let g:zf_no_plugin = 1
" let g:zf_no_theme = 1
if 1 " global settings
filetype plugin indent on
syntax on
set nocompatible
" global functions
function! ZF_system(cmd)
if exists('g:ZF_system_log')
let startTime = reltime()
let ret = system(a:cmd)
let costTime = float2nr(reltimefloat(reltime(startTime, reltime())) * 1000)
call add(g:ZF_system_log, {
\ 'cmd' : a:cmd,
\ 'time' : costTime,
\ })
return ret
else
return system(a:cmd)
endif
endfunction
function! ZF_rm(dir)
if g:zf_windows
silent! call ZF_system('rmdir /s/q "' . substitute(a:dir, '/', '\', 'g') . '"')
else
silent! call ZF_system('rm -rf "' . substitute(a:dir, '\', '/', 'g') . '"')
endif
endfunction
function! CygpathFix_absPath(path)
if len(a:path) <= 0|return ''|endif
if !exists('g:CygpathFix_isCygwin')
let g:CygpathFix_isCygwin = has('win32unix') && executable('cygpath')
endif
let path = fnamemodify(a:path, ':p')
if !empty(path) && g:CygpathFix_isCygwin
if 0 " cygpath is really slow
let path = substitute(system('cygpath -m "' . path . '"'), '[\r\n]', '', 'g')
else
if match(path, '^/cygdrive/') >= 0
let path = toupper(strpart(path, len('/cygdrive/'), 1)) . ':' . strpart(path, len('/cygdrive/') + 1)
else
if !exists('g:CygpathFix_cygwinPrefix')
let g:CygpathFix_cygwinPrefix = substitute(system('cygpath -m /'), '[\r\n]', '', 'g')
endif
let path = g:CygpathFix_cygwinPrefix . path
endif
endif
endif
return substitute(substitute(path, '\\', '/', 'g'), '\%(\/\)\@<!\/\+$', '', '') " (?<!\/)\/+$
endfunction
let g:zf_vimrc_path = CygpathFix_absPath(expand('<sfile>'))
" env
let g:zf_windows = 0
if (has('win32') || has('win64')) && !has('unix')
let g:zf_windows = 1
endif
if has('win32unix') && executable('cygpath')
let g:zf_cygwin = 1
else
let g:zf_cygwin = 0
endif
let g:zf_mac = 0
if has('unix')
try
silent! let s:uname = ZF_system('uname')
if match(s:uname, 'Darwin') >= 0
let g:zf_mac = 1
endif
endtry
endif
let g:zf_linux = 0
if has('unix')
let g:zf_linux = 1
endif
if !g:zf_windows && !g:zf_mac
let g:zf_linux = 1
elseif g:zf_linux
let g:zf_windows = 0
endif
if !exists('g:zf_fakevim')
let g:zf_fakevim = 0
endif
if !exists('g:zf_githost')
let g:zf_githost = 'https://github.com'
endif
if !exists('g:zf_vim_home_path')
let g:zf_vim_home_path = CygpathFix_absPath($HOME)
endif
if !exists('g:zf_vim_data_path')
let g:zf_vim_data_path = g:zf_vim_home_path . '/.vim'
endif
if !exists('g:zf_vim_plugin_path')
let g:zf_vim_plugin_path = g:zf_vim_data_path . '/bundle'
endif
if !exists('g:zf_vim_cache_path')
let g:zf_vim_cache_path = g:zf_vim_home_path . '/.vim_cache'
endif
if !isdirectory(g:zf_vim_cache_path)
call mkdir(g:zf_vim_cache_path, 'p')
endif
if !exists('g:zf_vim_viminfo_path')
if has('nvim')
let g:zf_vim_viminfo_path = g:zf_vim_cache_path . '/viminfo_nvim'
else
let g:zf_vim_viminfo_path = g:zf_vim_cache_path . '/viminfo'
endif
endif
" leader should be set before other key map
if g:zf_fakevim != 1
let mapleader = "'"
else
let mapleader = '\'
map ' <leader>
endif
" turn on this to improve performance (especially for some low performance shell or ssh)
if !exists('g:zf_low_performance')
let g:zf_low_performance = !has('nvim') && !has('gui') && v:version <= 800
endif
augroup ZF_VimLowPerf_augroup
autocmd!
autocmd User ZFVimLowPerf silent
augroup END
function! ZF_VimLowPerfToggle()
let g:zf_low_performance = !g:zf_low_performance
if g:zf_low_performance
echo 'low performance mode on'
else
echo 'low performance mode off'
endif
doautocmd User ZFVimLowPerf
endfunction
command! -nargs=0 ZFLowPerfToggle :call ZF_VimLowPerfToggle()
" git info
if !exists('g:zf_git_user_email')
let g:zf_git_user_email = 'z@zsaber.com'
endif
if !exists('g:zf_git_user_name')
let g:zf_git_user_name = 'ZSaberLv0'
endif
" you may set g:zf_git_user_token to git push without password
function! ZF_GitGlobalConfig()
call ZF_system('git config --global user.email "' . g:zf_git_user_email . '"')
call ZF_system('git config --global user.name "' . g:zf_git_user_name . '"')
call ZF_system('git config --global core.filemode false')
call ZF_system('git config --global core.autocrlf false')
call ZF_system('git config --global core.safecrlf true')
call ZF_system('git config --global core.quotepath false')
echo 'git global user changed to ' . g:zf_git_user_name . ' <' . g:zf_git_user_email . '>'
endfunction
command! -nargs=0 ZFGitGlobalConfig :call ZF_GitGlobalConfig()
endif " global settings
if 1 && !get(g:, 'zf_no_submodule', 0) " source local config before ext
for f in sort(split(globpath(g:zf_vim_data_path . '/ZFVimModule', '*.vim'), "\n"))
execute 'source ' . fnameescape(f)
endfor
for f in sort(split(globpath(g:zf_vim_data_path . '/ZFVimModule', '*/*.vim'), "\n"))
execute 'source ' . fnameescape(f)
endfor
endif
if 1 && !get(g:, 'zf_no_ext', 0)
let g:zf_vimrc_ext_path = g:zf_vim_data_path . '/ZFVimModule/zf_vimrc.ext'
if !filereadable(g:zf_vimrc_ext_path . '/README.md')
call ZF_system('git clone --depth=1 ' . g:zf_githost . '/ZSaberLv0/zf_vimrc.ext "' . g:zf_vimrc_ext_path . '"')
endif
if !exists('g:ZFVimrcUtil_updateCallback')
let g:ZFVimrcUtil_updateCallback = {}
endif
let g:ZFVimrcUtil_updateCallback['zf_vimrc.ext'] = 'ZF_VimrcExtUpdate'
function! ZF_VimrcExtUpdate()
let remote = ZF_system('cd "' . g:zf_vimrc_ext_path . '" && git remote get-url --all origin')
let remote = substitute(remote, '[\r\n]', '', 'g')
if match(remote, '^[a-z]\+://') < 0
let remote = ZF_system('cd "' . g:zf_vimrc_ext_path . '" && git remote -v')
let remote = matchstr(remote, '\%(origin[ \t]\+\)\@<=[^ \t]\+\%([ \t]\+(push)\)\@=')
let remote = substitute(remote, '://.\+@', '://', '')
endif
if empty(remote) || match(remote, g:zf_githost) < 0
call ZF_rm(g:zf_vimrc_ext_path)
call ZF_system('git clone --depth=1 ' . g:zf_githost . '/ZSaberLv0/zf_vimrc.ext "' . g:zf_vimrc_ext_path . '"')
else
call ZF_system('cd "' . g:zf_vimrc_ext_path . '" && git fetch --all && git reset --hard origin/master && git pull')
endif
endfunction
function! ZF_VimrcExtEdit()
execute 'edit ' . substitute(g:zf_vimrc_ext_path, ' ', '\\ ', 'g')
endfunction
nnoremap <leader>vimre :call ZF_VimrcExtEdit()<cr>
endif
if 1 && !get(g:, 'zf_no_submodule', 0) " sub modules
" extra install steps for sub modules
let g:zfmoduleInstallerList = []
function! ZF_ModuleInstaller(name, cmd, ...)
if get(g:, 'ZFModule_' . a:name, get(a:, 1, 1))
call add(g:zfmoduleInstallerList, {'name' : a:name, 'cmd' : a:cmd})
endif
endfunction
function! s:ZF_ModuleInstallAction()
if empty(g:zfmoduleInstallerList)
echo '[ZFVimrc] no sub module to install'
return
endif
for item in g:zfmoduleInstallerList
redraw
echo '============================================================'
echo '[ZFVimrc] updating ' . item.name
if type(item.cmd) == type('')
execute '' . item.cmd
else
call item.cmd()
endif
endfor
redraw
echo '============================================================'
echo '[ZFVimrc] sub module update finished'
messages
endfunction
function! s:ZF_ModuleInstall()
let moreSaved = &more
set nomore
let tmpfile = g:zf_vim_cache_path . '/ZFModuleInstall.log'
silent! call delete(tmpfile)
try
execute 'redir > ' . tmpfile
call s:ZF_ModuleInstallAction()
finally
redir END
endtry
enew
execute 'edit ' . substitute(tmpfile, ' ', '\\ ', 'g')
let &more = moreSaved
let @/ = '.*\(zfvimrc!\|error\|fail\|unable\|exception\|not found\|ambiguous\|err!\).*'
call histadd('/', @/)
call feedkeys('ggn', 'nt')
redraw!
echo '[ZFVimrc] update finish'
endfunction
command! -nargs=0 ZFModuleInstall :call s:ZF_ModuleInstall()
function! ZF_ModuleInstallFail(msg)
echo '[ZFVimrc!] ' . a:msg
endfunction
function! ZF_ModuleGetApt()
if !exists('s:ZF_ModuleGetApt')
let s:ZF_ModuleGetApt = ''
if !empty(globpath(substitute($PATH, ';', ',', 'g'), 'apt-cyg'))
let s:ZF_ModuleGetApt = 'sh -c "yes | apt-cyg install \"%s\""'
elseif executable('apt-get')
let s:ZF_ModuleGetApt = 'yes | apt-get install "%s"'
elseif executable('yum')
let s:ZF_ModuleGetApt = 'yes | yum install "%s"'
elseif executable('brew')
let s:ZF_ModuleGetApt = 'yes | brew install "%s"'
elseif executable('apt')
let s:ZF_ModuleGetApt = 'yes | apt install "%s"'
elseif executable('sh') " other special
if !empty(globpath(substitute($PATH, g:zf_windows ? ';' : ':', ',', 'g'), 'apt-get'))
let s:ZF_ModuleGetApt = 'sh -c "yes | apt-get install \"%s\""'
endif
endif
endif
if empty('s:ZF_ModuleGetApt')
call ZF_ModuleInstallFail('unable to find package manager (apt-get/yum/brew)')
endif
return s:ZF_ModuleGetApt
endfunction
function! ZF_ModuleGetPip()
if !exists('s:ZF_ModuleGetPip')
if executable('pipx')
let s:ZF_ModuleGetPip = 'pipx install "%s"'
elseif executable('pip3')
let s:ZF_ModuleGetPip = 'pip3 install "%s"'
elseif executable('pip')
let s:ZF_ModuleGetPip = 'pip install "%s"'
else
let s:ZF_ModuleGetPip = ''
endif
endif
if empty('s:ZF_ModuleGetPip')
call ZF_ModuleInstallFail('unable to find pip')
endif
return s:ZF_ModuleGetPip
endfunction
function! ZF_ModuleGetNpm()
if !exists('s:ZF_ModuleGetNpm')
if executable('npm')
let s:ZF_ModuleGetNpm = 'npm install -g "%s"'
else
let s:ZF_ModuleGetNpm = ''
endif
endif
if empty('s:ZF_ModuleGetNpm')
call ZF_ModuleInstallFail('unable to find npm')
endif
return s:ZF_ModuleGetNpm
endfunction
function! ZF_ModuleGetGithubRelease(userName, repoName)
if !exists('s:ZF_ModuleGetGithubRelease')
let userName = get(g:, 'ZF_ModuleGetGithubRelease_git_user_name', get(g:, 'zf_git_user_name', ''))
let userToken = get(g:, 'ZF_ModuleGetGithubRelease_git_user_token', get(g:, 'zf_git_user_token', ''))
let api = 'https://api.github.com/repos/%s/%s/releases/latest'
if executable('curl')
let s:ZF_ModuleGetGithubRelease = 'curl'
if !empty(userName) && !empty(userToken)
let s:ZF_ModuleGetGithubRelease .= printf(' --header "authorization: %s %s"', userName, userToken)
endif
let s:ZF_ModuleGetGithubRelease .= ' -s ' . api
elseif executable('wget')
let s:ZF_ModuleGetGithubRelease = 'wget'
if !empty(userName) && !empty(userToken)
let s:ZF_ModuleGetGithubRelease .= printf(' --header="authorization: %s %s"', userName, userToken)
endif
let s:ZF_ModuleGetGithubRelease .= ' -qO- ' . api
else
let s:ZF_ModuleGetGithubRelease = ''
endif
if !empty(s:ZF_ModuleGetGithubRelease)
let s:ZF_ModuleGetGithubRelease .= ' | grep "browser_"'
endif
endif
if empty('s:ZF_ModuleGetGithubRelease')
return []
endif
let list = ZF_system(printf(s:ZF_ModuleGetGithubRelease, a:userName, a:repoName))
if match(list, 'browser_') < 0
return []
endif
let ret = []
for item in split(list, "\n")
" http[^"]*
call add(ret, matchstr(item, 'http[^"]*'))
endfor
return ret
endfunction
function! ZF_ModuleExecShell(cmd)
echo a:cmd
let msg = ZF_system(a:cmd)
for item in split(msg, "\n")
echo item
endfor
return msg
endfunction
function! ZF_ModuleExec(fmt, ...)
if empty(a:fmt)
return
endif
let tmp = 'let cmd = printf(a:fmt'
if a:0 == 1 && type(a:1) == type([])
for i in range(len(a:1))
let tmp .= ', a:1[' . i . ']'
endfor
else
for i in range(a:0)
let tmp .= ', a:' . (i+1)
endfor
endif
let tmp .= ')'
execute tmp
return ZF_ModuleExecShell(cmd)
endfunction
function! ZF_ModulePackAdd(pm, packages)
if empty(a:pm)
return
endif
for package in split(a:packages, ' ')
call ZF_ModuleExec(a:pm, package)
endfor
endfunction
function! ZF_ModuleDownloadFile(to, url)
if !exists('s:ZF_ModuleDownloadFile')
if executable('curl')
let s:ZF_ModuleDownloadFile = 'curl -o "%s" -L "%s"'
let s:ZF_ModuleDownloadFile_sizeGetter = 'curl -sI "%s"'
elseif executable('wget')
let s:ZF_ModuleDownloadFile = 'wget -P "%s" "%s"'
let s:ZF_ModuleDownloadFile_sizeGetter = 'curl -sI "%s"'
else
let s:ZF_ModuleDownloadFile = ''
let s:ZF_ModuleDownloadFile_sizeGetter = ''
endif
endif
if empty(s:ZF_ModuleDownloadFile)
call ZF_ModuleInstallFail('no curl or wget available')
return 0
endif
let tmp = tempname()
let to = substitute(a:to, '\\', '/', 'g')
let parent = fnamemodify(CygpathFix_absPath(to), ':h')
if !isdirectory(parent)
call mkdir(parent, 'p')
endif
if filereadable(to)
let existSize = getfsize(to)
if existSize >= 0
let remoteSize = -1
for line in split(ZF_system(printf(s:ZF_ModuleDownloadFile_sizeGetter, a:url)), '[\r\n]')
if match(line, '\ccontent-length') >= 0
let remoteSize = str2nr(matchstr(line, '[0-9]\+'))
break
endif
endfor
if remoteSize == existSize
return 1
endif
endif
endif
let ret = ZF_system(printf(s:ZF_ModuleDownloadFile, substitute(tmp, '\\', '/', 'g'), a:url))
if v:shell_error != 0
call ZF_ModuleInstallFail('unable to download (' . v:shell_error . '), url: ' . a:url)
return 0
endif
call writefile(readfile(tmp, 'b'), to, 'b')
call delete(tmp)
return 1
endfunction
function! ZF_ModuleGitClone(repo, toPath)
if filereadable(a:toPath . '/.git/HEAD')
return
endif
call ZF_rm(a:toPath)
echo ZF_system('git clone --depth=1 ' . a:repo . ' "' . substitute(a:toPath, '\', '/', 'g') . '"')
endfunction
" steps: ZFInit, ZFPlugPrev, ZFPlugPost, ZFFinish
function! s:subModule(step)
for f in sort(split(globpath(g:zf_vim_data_path . '/ZFVimModule', '*/' . a:step . '/**/*.vim'), "\n"))
execute 'source ' . fnameescape(f)
endfor
endfunction
call s:subModule('ZFInit')
else
function! ZF_ModuleInstaller(name, cmd)
endfunction
function! s:subModule(step)
endfunction
endif " sub modules
" ==================================================
if 1 " custom key mapping
" esc
if !g:zf_fakevim
function! ZF_Setting_ijk()
call feedkeys((getpos('.')[2] == 1) ? "\<esc>" : "\<esc>l", 'nt')
return ''
endfunction
inoremap <esc> <esc>l
inoremap <expr> jk ZF_Setting_ijk()
cnoremap jk <c-c>
else
noremap <esc> <esc>
inoremap jk <esc>l
cnoremap jk <esc>
endif
if !g:zf_fakevim
nnoremap <space> <esc>
xnoremap <space> <esc>
onoremap <space> <esc>
else
noremap <space> <esc>
endif
" visual
if !g:zf_fakevim
nnoremap V <c-v>
xnoremap V <c-v>
nnoremap <c-v> V
xnoremap <c-v> V
else
noremap V <c-v>
noremap <c-v> V
endif
" special select mode mapping
" some plugin use vmap, which would cause unexpected behavior under select mode
function! ZF_Setting_SelectModeMap()
let s:_selectmode_keys = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~!@#$%^&*-_=+\;:,./?)]}>'
for c in split(s:_selectmode_keys, '\ze')
silent! execute 'snoremap <silent> ' . c . ' <c-g>"_c' . c
endfor
let s:_selectmode_keys = '|'
for c in split(s:_selectmode_keys, '\ze')
let c = '\' . c
silent! execute 'snoremap <silent> ' . c . ' <c-g>"_c' . c
endfor
silent! snoremap <silent> <space> <esc>
silent! snoremap <silent> jk <esc>gv
silent! snoremap <silent> <bs> <c-g>"_c
silent! snoremap <silent> ( <c-g>"_c()<esc>i
silent! snoremap <silent> [ <c-g>"_c[]<esc>i
silent! snoremap <silent> { <c-g>"_c{}<esc>i
silent! snoremap <silent> < <c-g>"_c<><esc>i
silent! snoremap <silent> ' <c-g>"_c''<esc>i
silent! snoremap <silent> " <c-g>"_c""<esc>i
silent! snoremap <silent> ` <c-g>"_c``<esc>i
silent! snoremap <silent> _ <c-g>"_c_<esc>a
silent! snoremap <silent> : <c-g>"_c:<esc>a
endfunction
if !g:zf_fakevim
augroup ZF_Setting_SelectModeMap_augroup
autocmd!
autocmd User ZFVimrcPostNormal call ZF_Setting_SelectModeMap()
augroup END
endif
" scrolling
nnoremap <c-h> zh
nnoremap <c-l> zl
nnoremap <c-j> <c-e>
nnoremap <c-k> <c-y>
imap <c-h> <left>
imap <c-l> <right>
imap <c-j> <down>
imap <c-k> <up>
cnoremap <c-h> <left>
cnoremap <c-l> <right>
if !g:zf_fakevim
if exists('*wildmenumode')
cnoremap <expr> <c-j> wildmenumode() ? "\<c-n>" : "\<down>"
cnoremap <expr> <c-k> wildmenumode() ? "\<c-p>" : "\<up>"
else
cnoremap <c-j> <down>
cnoremap <c-k> <up>
endif
else
cnoremap <c-j> <down>
cnoremap <c-k> <up>
endif
nnoremap H :bp<cr>
nnoremap L :bn<cr>
if !g:zf_fakevim
nnoremap J <c-f>
xnoremap J <c-f>
onoremap J <c-f>
nnoremap K <c-b>
xnoremap K <c-b>
onoremap K <c-b>
else
noremap J <c-f>
noremap K <c-b>
endif
" move
if !g:zf_fakevim
nmap z, %
xmap z, %
nnoremap , $
xnoremap , $
onoremap , $
nnoremap g, g$
xnoremap g, g$
onoremap g, g$
nnoremap j gj
xnoremap j gj
onoremap j gj
nnoremap gj j
xnoremap gj j
onoremap gj j
nnoremap k gk
xnoremap k gk
onoremap k gk
nnoremap gk k
xnoremap gk k
onoremap gk k
else
map z, %
noremap , $
noremap g, $
noremap j gj
noremap k gk
endif
" brace jump
nnoremap zg <nop>
nnoremap zg) va)<esc>h%
nnoremap z) va)<esc>h
xnoremap zg) <esc>`<mz`>va)<esc>h%m>`zm<:delmarks z<cr>gv
xnoremap z) <esc>`<mz`>va)<esc>`zm<:delmarks z<cr>gvh
nnoremap zg] va]<esc>h%
nnoremap z] va]<esc>h
xnoremap zg] <esc>`<mz`>va]<esc>h%m>`zm<:delmarks z<cr>gv
xnoremap z] <esc>`<mz`>va]<esc>`zm<:delmarks z<cr>gvh
nnoremap zg} va}<esc>h%
nnoremap z} va}<esc>h
xnoremap zg} <esc>`<mz`>va}<esc>h%m>`zm<:delmarks z<cr>gv
xnoremap z} <esc>`<mz`>va}<esc>`zm<:delmarks z<cr>gvh
nnoremap zg> va><esc>h%
nnoremap z> va><esc>h
xnoremap zg> <esc>`<mz`>va><esc>h%m>`zm<:delmarks z<cr>gv
xnoremap z> <esc>`<mz`>va><esc>`zm<:delmarks z<cr>gvh
nnoremap zg" vi"<esc>`<h
nnoremap z" vi"<esc>
xnoremap zg" <esc>`<mz`>vi"<esc>`<m>`zm<:delmarks z<cr>gv
xnoremap z" <esc>`<mz`>vi"<esc>`zm<:delmarks z<cr>gv
nnoremap zg' vi'<esc>`<h
nnoremap z' vi'<esc>
xnoremap zg' <esc>`<mz`>vi'<esc>`<m>`zm<:delmarks z<cr>gv
xnoremap z' <esc>`<mz`>vi'<esc>`zm<:delmarks z<cr>gv
nmap zg; zg}
nmap z; z}
xmap zg; zg}
xmap z; z}
" go to define
if !g:zf_fakevim
if empty(mapcheck('zj', 'n'))
nnoremap zj <c-]>
endif
if empty(mapcheck('zk', 'n'))
nnoremap zk <c-t>
endif
else
nnoremap zj <c-]>
nnoremap zk <c-t>
endif
nnoremap zh :tprevious<cr>
nnoremap zl :tnext<cr>
" redo
nnoremap U <c-r>
" modify/delete without change clipboard
if !g:zf_fakevim
nnoremap c "_c
xnoremap c "_c
nnoremap d "_d
xnoremap d "_d
else
noremap c c
noremap d d
endif
nnoremap <del> "_dl
vnoremap <del> "_d
inoremap <del> <right><bs>
" always use system clipboard
if has('clipboard')
nnoremap zp :let @" = @*<cr>:echo 'copied from system clipboard'<cr>
else
nnoremap zp <nop>
endif
" https://github.com/neovim/neovim/issues/1822
set clipboard+=unnamed
set clipboard+=unnamedplus
nnoremap p gP
xnoremap p "_dgP
nnoremap P gp
xnoremap P "_dgp
nmap <c-g> p
xmap <c-g> p
if has('clipboard')
inoremap <c-g> <c-r>*
" paste as user typed
" to ensure the command would exist in command history
function! ZF_Setting_command_paste()
call feedkeys("\<c-r>*", 'nt')
return ''
endfunction
cnoremap <expr> <c-g> '' . ZF_Setting_command_paste()
snoremap <c-g> <c-o>"_c<c-r>*
else
if !g:zf_fakevim
inoremap <c-g> <c-r>"
cnoremap <c-g> <c-r>"
snoremap <c-g> <c-o>"_dgP
else
inoremap <c-g> <c-r>0
cnoremap <c-g> <c-r>0
snoremap <c-g> <c-o>"_c<c-r>0
endif
endif
if has('clipboard')
cnoremap <s-insert> <c-r>*
else
cnoremap <s-insert> <c-r>"
endif
if !g:zf_fakevim
nnoremap DU "tyy"tp
else
nnoremap DU yyp
endif
" window and buffer management
nnoremap B :bufdo<space>
nnoremap zs :w<cr>
nnoremap ZS :wa<cr>
nnoremap zx :w<cr>:bd<cr>
nnoremap ZX :wa<cr>:bufdo bd<cr>
nnoremap CX :bufdo bd!<cr>
if !g:zf_fakevim
function! ZF_Setting_cx()
let bufnr = bufnr('%')
let bufhidden = &bufhidden
bp
if 1
\ && bufhidden != 'unload'
\ && bufhidden != 'delete'
\ && bufhidden != 'wipe'
execute 'bd! ' . bufnr
endif
endfunction
nnoremap <silent> cx :call ZF_Setting_cx()<cr>
nnoremap x :bd<cr>
else
nnoremap cx :bd!<cr>
nnoremap x :q<cr>
endif
command! W w !sudo tee % > /dev/null
nnoremap WH <c-w>h
nnoremap WL <c-w>l
nnoremap WJ <c-w>j
nnoremap WK <c-w>k
nnoremap WO :resize<cr>:vertical resize<cr>
nnoremap WI :vertical resize<cr>
nnoremap WU :resize<cr>
nnoremap WW <c-w>w
nnoremap WN <c-w>=
nnoremap Wh 30<c-w><
nnoremap Wl 30<c-w>>
nnoremap Wj 10<c-w>+
nnoremap Wk 10<c-w>-
" fold
xnoremap ZH zf
nnoremap ZH zc
nnoremap ZL zo
nnoremap Zh zC
nnoremap Zl zO
nnoremap ZU zE
nnoremap ZI zM
nnoremap ZO zR
" diff util
nnoremap D <nop>
nnoremap DJ ]czz
nnoremap DK [czz
nnoremap DH do
xnoremap DH :diffget<cr>
nnoremap DL dp
xnoremap DL :diffput<cr>
nnoremap DD :diffupdate<cr>
" quick move lines
nnoremap C <nop>
if !g:zf_fakevim
nnoremap CH v"txhh"tp
nnoremap CL v"tx"tp
nnoremap CJ mT:m+<cr>`T:delmarks T<cr>:echo ''<cr>
nnoremap CK mT:m-2<cr>`T:delmarks T<cr>:echo ''<cr>
xnoremap CH "txhh"tp`<hm<`>hm>gv
xnoremap CL "tx"tp`<lm<`>lm>gv
xnoremap CJ :m'>+<cr>gv
xnoremap CK :m'<-2<cr>gv
else
nnoremap CH vxhhp
nnoremap CL vxp
nnoremap CJ :m+<cr>
nnoremap CK :m-2<cr>
vnoremap CH xhhp`<hm<`>hm>gv
vnoremap CL xp`<lm<`>lm>gv
vnoremap CJ :m'>+<cr>gv
vnoremap CK :m'<-2<cr>gv
endif
nnoremap < <<
nnoremap > >>
" inc/dec numbers
nnoremap CI <c-a>
nnoremap CU <c-x>
set nrformats+=alpha
set nrformats-=octal
" macro spec
function! ZF_Setting_VimMacroMap()
nnoremap Q :call ZF_Setting_VimMacroBegin(0)<cr>
nnoremap zQ :call ZF_Setting_VimMacroBegin(1)<cr>
nnoremap cQ :let @t = 'let @m="' . @m . '"'<cr>q:"tgP
nmap M @m
endfunction
function! ZF_Setting_VimMacroBegin(isAppend)
nnoremap Q q:call ZF_Setting_VimMacroEnd()<cr>
nnoremap M q:call ZF_Setting_VimMacroEnd()<cr>@m
if !a:isAppend
normal! qm
else
normal! qM
endif
endfunction
function! ZF_Setting_VimMacroEnd()
call ZF_Setting_VimMacroMap()
echo 'macro recorded, use M in normal mode to repeat'
endfunction
if !g:zf_fakevim
" mapping '::' would confuse some vim simulate plugins which would cause strange behavior,
" even worse, some of them won't check 'if' statement and always apply settings inside the 'if',
" here's some tricks (using autocmd) to completely prevent the mapping from being executed
augroup ZF_Setting_VimMacro_augroup
autocmd!
autocmd User ZFVimrcPostNormal
\ call ZF_Setting_VimMacroMap()
\| nnoremap :: q:k$
\| nnoremap // q/k$
augroup END
endif
" quick edit command
function! ZF_Setting_cmdEdit()
let cmdtype = getcmdtype()
if cmdtype != ':' && cmdtype != '/'
return ''
endif
call feedkeys("\<c-c>q" . cmdtype . 'k0' . (getcmdpos() - 1) . 'l', 'nt')
return ''
endfunction
cnoremap <silent><expr> ;; ZF_Setting_cmdEdit()
" search and replace
" (here's a memo for regexp)
"
" zero width:
" (?=exp) : anything end with exp (excluding exp)
" (?!exp) : anything not end with exp
" (?<=exp) : anything start with exp (excluding exp)
" (?<!exp) : anything not start with exp
"
" match as less as possible:
" .* : .*?
" .+ : .+?
" .{n,} : .{n,}?
"
" match line except contains zzz:
" ^(?!.*zzz).*$
if !g:zf_fakevim
nnoremap / /\v
nnoremap ? /\v
nnoremap <leader>vr :.,$s/\v/gec<left><left><left><left>
xnoremap <leader>vr "ty:.,$s/\v<c-r>t//gec<left><left><left><left>
nnoremap <leader>zr :.,$s/\v/gec<left><left><left><left><<c-r><c-w>>/
xnoremap <leader>zr "ty:.,$s/\v<<c-r>t>//gec<left><left><left><left>
nnoremap <leader>v/ :%s/\v//gn<left><left><left><left>
xnoremap <leader>v/ "ty:%s/\v<c-r>t//gn<left><left><left><left>
nnoremap <leader>z/ :%s/\v<<c-r>t>//gn<left><left><left><left>
xnoremap <leader>z/ "ty:%s/\v<<c-r>t>//gn<left><left><left><left>
else
nnoremap / /
nnoremap ? /
nnoremap <leader>vr :.,$s//gec<left><left><left><left>
vnoremap <leader>vr y:.,$s/<c-r>0//gec<left><left><left><left>
nnoremap <leader>zr :.,$s//gec<left><left><left><left>\<<c-r><c-w>\>/
vnoremap <leader>zr y:.,$s/\<<c-r>0\>//gec<left><left><left><left>
nnoremap <leader>v/ :%s///gn<left><left><left><left>
vnoremap <leader>v/ y:%s/<c-r>0//gn<left><left><left><left>
nnoremap <leader>z/ :%s/\<<c-r>0\>//gn<left><left><left><left>
vnoremap <leader>z/ y:%s/\<<c-r>0\>//gn<left><left><left><left>
endif
" command line utils
cnoremap <expr> %% getcmdtype() == ':' ? substitute(expand('%:p'), '\\', '/', 'g') : '%%'
" suspend is not useful and would confuse user
nnoremap <c-z> <nop>
" join
if !g:zf_fakevim
function! ZF_Setting_join(bang) range
let lastline = (a:firstline == a:lastline) ? a:lastline + 1 : a:lastline
if a:bang != '!'
execute printf('%d,%djoin', a:firstline, lastline)
else
let pos = getpos('.')
" (^.*?)[ \t]*\n[ \t]*(.*?)$
call setpos('.', [pos[0], a:firstline, pos[2], pos[3]])
for i in range(a:firstline, lastline - 1)
execute printf('%d,%ds/\(^.\{-}\)[ \t]*\n[ \t]*\(.\{-}\)$/\1\2/ge', a:firstline, a:firstline)
endfor
call setpos('.', pos)
endif
endfunction
nnoremap <silent> Y :call ZF_Setting_join('')<cr>
xnoremap <silent> Y :call ZF_Setting_join('')<cr>
nnoremap <silent> gY :call ZF_Setting_join('!')<cr>
xnoremap <silent> gY :call ZF_Setting_join('!')<cr>
else
noremap gY J
noremap Y J
endif
endif " custom key mapping
" ==================================================
if 1 " common settings
" common
if !empty(g:zf_vim_viminfo_path)
let zf_vim_viminfo_path = substitute(substitute(g:zf_vim_viminfo_path, '\\', '/', 'g'), ' ', '\\ ', 'g')
if has('viminfo')
if exists('&viminfofile')
execute 'set viminfofile=' . zf_vim_viminfo_path
else
execute 'set viminfo+=n' . zf_vim_viminfo_path
endif
elseif exists('&shada')
execute 'set shada+=n' . zf_vim_viminfo_path
endif
endif
set hidden
set list
set listchars=tab:>\ ,trail:-,extends:>,precedes:<,nbsp:+
set modeline
silent! set shortmess+=F
set showcmd
set showmatch
set wildmenu
try
silent! set wildoptions+=pum
catch
endtry
set autoread
set nobackup
set nowritebackup
set noswapfile
set nowrap
set synmaxcol=200
set lazyredraw
set guioptions=gtk
set whichwrap=b,s,<,>,[,]
set display=lastline
set sessionoptions-=options
" https://github.com/vim/vim/issues/13721
set ttimeoutlen=50
if has("patch-8.1.1564")
set signcolumn=number
endif
function! ZF_Setting_common_action()
set number
set iskeyword=@,48-57,_,128-167,224-235
if !g:zf_fakevim
set textwidth=0
endif
endfunction
call ZF_Setting_common_action()
augroup ZF_Setting_common_augroup
autocmd!
autocmd FileType,BufNewFile,BufReadPost * call ZF_Setting_common_action()
augroup END
function! ZF_Setting_isLargeFile(file)
if get(b:, 'zf_vim_largefile_force', 0)
return 1
endif
try
let size = getfsize(a:file)
let largeFile = get(g:, 'ZF_Setting_largefile', 2 * 1024 * 1024)
if size == -2 || (largeFile > 0 && size > largeFile)
return 1
endif
let largeColumn = get(g:, 'ZF_Setting_largefile_column', 2000)
if largeColumn > 0 && filereadable(a:file)
for line in readfile(a:file, '', get(g:, 'ZF_Setting_largefile_columnChecklines', 20))
if len(line) >= largeColumn
return 1
endif
endfor
endif
catch
endtry
return 0
endfunction
augroup ZF_Setting_largefile_augroup
autocmd!
function! s:ZF_Setting_largefile_restore(...)
set eventignore-=FileType
endfunction
function! s:ZF_Setting_largefile_setup(notifyRestore)