-
Notifications
You must be signed in to change notification settings - Fork 4
/
.vimrc
2043 lines (1772 loc) · 59.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
"---------------------------------------------------------------------------
" __ _ _ _
" ____/ /___ _____ (_)_____( ) _ __ (_)____ ___ _____ _____
" / __ // _ \ / ___// // ___/|/ | | / // // __ `__ \ / ___// ___/
" / /_/ // __// / / /(__ ) | |/ // // / / / / // / / /__
" \__,_/ \___//_/ /_//____/ |___//_//_/ /_/ /_//_/ \___/
"
"---------------------------------------------------------------------------
"---------------------------------------------------------------------------
" basic settings {{{1
if has('vim_starting') && has('reltime')
let g:startuptime = reltime()
augroup vimrc-startuptime
autocmd! VimEnter * let g:startuptime = reltime(g:startuptime) | redraw
\ | echomsg 'startuptime: ' . reltimestr(g:startuptime)
augroup END
endif
if has('win32')
let $DOTVIM=expand('~/vimfiles')
else
let $DOTVIM=expand('~/.vim')
endif
let $VIMBUNDLE=$DOTVIM.expand('/bundle')
function! s:bundled(bundle)
let plugs = get(g:, 'plugs', {})
return has_key(plugs, a:bundle) ? isdirectory(plugs[a:bundle].dir) : 0
endfunction
"---------------------------------------------------------------------------
" Plug {{{2
call plug#begin($VIMBUNDLE)
" original repos on github
Plug 'AndrewRadev/linediff.vim'
Plug 'Keithbsmiley/swift.vim'
Plug 'DeaR/vim-tabpagebuffer-misc'
Plug 'Shougo/neocomplete'
Plug 'Shougo/neosnippet'
Plug 'Shougo/neosnippet-snippets'
Plug 'Shougo/tabpagebuffer.vim'
Plug 'Shougo/vimproc.vim', { 'do' : 'make' }
Plug 'Yggdroot/indentLine'
Plug 'airblade/vim-rooter'
Plug 'airblade/vim-gitgutter'
Plug 'bling/vim-airline'
Plug 'chrisbra/Recover.vim'
Plug 'chrisbra/vim-diff-enhanced'
Plug 'ctrlpvim/ctrlp.vim'
Plug 'easymotion/vim-easymotion'
Plug 'fatih/vim-go'
Plug 'glidenote/memolist.vim'
Plug 'haya14busa/vim-asterisk'
Plug 'haya14busa/incsearch.vim'
Plug 'haya14busa/vim-metarepeat'
Plug 'ivalkeen/vim-ctrlp-tjump'
Plug 'Julian/vim-textobj-variable-segment'
Plug 'junegunn/fzf', { 'do' : './install --all' }
Plug 'junegunn/fzf.vim'
Plug 'junegunn/vim-after-object'
Plug 'junegunn/vim-easy-align'
Plug 'justinmk/vim-dirvish'
Plug 'justmao945/vim-clang'
Plug 'kana/vim-altr'
Plug 'kana/vim-niceblock'
Plug 'kana/vim-operator-user'
Plug 'kana/vim-operator-replace'
Plug 'kana/vim-smarttill'
Plug 'kana/vim-smartword'
Plug 'kana/vim-submode'
Plug 'kana/vim-textobj-user'
Plug 'kana/vim-textobj-entire'
Plug 'kana/vim-textobj-function'
Plug 'kana/vim-textobj-indent'
Plug 'kana/vim-textobj-line'
Plug 'koron/vim-gosrc'
Plug 'koturn/vim-replica'
Plug 'lambdalisue/pinkyless.vim'
Plug 'majutsushi/tagbar'
Plug 'mattn/ctrlp-everything'
Plug 'mattn/ctrlp-filer'
Plug 'mattn/ctrlp-register'
Plug 'mattn/gist-vim'
Plug 'mattn/learn-vimscript'
Plug 'mattn/emmet-vim'
Plug 'mhinz/vim-startify'
Plug 'nelstrom/vim-markdown-folding'
Plug 'nixprime/cpsm'
Plug 'ntpeters/vim-better-whitespace'
Plug 'ompugao/ctrlp-locate'
Plug 'rhysd/committia.vim'
Plug 'rhysd/conflict-marker.vim'
Plug 'rhysd/vim-grammarous'
Plug 'rhysd/vim-clang-format'
Plug 'rhysd/vim-operator-surround'
Plug 'rhysd/vim-textobj-word-column'
Plug 'rphillips/vim-zoomwin'
Plug 'scrooloose/syntastic'
Plug 'sgur/ctrlp-extensions.vim'
Plug 'sgur/vim-textobj-parameter'
Plug 'sjl/gundo.vim'
Plug 'supermomonga/thingspast.vim'
Plug 't9md/vim-surround_custom_mapping'
Plug 'tacahiroy/ctrlp-funky'
Plug 'thinca/vim-ambicmd'
Plug 'thinca/vim-fontzoom'
Plug 'thinca/vim-poslist'
Plug 'thinca/vim-qfreplace'
Plug 'thinca/vim-quickrun'
Plug 'thinca/vim-scouter'
Plug 'thinca/vim-singleton'
Plug 'thinca/vim-textobj-comment'
Plug 'thinca/vim-themis'
Plug 'thinca/vim-zenspace'
Plug 'tpope/vim-commentary'
Plug 'tpope/vim-endwise'
Plug 'tpope/vim-fugitive'
Plug 'tpope/vim-rails'
Plug 'tpope/vim-repeat'
Plug 'tpope/vim-surround'
Plug 'tyru/capture.vim'
Plug 'tyru/eskk.vim'
Plug 'tyru/nextfile.vim'
Plug 'tyru/open-browser.vim'
Plug 'vim-jp/vital.vim'
Plug 'vim-ruby/vim-ruby', { 'for' : 'ruby' }
Plug 'vimtaku/hl_matchit.vim'
Plug 'w0rp/ale'
Plug 'wellle/targets.vim'
Plug 'deris/columnjump'
Plug 'deris/vim-fitcolumn'
Plug 'deris/parajump'
Plug 'deris/vim-cmdline-switch'
Plug 'deris/vim-diffbuf'
Plug 'deris/vim-dirdiff', 'dev'
Plug 'deris/vim-pasta'
Plug 'deris/vim-operator-insert'
Plug 'deris/vim-rengbang'
Plug 'deris/vim-shot-f'
Plug 'deris/vim-textobj-enclosedsyntax'
Plug 'deris/vim-multi-replace'
" vim-scripts repos
Plug 'vim-scripts/HybridText'
Plug 'vim-scripts/UnconditionalPaste'
Plug 'vim-scripts/matchit.zip'
" color scheme
Plug 'deris/molokai'
Plug 'deris/vim-wombat'
Plug 'morhetz/gruvbox'
Plug 'vim-scripts/newspaper.vim'
Plug 'w0ng/vim-hybrid'
" Japanese help
Plug 'vim-jp/vimdoc-ja'
call plug#end()
" }}}
if s:bundled('vim-singleton')
if has('clientserver')
call singleton#enable()
endif
endif
"---------------------------------------------------------------------------
" Setting options:{{{2
" enable syntax highlighting
syntax enable
set ignorecase
set smartcase
set incsearch
set hlsearch
set tabstop=4
set expandtab
set autoindent
set backspace=indent,eol,start
set nowrapscan
set showmatch
set matchpairs+=<:>
set wildmenu
set wildmode=longest,full
set wildignore=.git,.hg,.svn
set wildignore+=*.jpg,*.jpeg,*.bmp,*.gif,*.png
set wildignore+=*.o,*.obj,*.exe,*.dll,*.manifest,*.so,*.out,*.class
set wildignore+=*.swp,*.swo,*.swn
set wildignore+=*.DS_Store
let format_allow_over_tw = 1
set shiftwidth=2
set smartindent
set smarttab
set whichwrap=b,s,h,l,<,>,[,]
set hidden
set textwidth=0
set formatoptions-=t
set formatoptions-=c
set formatoptions-=r
set formatoptions-=o
set formatoptions-=v
set formatoptions+=l
set formatoptions+=j
set nonumber
set ruler
set list
set listchars=tab:>-,trail:-,nbsp:%,extends:>,precedes:<,eol:<
set t_Co=256
set nrformats=hex
set winaltkeys=no
set visualbell
set noequalalways
set nowrap
set laststatus=2
set cmdheight=2
set showcmd
set title
set lines=50
set showtabline=2
set previewheight=10
set helpheight=12
set virtualedit=block
set scrolloff=5
set backup
function! s:LetAndMkdir(variable, path) "{{{
try
if !isdirectory(a:path)
call mkdir(a:path, 'p')
endif
catch
echohl WarningMsg
echom '[error]' . a:path . 'is exist and is not directory, but is file or something.'
echohl None
endtry
execute printf("let %s = a:path", a:variable)
endfunction "}}}
call s:LetAndMkdir('&backupdir', $DOTVIM.expand('/backup'))
set swapfile
call s:LetAndMkdir('&directory', $DOTVIM.expand('/swap'))
set history=2000
let &showbreak = '+++ '
set ttyfast
if has('persistent_undo')
set undofile
call s:LetAndMkdir('&undodir', $DOTVIM.expand('/undo'))
endif
set tags=./tags;
let &termencoding = &encoding
set encoding=utf-8
set fileencoding=utf-8
set fileencodings=ucs-bom,utf-8,iso-2022-jp-3,iso-2022-jp,eucjp-ms,euc-jisx0213,euc-jp,sjis,cp932
set fileformats=unix,dos,mac
if exists('&ambiwidth')
set ambiwidth=double
endif
if s:bundled('vim-wombat')
colorscheme wombat
else
colorscheme desert
endif
scriptencoding utf-8
let g:my_rg_path = ''
let g:my_ag_path = ''
let g:my_jvgrep_path = ''
let g:my_fd_path = ''
if has('win32')
let g:my_rg_path = 'C:\usr\local\bin\rg.exe'
let g:my_ag_path = 'C:\usr\local\bin\ag.exe'
let g:my_jvgrep_path = 'C:\usr\local\bin\jvgrep.exe'
let g:my_fd_path = 'C:\usr\local\bin\fd.exe'
elseif has('unix')
let g:my_rg_path = 'rg'
let g:my_ag_path = 'ag'
let g:my_jvgrep_path = 'jvgrep'
let g:my_fd_path = 'fd'
endif
if executable(g:my_ag_path)
let &grepprg = g:my_ag_path . ' --vimgrep -S --hidden'
set grepformat=%f:%l:%c:%m
elseif executable(g:my_rg_path)
let &grepprg = g:my_rg_path . ' --vimgrep -S --no-heading --hidden'
set grepformat=%f:%l:%c:%m
elseif executable(g:my_jvgrep_path)
let &grepprg = g:my_jvgrep_path . ' --no-color -nr8 --enc utf-8,cp932,euc-jp'
endif
" vimdiff highlight
augroup diffcolor
autocmd!
autocmd VimEnter,ColorScheme * hi DiffAdd cterm=bold ctermbg=DarkGreen gui=bold guibg=DarkGreen
autocmd VimEnter,ColorScheme * hi DiffChange ctermbg=DarkMagenta guibg=DarkMagenta
autocmd VimEnter,ColorScheme * hi DiffDelete ctermbg=DarkRed guibg=DarkRed
autocmd VimEnter,ColorScheme * hi DiffText cterm=bold ctermbg=Blue gui=bold guibg=Blue
augroup END
" highlight cursorline and cursorcolumn only current window
augroup cursorsetting
autocmd!
autocmd BufWinEnter,WinEnter * setlocal cursorline cursorcolumn
autocmd BufWinLeave,WinLeave * setlocal nocursorline nocursorcolumn
augroup END
" move current file directory
augroup movecurrentdir
autocmd!
autocmd BufRead,BufEnter * if isdirectory(expand('%:p:h')) | lcd %:p:h | endif
augroup END
" make directory if parent directory does'nt exist
augroup AutoMkdir
autocmd!
autocmd BufNewFile * call PromptAndMakeDirectory()
augroup END
function! PromptAndMakeDirectory()
let dir = expand("<afile>:p:h")
if !isdirectory(dir) && confirm("Create a new directory [".dir."]?", "&Yes\n&No") == 1
call mkdir(dir, "p")
" Reset fullpath of the buffer in order to avoid problems when using autochdir.
file %
endif
endfunction
" }}}
"---------------------------------------------------------------------------
" custom keymaps and commands {{{2
" my mappable key list
" nmap <Space>a
" nmap <Space>i
" nmap <Space>j
" nmap <Space>k
" nmap <Space>m
" nmap <Space>r
" nmap <Space>z
" nmap ,a
" nmap ,e
" nmap ,f
" nmap ,h
" nmap ,i
" nmap ,k
" nmap ,n
" nmap ,o
" nmap ,p
" nmap ,u
" nmap ,v
" nmap ,x
" nmap ,y
" nmap ,z
let mapleader = ","
let maplocalleader = ","
nnoremap \ ,
if has('mac')
noremap ¥ \
noremap \ ¥
endif
" Esc
inoremap jk <Esc>
if has('mac')
" prevent miss type
inoremap <D-j>k <Esc>
inoremap <D-j><D-k> <Esc>
endif
" switch j,k and gj,gk
nnoremap j gj
nnoremap k gk
vnoremap j gj
vnoremap k gk
nnoremap gj j
nnoremap gk k
vnoremap gj j
vnoremap gk k
" key map ^,$ to <Space>h,l. Because ^ and $ is difficult to type and damage little finger!!!
noremap <Space>h ^
noremap <Space>l $
" same as above. but don't use noremap because want to map to matchit plugin
map <Space>n %
" edit vimrc
if has('gui')
nnoremap <silent> <Space>. :<C-u>execute 'tab drop ' . escape(resolve($MYVIMRC), ' ')<CR>
else
nnoremap <silent> <Space>. :<C-u>execute 'tabe ' . escape(resolve($MYVIMRC), ' ')<CR>
endif
" last selected text operator
onoremap gv :<C-u>normal! gv<CR>
" select last changed text
nnoremap gc `[v`]
vnoremap gc :<C-u>normal gc<CR>
onoremap gc :<C-u>normal gc<CR>
" move last changed text
nnoremap gI `.zz
" paste last yanked text
nnoremap 'p "0p
" Use more logical mapping (see :h Y)
nnoremap Y y$
" paste from clipboard
nnoremap <Space>p "*p
vnoremap <Space>p "*p
nnoremap <Space>P "*P
vnoremap <Space>P "*P
" yank to clipboard
nnoremap <Space>y "*y
nnoremap <Space>Y "*y$
vnoremap <Space>y "*y
" delete to clipboard
nnoremap <Space>d "*d
nnoremap <Space>D "*d$
vnoremap <Space>d "*d
" insert blank line
nnoremap <silent> <Space>o :<C-u>for i in range(1, v:count1) \| call append(line('.'), '') \| endfor \| silent! call repeat#set("<Space>o", v:count1)<CR>
nnoremap <silent> <Space>O :<C-u>for i in range(1, v:count1) \| call append(line('.')-1, '') \| endfor \| silent! call repeat#set("<Space>O", v:count1)<CR>
nnoremap <silent> <S-Space>O :<C-u>for i in range(1, v:count1) \| call append(line('.')-1, '') \| endfor \| silent! call repeat#set("<S-Space>O"), v:count1<CR>
" swap ; and :
nnoremap ; :
vnoremap ; :
nnoremap q; q:
vnoremap q; q:
nnoremap : ;
vnoremap : ;
" repeat last command-line
nnoremap <Space>; @:
" repeats the last command on every line
vnoremap . :normal .<CR>
function! s:dot_repeat(bang)
for i in range(v:count1)
execute "normal".a:bang." ."
endfor
endfunction
command! -nargs=0 -bang DotRepeat call s:dot_repeat("<bang>")
" repeats the last command v:count1 time
nnoremap <Leader>. :<C-u>DotRepeat<CR>
let reg_list = []
call extend(reg_list, range(char2nr('0'), char2nr('9')))
call extend(reg_list, range(char2nr('a'), char2nr('z')))
call extend(reg_list, ['"', '.', '=', '*', '+', ':', '@'])
for i in reg_list
execute 'vnoremap @'.i . ' :normal @'.i.'<CR>'
endfor
" disable because this is dangerous key
nnoremap ZZ <Nop>
nnoremap ZQ <Nop>
" Don't use Ex mode, use Q for formatting
nnoremap Q gq
" use <C-q> instead of @
nnoremap <C-q> @
nnoremap <C-q><C-q> @@
" text-object
onoremap aa a>
vnoremap aa a>
onoremap ia i>
vnoremap ia i>
onoremap ar a]
vnoremap ar a]
onoremap ir i]
vnoremap ir i]
onoremap ad a"
vnoremap ad a"
onoremap id i"
vnoremap id i"
" ex command
nnoremap <Space>w :<C-u>update<CR>
nnoremap <Space>q :<C-u>q<CR>
nnoremap <Space>Q :<C-u>q!<CR>
nnoremap <S-Space>Q :<C-u>q!<CR>
nnoremap <Space>bd :<C-u>bwipeout<CR>
nnoremap <Space>bD :<C-u>bwipeout!<CR>
nnoremap <Space>bb :<C-u>buffer #<CR>
" virtual replace mode
nnoremap R gR
" Search the word nearest to the cursor in new window.
nnoremap <C-w>* <C-w>s*
nnoremap <C-w># <C-w>s#
" command mode
cnoremap <C-b> <Left>
cnoremap <C-f> <Right>
cnoremap <M-b> <S-Left>
cnoremap <M-f> <S-Right>
cnoremap <C-a> <Home>
cnoremap <expr> <C-d> (getcmdpos()==strlen(getcmdline())+1 ? "\<C-d>" : "\<Del>")
cnoremap <C-e> <END>
cnoremap <C-h> <BS>
cnoremap <C-u> <C-e><C-u>
" open the command-line window from command-line mode
cnoremap <C-v> <C-f>a
" insert the line under the cursor
cnoremap <expr> <C-r><C-l> matchstr(getline("."), '[^ \t:][^\r\n]*')
" easy to type in ASCII keyboard
cnoremap <C-r>' <C-r>"
" auto escape in command-line mode
cnoremap <expr> / getcmdtype() == '/' ? '\/' : '/'
cnoremap <expr> ? getcmdtype() == '?' ? '\?' : '?'
" insert substitute command
nnoremap gs :<C-u>%s///g<Left><Left><Left>
vnoremap gs :s///g<Left><Left><Left>
nnoremap gS :<C-u>%s///gc<Left><Left><Left>
vnoremap gS :s///gc<Left><Left><Left>
" insert grep/vimgrep command
nnoremap gr :<C-u>grep
nnoremap <Space>gr :<C-u>vimgrep // **<Left><Left><Left><Left>
" for vimdiff
nnoremap dp dp:<C-u>diffupdate<CR>]czz
nnoremap do do:<C-u>diffupdate<CR>]czz
vnoremap <Leader>dp :diffput<CR>:<C-u>diffupdate<CR>zz
vnoremap <Leader>do :diffget<CR>:<C-u>diffupdate<CR>zz
nnoremap du :<C-u>diffupdate<CR>
augroup diffsetting
autocmd!
autocmd InsertLeave * if &diff | diffupdate | echo 'diffupdated' | endif
augroup END
" insert mode
inoremap <C-b> <Left>
inoremap <C-f> <Right>
inoremap <M-b> <S-Left>
inoremap <M-f> <S-Right>
inoremap <C-a> <C-o>^
inoremap <C-e> <C-o>$
inoremap <C-h> <BS>
inoremap <C-d> <Del>
"inoremap <C-k> <C-o>C
"inoremap <C-y> <C-o>p
" undoable C-w,C-u
inoremap <C-w> <C-g>u<C-w>
inoremap <C-u> <C-g>u<C-u>
inoremap <S-Enter> <C-o>O
" stop the highlighting
nnoremap <silent> <Esc><Esc> :<C-u>nohlsearch<CR>
nnoremap <silent> <C-[><C-[> :<C-u>nohlsearch<CR>
" n is always search forwward and N is always search backward
nnoremap <expr> n <SID>search_forward_p() ? 'nzv' : 'Nzv'
nnoremap <expr> N <SID>search_forward_p() ? 'Nzv' : 'nzv'
vnoremap <expr> n <SID>search_forward_p() ? 'nzv' : 'Nzv'
vnoremap <expr> N <SID>search_forward_p() ? 'Nzv' : 'nzv'
onoremap <expr> n <SID>search_forward_p() ? 'n' : 'N'
onoremap <expr> N <SID>search_forward_p() ? 'N' : 'n'
function! s:search_forward_p()
return exists('v:searchforward') ? v:searchforward : 1
endfunction
" type H to move top of screen and type L to move bottom of screen if scrolloff set
if &scrolloff > 0
nnoremap <expr> H 'H' . &scrolloff . 'kzvzz'
nnoremap <expr> L 'L' . &scrolloff . 'jzvzz'
endif
" change virtualedit
nnoremap <Space>va :<C-u>setlocal virtualedit=all<CR>:setlocal virtualedit?<CR>
nnoremap <Space>vb :<C-u>setlocal virtualedit=block<CR>:setlocal virtualedit?<CR>
nnoremap <Space>vv :<C-u>let &virtualedit=(&ve == "block" ? "all" : "block")<CR>:setlocal virtualedit?<CR>
" swap gf and gF
noremap gf gF
noremap gF gf
" tab mode
nnoremap [tabmode] <Nop>
nmap t [tabmode]
nnoremap <Space>, :<C-u>MyHome<CR>
command! -nargs=0 MyHome call s:home()
function! s:home()
let memo = '~/memo.md'
if has('gui')
execute 'tab drop ' . memo
else
execute 'tabe ' . memo
endif
endfunction
nnoremap [tabmode]t :<C-u>MyMemoNew<CR>
nnoremap [tabmode]n :<C-u>tab %<CR>
nnoremap [tabmode]d :<C-u>tabclose<CR>
call s:LetAndMkdir('g:my_memo_save_dir', $DOTVIM.expand('/memo'))
command! -nargs=? MyMemoNew call s:create_memo(<f-args>)
command! -nargs=1 MyMemoGrep call s:grep_memo(<q-args>)
function! s:create_memo(...)
if a:0 == 0
let ext = get(g:, 'my_memo_default_ext', '.txt')
else
if a:1 !~ '^\.\?\w\+$'
echohl WarningMsg
echom '[error]' . a:1 . 'is must be word.'
echohl None
endif
let ext = (a:1 =~ '^\.' ? '' : '.') . a:1
endif
execute printf('tabe %s/%s%s', g:my_memo_save_dir, strftime('%Y-%m-%d_%H%M%S'), ext)
endfunction
function! s:grep_memo(pat)
execute printf('vimgrep %s %s/*', a:pat, g:my_memo_save_dir)
endfunction
nnoremap <silent> <C-p> gT
nnoremap <silent> <C-n> gt
" tag jump
nnoremap [tagjump] <Nop>
nmap <Space>t [tagjump]
nnoremap [tagjump]t <C-]>
nnoremap [tagjump]j :<C-u>tag<CR>
nnoremap [tagjump]k :<C-u>pop<CR>
nnoremap [tagjump]l :<C-u>tags<CR>
"--------------------
" Function: Open tag under cursor in new tab
" Source: http://stackoverflow.com/questions/563616/vimctags-tips-and-tricks
"--------------------
nnoremap [tagjump]n :<C-u>tab split<CR>:exec("tag ".expand("<cword>"))<CR>
nnoremap [tagjump]s :<C-u>split<CR>:exec("tag ".expand("<cword>"))<CR>
nnoremap [tagjump]v :<C-u>vsplit<CR>:exec("tag ".expand("<cword>"))<CR>
" filetype
nnoremap [filetype] <Nop>
nmap <Leader>t [filetype]
nnoremap [filetype]p :<C-u>set filetype=perl<CR>
nnoremap [filetype]v :<C-u>set filetype=vim<CR>
nnoremap [filetype]c :<C-u>set filetype=c<CR>
nnoremap [filetype]s :<C-u>set filetype=shell<CR>
nnoremap [filetype]r :<C-u>set filetype=ruby<CR>
nnoremap [filetype]j :<C-u>set filetype=javascript<CR>
nnoremap [filetype]h :<C-u>set filetype=html<CR>
nnoremap [filetype]g :<C-u>set filetype=go<CR>
command! -nargs=1 -complete=filetype FileType execute "set filetype=".<q-args>
" change expandtab booster
nnoremap t2 :<C-U>setlocal expandtab shiftwidth=2 tabstop=2<CR>
nnoremap t4 :<C-U>setlocal noexpandtab shiftwidth=4 tabstop=4<CR>
nnoremap <Space>t2 :<C-U>setlocal expandtab shiftwidth=2 tabstop=2 nolist<CR>
nnoremap <Space>t4 :<C-U>setlocal noexpandtab shiftwidth=4 tabstop=4 nolist<CR>
" Switch off diff mode
function! s:DiffOff()
diffoff!
set nowrap
endfunction
command! DiffOff call s:DiffOff()
function! s:DiffClose()
DiffOff
windo bwipeout!
endfunction
command! DiffClose call s:DiffClose()
command! -nargs=0 EchoSynName echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')
" execute command (inner %s replace last selected text)
function! ExecuteWithSelectedText(command)
if a:command !~? '%s'
return
endif
let reg = '"'
let [save_reg, save_type] = [getreg(reg), getregtype(reg)]
normal! gvy
let selectedText = @"
call setreg(reg, save_reg, save_type)
if selectedText == ''
return
endif
execute printf(a:command, selectedText)
endfunction
function! MyPrintNumber(motion_wise)
let spos = getpos("'[")
let epos = getpos("']")
if (spos[1] != epos[1]) || (spos[2] > epos[2])
return
endif
let line = getline(spos[1])
let target = line[spos[2]-1 : epos[2]-1]
echo printf("BIN: %b\n" .
\ "DEC: %d\n" .
\ "HEX: 0x%X\n", target, target, target)
endfunction
call operator#user#define('my-print-num', 'MyPrintNumber')
nmap <Space>u <Plug>(operator-my-print-num)
command -nargs=? -range=% ExtractMatches <line1>,<line2>call s:extract_matches(<f-args>)
function! s:extract_matches(...) range
let s:pattern = get(a:000, 0, @/)
let s:result_line = filter(getline(a:firstline, a:lastline), 'v:val =~# s:pattern')
new
setlocal buftype=nofile
call setline(1, s:result_line)
endfunction
command -nargs=? -range=% ExtractMatchWords call s:extract_match_words(<line1>, <line2>, <q-args>)
let g:extract_match_word_with_word = 1
let g:extract_match_word_with_sort = 1
let g:extract_match_word_line_num = 10
function! s:extract_match_words(fline, lline, pattern) range
let s:result_word = {}
let save_pos = getcurpos()
if a:fline == a:lline
let [firstline, lastline] = [1, line('$')]
else
let [firstline, lastline] = [a:fline, a:lline]
endif
let pattern = a:pattern
if g:extract_match_word_with_word != 0
let pattern = '\<\w*' . a:pattern . '\m\w*\>'
endif
silent! execute printf('%s,%ss/%s/\=s:save_match(s:result_word)/gn', firstline, lastline, pattern)
call setpos('.', save_pos)
if empty(s:result_word)
echo 'no match found'
else
let result = []
if g:extract_match_word_with_sort != 0
let result = sort(keys(s:result_word))
endif
new
setlocal buftype=nofile
setlocal noswapfile
call setline(1, result)
execute 'resize ' . g:extract_match_word_line_num
endif
endfunction
function! s:save_match(result_word)
let a:result_word[submatch(0)] = get(a:result_word, submatch(0), 0) + 1
return ''
endfunction
" for quickfix windows
augroup MyQuickFix
autocmd!
autocmd FileType qf nnoremap <silent> <buffer> o :<C-u>call <SID>tabopen_quickfix_entry()<CR>
autocmd FileType qf nnoremap <silent> <buffer> dd :call <SID>del_quickfix_entry()<CR>
autocmd FileType qf vnoremap <silent> <buffer> d :call <SID>del_quickfix_entry()<CR>
autocmd FileType qf nnoremap <silent> <buffer> u :<C-u>call <SID>undo_quickfix_entry()<CR>
autocmd QuickFixCmdPost * cwindow
augroup END
function! s:tabopen_quickfix_entry()
let qf = getqflist()
if empty(qf)
return
endif
let index = line('.') - 1
tabe
execute printf('buffer +%d %d', qf[index].lnum, qf[index].bufnr)
endfunction
function! s:undo_quickfix_entry()
let history = get(w:, 'qf_history', [])
if !empty(history)
call setqflist(remove(history, -1), 'r')
endif
endfunction
function! s:del_quickfix_entry() range
let qf = getqflist()
let history = get(w:, 'qf_history', [])
call add(history, copy(qf))
let w:qf_history = history
unlet! qf[a:firstline - 1 : a:lastline - 1]
call setqflist(qf, 'r')
execute a:firstline
endfunction
function! QFSortWithText(func)
if &ft != 'qf'
return
endif
call s:sort_quickfix_entry(a:func)
endfunction
function! s:sort_quickfix_entry(func)
let qf = getqflist()
let history = get(w:, 'qf_history', [])
call add(history, copy(qf))
let w:qf_history = history
let qf = sort(qf, {arg1, arg2 ->a:func(arg1.text, arg2.text)})
call setqflist(qf, 'r')
endfunction
" https://github.com/google/codesearch
let g:codesearch_bin_path = '~/go/bin'
function! s:codesearch_search(pattern)
let csearch = g:codesearch_bin_path . '/csearch'
if !executable(csearch)
echohl WarningMsg
echom printf('[error] %s is not executable', csearch)
echohl None
return
endif
let s:save_grepprg = &grepprg
let s:save_grepformat = &grepformat
let &grepprg = csearch . ' -i -n'
set grepformat=%f:%l:%m
try
execute 'silent grep! ' . a:pattern
finally
let &grepprg = s:save_grepprg
let &grepformat = s:save_grepformat
endtry
endfunction
function! s:codesearch_index(dir)
let cindex = g:codesearch_bin_path . '/cindex'
if !executable(cindex)
echohl WarningMsg
echom printf('[error] %s is not executable', cindex)
echohl None
return
endif
if !isdirectory(a:dir)
throw printf('error: %s is not directory', a:dir)
endif
execute printf('%s %s', cindex, a:dir)
endfunction
command! -nargs=1 CSSearch call s:codesearch_search(<q-args>)
command! -nargs=1 CSIndex call s:codesearch_index(<q-args>)
" popup definition or local declaration
let g:popup_before_additional_line = 0
let g:popup_after_additional_line = 2
let g:popup_no_blank_line = 1
function! s:popup_definition()
call s:move_and_popup(function('s:tag'), function('s:pop'))
endfunction
function! s:tag()
execute "normal! \<C-]>"
endfunction
function! s:pop()
execute "normal! \<C-t>"
endfunction
command! -nargs=0 PopupDefinition silent! call s:popup_definition()
nnoremap <C-g> :<C-u>PopupDefinition<CR>
function! s:popup_local_declaration()
call s:move_and_popup(function('s:go_local_decl'), function('s:go_back'))
endfunction
function! s:go_local_decl()
execute "normal! gd"
endfunction
function! s:go_back()
execute "normal! \<C-o>"
endfunction
command! -nargs=0 PopupLocalDeclaration silent! call s:popup_local_declaration()
nnoremap <C-l> :<C-u>PopupLocalDeclaration<CR>
function! s:move_and_popup(move_func, post_func)
if !exists('*popup_create')
echohl WarningMsg
echom printf('[error] popup_create is not supported')
echohl None
return
endif
let has_jumped = 0
let save_view = {}
try
let bpos = getcurpos()
let save_view = winsaveview()
call a:move_func()
let apos = getcurpos()
if bpos == apos
return
endif
let has_jumped = 1
let strs = s:get_around_lines(g:popup_before_additional_line, g:popup_after_additional_line, g:popup_no_blank_line)
call map(strs, {key, val -> substitute(val, "\t", repeat(' ', &tabstop), 'g')})
call a:post_func()
call winrestview(save_view)
let has_jumped = 0
call popup_create(strs, {
\ 'line': 'cursor',
\ 'col': 'cursor',
\ 'pos': 'topleft',
\ 'wrap': 0,
\ 'border': [1, 1, 1, 1],
\ 'moved': 'any',
\ })
finally
if has_jumped == 1
call a:post_func()
call winrestview(save_view)
else
let apos = getcurpos()
if bpos != apos
call a:post_func()
call winrestview(save_view)
endif
endif
endtry
endfunction
function! s:get_around_lines(before_count, after_count, no_blank_line)
let cur_lnum = line('.')
let before_lnum = max([1, cur_lnum - a:before_count])
let after_lnum = min([line('$'), cur_lnum + a:after_count])
if a:no_blank_line == 1
if a:before_count > 0 && cur_lnum != 1
let lnum = cur_lnum - 1