-
Notifications
You must be signed in to change notification settings - Fork 0
/
.vim-basic.vim
1355 lines (1181 loc) · 40.3 KB
/
.vim-basic.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
" vim: fdm=marker ts=2 sw=2 sts=2 expandtab
"
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Map Helper"{{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}
"{{{
" Map Partial
" C:Ctrl, M:Alt, S:shift,DOWN:down,UP:up,LEFT:left,RIGHT:right
" Notes
" 1. Ctrl-b and Ctrl-B are synonymous, so Ctrl+Shit+.. can't work
" 2. ctrl+f: show command history normal
" 3. bottom blank white line fixed
" # filepath: ~/.gtkrc-2.0
" # desc: let gvim white board invisible.
" style "vimfix" {
" bg[NORMAL] = "#272822" # this matches my gvim theme 'Normal' bg color.
" }
" widget "vim-main-window.*GtkForm" style "vimfix"
" 4.
"}}}
" true color setting in nvim
let $NVIM_TUI_ENABLE_TRUE_COLOR=1
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Helper functions"{{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}
"{{{
function! GetBufLists(info) abort
redir => bufoutput
if a:info == 1
silent buffers
else
silent buffers!
endif
redir END
return bufoutput
endfunction
function! GetBufListedNr() abort
let s:bufoutput = GetBufLists(1)
let s:count = 0
for buf in split(s:bufoutput, '\n')
let s:count += 1
" let s:bits = split(buf, '"')
" if s:bits[0] !~ "u"
" let s:count += 1
" endif
endfor
return s:count
endfunction
function! GetBufNr(nr) abort
let s:bufoutput = GetBufLists(1)
let s:count = 1
for buf in split(s:bufoutput, '\n')
let s:bits = split(buf, ' ')
if s:count == a:nr
return str2nr(s:bits[0])
endif
let s:count +=1
endfor
return 0
endfunction
function! GetBufListsNu() abort
let s:bufoutput = GetBufLists(1)
let s:arrlist = []
for buf in split(s:bufoutput, '\n')
let s:bits = split(buf, ' ')
call add(s:arrlist, str2nr(s:bits[0]))
endfor
return s:arrlist
endfunction
func! DeleteTillSlash()
let g:cmd = getcmdline()
if has("win16") || has("win32")
let g:cmd_edited = substitute(g:cmd, "\\(.*\[\\\\]\\).*", "\\1", "")
else
let g:cmd_edited = substitute(g:cmd, "\\(.*\[/\]\\).*", "\\1", "")
endif
if g:cmd == g:cmd_edited
if has("win16") || has("win32")
let g:cmd_edited = substitute(g:cmd, "\\(.*\[\\\\\]\\).*\[\\\\\]", "\\1", "")
else
let g:cmd_edited = substitute(g:cmd, "\\(.*\[/\]\\).*/", "\\1", "")
endif
endif
return g:cmd_edited
endfunc
func! CurrentFileDir(cmd)
return a:cmd . " " . expand("%:p:h") . "/"
endfunc
function! Vimcmd(cmd) abort
redir => output
silent exe a:cmd
redir END
return output
endfunction
function! GetRangInParagraph()
let s:line = line('.')
let s:col = col('.')
normal `{
let s:firstline = line('.')
normal `}
let s:lastline = line('.')
exe s:line
exec printf('normal! 0%d|', s:col)
return [s:firstline, s:lastline]
endfunction
"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Settings"{{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}
"{{{
" force show window title
set notitle
" completion dont show preview window
set completeopt-=preview
" Set utf8 as standard encoding and en_US as the standard language
set termencoding=utf-8
if !has('nvim')
set encoding=utf8
endif
" Use Unix as the standard file type
set ffs=unix,dos,mac
set fileencodings=utf8,ucs-bom,gbk,cp936,gb2312,gb18030
set previewheight=20
set diffopt+=vertical
" set search pattern empty by default
autocmd VimEnter * exe 'let @/ = ""'
" set split rule
set splitbelow
set splitright
set spellfile=~/.vim/spell/en.utf-8.add
" autocmd FileType gitcommit setlocal spell
" disable Background Color Erase (BCE) so that color schemes
" render properly when inside 256-color tmux and GNU screen.
" see also http://snk.tuxfamily.org/log/vim-256color-bce.html
" if &term =~ '256color'
" set t_ut=
" " au VimEnter * if $is_tmux != '' | call ToggleLabelBar() | endif
" endif
"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Folding Save And View"{{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}
"{{{
set nofoldenable
set foldlevel=99
autocmd FileType git,gitcommit setlocal foldlevel=99
"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => General"{{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}
"{{{
" Sets how many lines of history VIM has to remember
set history=700
" Enable filetype plugins
filetype plugin on
filetype indent on
" Set to auto read when a file is changed from the outside
set autoread
" With a map leader it's possible to do extra key combinations
" like <leader>w saves the current file
let mapleader = ","
let g:mapleader = ","
" Fast saving
nmap <silent> <leader>w :w!<cr>
" :W sudo saves the file
" (useful for handling the permission-denied error)
" command! W silent w !sudo tee % > /dev/null
"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => VIM user interface"{{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}
"{{{
" Set 7 lines to the cursor - when moving vertically using j/k
set so=7
" Turn on the WiLd menu
set wildmenu
" Ignore compiled files
set wildignore=*.o,*~,*.pyc
if has("win16") || has("win32")
set wildignore+=*/.git/*,*/.hg/*,*/.svn/*,*/.DS_Store,.idea,.phpcomplete,cscope.*,tags,.tags
else
set wildignore+=.git\*,.hg\*,.svn\*,fugitive\*,.idea,.phpcomplete,cscope.*,tags,.tags
endif
"Always show current position
set ruler
" Height of the command bar
set cmdheight=1
" A buffer becomes hidden when it is abandoned
set hid
" Configure backspace so it acts as it should act
set backspace=eol,start,indent
set whichwrap+=<,>,h,l
" In many terminal emulators the mouse works just fine, thus enable it.
if has('mouse')
set mouse=a
endif
" Ignore case when searching
set ignorecase
" When searching try to be smart about cases
set smartcase
" Highlight search results
set hlsearch
" Makes search act like search in modern browsers
set incsearch
" Don't redraw while executing macros (good performance config)
" set lazyredraw
" For regular expressions turn magic on
set magic
" Show matching brackets when text indicator is over them
set showmatch
" How many tenths of a second to blink when matching brackets
set mat=2
" No annoying sound on errors
set noerrorbells
set novisualbell
set t_vb=
set tm=500
" Add a bit extra margin to the left
set foldcolumn=1
" Keep cursor position when switch buffers
if v:version >= 700
au BufLeave * let b:winview = winsaveview()
au BufEnter * if(exists('b:winview')) | call winrestview(b:winview) | endif
endif
"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Colors and Fonts"{{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}
"{{{
" NOTICE : 'set background=dark' must before 'colorscheme sublime'
" add variable 'g:colorschemealreadyseted' prevent auto source look bad.
"{{{
function! SetColorScheme()
if exists('g:colorscheme_already_seted') == 0
syntax enable
set t_co=256
set background=dark
colorscheme molokai
let g:colorscheme_already_seted = 1
endif
endfunction
" Goyo Plugin Must call for well
call SetColorScheme()
"}}}
" Set extra options when running in GUI mode
"{{{
if has("gui_running")
set guioptions-=T
set guioptions-=e
set t_Co=256
set guitablabel=%M\ %t
" set guifont=Source\ Code\ Pro\ Medium\ 11
set guifont=SauceCodePro\ Nerd\ Font\ Medium\ 11
set guioptions-=m " hide menus
set guioptions-=T " hide tools
set guioptions-=L " hide scroll left
set guioptions-=r " hide scroll right
set guioptions-=b " hide scroll bottom
set showtabline=0 " hide tabline
set columns=130 lines=35
endif
"}}}
"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Files, backups and undo"{{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}
"{{{
" Turn backup off, since most stuff is in SVN, git et.c anyway...
set nobackup
set nowb
set noswapfile
"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Text, tab and indent related"{{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}
"{{{
" Use spaces instead of tabs
set expandtab
" Be smart when using tabs ;)
set smarttab
" 1 tab == 4 spaces
set shiftwidth=4
set tabstop=4
" Linebreak on 500 characters
set lbr
set tw=500
set ai "Auto indent
set si "Smart indent
set wrap "Wrap lines
"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Visual mode related"{{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}
"{{{
" Visual mode pressing * or # searches for the current selection
" Super useful! From an idea by Michael Naumann
vnoremap <silent> * :call VisualSelection('f', '')<CR>
vnoremap <silent> # :call VisualSelection('b', '')<CR>
"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Moving around, tabs, windows and buffers"{{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}
"{{{
" Treat long lines as break lines (useful when moving around in them)
map j gj
map k gk
" Map <Space> to / (search) and Ctrl-<Space> to ? (backwards search)
map <space> /
" Disable highlight when <leader><cr> is pressed
" use <C-[> or coh to replace it. <CR> in qf will be not get what it suppose to be.
" nnoremap <silent> <CR> :noh<CR>
nnoremap <silent> <Esc>h :noh<Esc>
nnoremap <silent> <Esc>o :only<CR>
nnoremap <silent> <Esc>q :q<CR>
" Smart way to move between windows
cabbrev vba vert ba
" window rezoom
" map <C-W><C-Left> <C-W><C-=>
" map <C-W><C-Right> <C-W><C-\|>
" map <C-W><C-Up> <C-W><C-_>
" map <C-W><C-Down> <C-W><C-=>
" Zoom / Restore window.
"{{{
function! s:ZoomToggle() abort
if exists('t:zoomed') && t:zoomed
execute t:zoom_winrestcmd
let t:zoomed = 0
else
let t:zoom_winrestcmd = winrestcmd()
resize
vertical resize
let t:zoomed = 1
endif
" integrate with tmux
call system('xdotool key F6 z')
endfunction
"}}}
command! ZoomToggle call s:ZoomToggle()
nnoremap <silent> <leader>a :ZoomToggle<CR>
" map like terminal
inoremap <C-E> <End>
inoremap <C-A> <Home>
" Make sure all the deleted buffers will unload when session start next.
" autocmd VimLeavePre * 1,1000argd
" Close the current buffer
map <leader>bd :Bclose<cr>
" Close all the buffers
map <leader>ba :%bd!<cr>
map <Leader>br :call BufferRightAllDelete()<cr>
map <Leader>bl :call BufferLeftAllDelete()<cr>
map <Leader>bo :call BufferOtherAllDelete()<cr>
" bug with function bufexists() , so use 'try ... catch ... end'
"{{{
function! BufferOtherAllDelete() abort
silent call BufferLeftAllDelete()
silent call BufferRightAllDelete()
echo "** Delete All Other Buffer"
endfunction
function! BufferLeftAllDelete() abort
let s:arrlist = GetBufListsNu()
let s:bufnums = bufnr('%')
try
silent exe s:arrlist[0] . ',' . s:bufnums . '-bd!'
catch
endtry
echo "** Delete All Buffer At Current Left."
endfunction
function! BufferRightAllDelete() abort
let s:arrlist = GetBufListsNu()
let s:bufnums = bufnr('%')
let i = index(s:arrlist, s:bufnums)
try
silent exe s:arrlist[i+1] . ',$bd!'
catch
endtry
echo "** Delete All Buffer At Current Right."
endfunction
"}}}
" Switch CWD to the directory of the open buffer
map <leader>cd :cd %:p:h<cr>:pwd<cr>
" Specify the behavior when switching between buffers
try
set switchbuf=useopen,usetab,newtab
set stal=2
catch
endtry
"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Status line"{{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}
"{{{
" Always show the status line
set laststatus=2
" Format the status line
set statusline=\ %{HasPaste()}%F%m%r%h\ %w\ \ CWD:\ %r%{getcwd()}%h\ \ \ Line:\ %l
"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Ack searching and cope displaying"{{{
" requires ack.vim - it's much better than vimgrep/grep"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"{{{
" When you press gv you Ack after the selected text
vnoremap <silent> gv :call VisualSelection('gv', '')<CR>
" Open Ack and put the cursor in the right position
" map <leader>g :CtrlSF
" When you press <leader>r you can search and replace the selected text
vnoremap <silent> <leader>r :call VisualSelection('replace', '')<CR>
" Do :help cope if you are unsure what cope is. It's super useful!
"
" When you search with Ack, display your results in cope by doing:
" <leader>cc
"
" To go to the next search result do:
" <leader>n
"
" To go to the previous search results do:
" <leader>p
"
map <leader>cc :botright cope<cr>
map <leader>co ggVGy:enew<cr>:set syntax=qf<cr>pgg
map <leader>n :cn<cr>
map <leader>p :cp<cr>
"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Misc"{{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}
"{{{
" Remove the Windows ^M - when the encodings gets messed up
noremap dwm mmHmt:%s/<C-V><cr>//ge<cr>'tzt'm
" Toggle paste mode on and off
nmap cop :setlocal paste!<cr>
"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Helper functions"{{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}
"{{{
function! CmdLine(str)
call feedkeys(":" . a:str)
endfunction
let g:virsual_selection_act = 0
function! VisualSelection(direction, extra_filter) range
let g:virsual_selection_act = 1
let l:saved_reg = @"
execute "normal! vgvy"
let l:pattern = escape(@", '\\/.*$^~[]')
let l:pattern = substitute(l:pattern, "\n$", "", "")
if a:direction == 'b'
let @/ = l:pattern
exec "normal N"
elseif a:direction == 'gv'
call CmdLine("CtrlSF \"" . l:pattern . "\" " )
elseif a:direction == 'replace'
call CmdLine("%s" . '/'. l:pattern . '/')
elseif a:direction == 'f'
let @/ = l:pattern
exec "normal n"
elseif a:direction == 'select'
execute printf("Select %s", l:pattern)
elseif a:direction == 'overcmdline'
let @/=''
execute printf("Replace %s", l:pattern)
endif
let g:virsual_selection_act = 0
let @/ = l:pattern
let @" = l:saved_reg
endfunction
" Returns true if paste mode is enabled
function! HasPaste()
if &paste
return 'PASTE MODE '
en
return ''
endfunction
" Don't close window, when deleting a buffer
command! Bclose call <SID>BufcloseCloseIt()
function! <SID>BufcloseCloseIt()
let l:currentBufNum = bufnr("%")
let l:alternateBufNum = bufnr("#")
if buflisted(l:alternateBufNum)
buffer #
else
bnext
endif
if bufnr("%") == l:currentBufNum
new
endif
if buflisted(l:currentBufNum)
execute("bdelete! ".l:currentBufNum)
endif
endfunction
"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Fast editing and reloading of vimrc configs"{{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}
"{{{
" map <leader>e :e! ~/.vimrc<cr>
autocmd! BufWritePost *.vimrc exec "source % | AirlineRefresh"
autocmd! BufWritePost *.vim-basic.vim exec "source % | AirlineRefresh"
autocmd! BufWritePost *.vim-plugin.vim exec "source % | AirlineRefresh"
"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Turn persistent undo on "{{{
" means that you can undo even when you close a buffer/VIM"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"{{{
try
set undodir=~/.vim/temp_dirs/undodir
set undofile
catch
endtry
if has('nvim')
set shada='20,<50,:20,%,n~/.nvim/nviminfo
else
set viminfo='20,<50,:20,%,n$HOME/.vim/files/info/viminfo
endif
" Return to last edit position when opening files (You want this!)
autocmd BufReadPost *
\ if line("'\"") > 0 && line("'\"") <= line("$") |
\ exe "normal! g`\"" |
\ endif
"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Command mode related"{{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}
"{{{
" Smart mappings on the command line
cno $h e ~/
cno $d e ~/Desktop/
cno $j e ./
cno $c e <C-\>eCurrentFileDir("e")<cr>
" $q is super useful when browsing on the command line
" it deletes everything until the last slash
cno $q <C-\>eDeleteTillSlash()<cr>
" Bash like keys for the command line
cnoremap <C-A> <Home>
cnoremap <C-E> <End>
cnoremap <C-K> <C-U>
cnoremap <C-N> <Down>
" Map ½ to something useful
map ½ $
cmap ½ $
imap ½ $
"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Parenthesis/bracket"{{{
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}
"{{{
" select text, then Quickly ouput '$1'
vnoremap $1 <esc>`>a)<esc>`<i(<esc>
vnoremap $2 <esc>`>a]<esc>`<i[<esc>
vnoremap $3 <esc>`>a}<esc>`<i{<esc>
vnoremap $$ <esc>`>a"<esc>`<i"<esc>
vnoremap $q <esc>`>a'<esc>`<i'<esc>
vnoremap $e <esc>`>a"<esc>`<i"<esc>
" Map auto complete of (, ", ', [
" inoremap $1 ()<esc>i
" inoremap $2 []<esc>i
" inoremap $3 {}<esc>i
" inoremap $4 {<esc>o}<esc>O
" inoremap $q ''<esc>i
" inoremap $e ""<esc>i
" inoremap $t <><esc>i
"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => General abbreviations"{{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}
"{{{
iab xdate <c-r>=strftime("%Y-%m-%d %H:%M:%S")<cr>
"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Custom Settings"{{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}
"{{{
set nonumber
set nocursorline
set nowrap
" cursorline toggle
cabbrev chx !chmod +x %
command! Reload :e!
" normal mode keypress 'K' will show help of function.
autocmd BufNewFile,Bufread *.vim set keywordprg="help"
" let QuicklyFix buffer out of list
autocmd FileType qf,vimfiler,git setlocal nobuflisted
autocmd BufWinEnter * if &previewwindow | setlocal nobuflisted | endif
" System clipboard sharing
if has('clipboard')
set clipboard=unnamedplus
endif
"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => key map"{{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}
"{{{
"Editing mappings{{{
" Remap VIM 0 to first non-blank character
map 0 ^
" Goback normal mode Quickly
" imap jj <Esc>
" Move a line of text using ALT+[jk] or Comamnd+[jk] on mac
nmap <M-j> mz:m+<cr>`z
nmap <M-k> mz:m-2<cr>`z
vmap <M-j> :m'>+<cr>`<my`>mzgv`yo`z
vmap <M-k> :m'<-2<cr>`>my`<mzgv`yo`z
" Delete trailing white space on save, useful for Python and CoffeeScript ;)
func! DeleteTrailingWS()
exe "normal mz"
%s/\s\+$//ge
exe "normal `z"
endfunc
command! TrailingWhiteSpaceDelete call DeleteTrailingWS()
autocmd BufWrite *.py :call DeleteTrailingWS()
autocmd BufWrite *.coffee :call DeleteTrailingWS()
"}}}
nnoremap <silent> <F4> :<C-u>q<cr>
" let the help buffer map 'q' to quit
autocmd FileType help,qf nmap <silent> <buffer> q :<C-u>q<CR>
autocmd FileType netrw nmap <silent> <buffer> qq <Leader>x
" It cause chaos without keyword exe to do map sometimes, just like below codes it will more one extra space if without 'exe'
autocmd BufEnter * if &previewwindow | exe 'nmap <buffer> q ZZ' | endif
autocmd BufEnter * if stridx(expand('%p'), 'fugitive://') > -1 | call CloseFromFugitiveView() | endif
function! CloseFromFugitiveView()
let s:filename = expand('%p')
let s:root = substitute(split(s:filename, '/.git/')[0], 'fugitive://', '', '')
let s:basename = fnamemodify(s:filename, ':p:t')
let s:real_filename = printf('%s/%s', s:root, s:basename)
if stridx(Vimcmd('nmap q'), 'No mapping found') > -1
let s:action = ''
if filereadable(s:real_filename)
let s:action = printf('e %s', s:real_filename)
else
let s:action = 'wincmd w'
endif
exec printf('nmap <buffer> q :%s<CR>', s:action)
endif
if &diff
nmap <buffer> q <Leader>x
endif
endfunction
" Smart quit in windows and buffers"{{{
map <silent> <leader>x :<C-U>call SmartQuit(0)<cr>
" use buffer 'nmap q' to quit if 'nmap q' is exsist
let g:smartqdebug = 0
function! NormalQOr(cmd) abort
if g:smartqdebug == 1 | exec 'sleep 3' | endif
if match(Vimcmd('nmap q'), '\cdelete\|exit\|close\|quit') > -1
exec 'normal q'
elseif index(['gitcommit'], &filetype) > -1
exec 'q'
else
exec a:cmd
end
endfunction
function! SmartQuit(tag) abort
" if gdiff run, call function once
if getwinvar('#', '&diff')
call NormalQOr('q')
if buflisted(bufnr('fugitive')) != 0
if g:smartqdebug == 1 | echo "delete from 0" | endif
call NormalQOr('bdelete! fugitive:')
endif
return
endif
" delete the current window
if a:tag == 0
if buflisted(bufnr('%')) == 0
if g:smartqdebug == 1 | echo "delete from 1" | endif
call NormalQOr('bdelete!')
else
if winnr('$') > 1
if g:smartqdebug == 1 | echo "delete from 2" | endif
call NormalQOr('q')
else
if g:smartqdebug == 1 | echo "delete from 3" | endif
call NormalQOr('bdelete!')
endif
endif
endif
" Close all specail window
exe 'pc'
exe 'lclose'
exe 'cclose'
" delete unuseful window
let winnums = winnr('$')
let bufnrtmp = []
if winnums > 0
let index = winnums
while index > 0
if buflisted(winbufnr(index)) == 0
call insert(bufnrtmp, winbufnr(index))
endif
let index -= 1
endwhile
if len(bufnrtmp) > 0
if g:smartqdebug == 1 | echo "delete from 4" | endif
call NormalQOr('bdelete!' . join(bufnrtmp, ' '))
endif
endif
" continue
if buflisted(bufnr('%')) == 0 && GetBufListedNr() > 0
if g:smartqdebug == 1 | echo "continue ..." | endif
call SmartQuit(1)
endif
endfunction
"}}}
" toggle conceal"{{{
nmap coC :ToggleConceal<CR>
command! ToggleConceal call s:ToogleConceal()
function! s:ToogleConceal() abort
exe 'set conceallevel=' . (&conceallevel==0 ? 2 : 0)
endfunction
"}}}
" show the command line
" nnoremap cm :
" use 'L' to switch cmdline quickly
nnoremap cme :e<Space>
nnoremap cms :e ~/.storge<cr>
nnoremap cmt :e /tmp/t.php<cr>
nnoremap cmp :e /tmp/p.py<cr>
nnoremap cmv :e /tmp/v.vim<cr>
nnoremap cmb :e /tmp/buffer<cr>
" split buffer can use <C-w>s and <C-w>v
" keymap for jumping window
for i in range(1,9)
exe printf( 'nnoremap <C-w>%d :%dwincmd w<CR>', i, i )
endfor
inoremap <C-V> <Esc>pa
" gvim: map break line
nmap <S-CR> o
imap <S-CR> <Esc>o
nmap <C-CR> o
imap <C-CR> <Esc>o
" file map
nmap new :enew<cr>
nmap <Leader>sa :sav<Space>
" if has("gui_running")
" noremap <C-Z> <cr>
" endif
" open temp buffer file to paste selection
exec "vnoremap <C-N> y:<C-U>e /tmp/buffer<cr><Esc>O" . repeat('-', 120) . "<cr><Esc>pggO<Esc>"
"Buffer Switch Quickly{{{
" map <silent> <M-0> :bl<cr>
" for n in [1,2,3,4,5,6,7,8,9]
" exe 'map <silent> <M-' . n . '> :call GoToBuffer('. n . ')<cr>'
" endfor
" map <silent> <Tab> :call GoToBufferNext('next')<cr>
" map <silent> <S-Tab> :call GoToBufferNext('prev')<cr>
func! GoToBufferNext(tag)
let s:commandstr = a:tag == 'next' ? 'bn' : 'bp'
" let s:arr = ['tagbar', 'nerdtree', 'qf', 'ctrlsf', 'runner']
" if index(s:arr, &filetype) == -1
let s:arrlist = GetBufListsNu()
if index(s:arrlist, bufnr('%')) > -1 && winnr('$') == 1
execute s:commandstr
endif
endfunc
function! GoToBuffer(tag) abort
let s:nr = GetBufNr(a:tag)
if s:nr > 0 && buflisted(bufnr('%'))
exe 'b' . s:nr
" if buflisted(a:tag)
" exe 'b' . a:tag
" elseif a:tag == 1
" exe 'b' . s:nr
endif
endfunction
"}}}
"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => vim-session, Remember opened file when exit"{{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}
"{{{
" MaximizeWindow
" The bottom will more one line when vim session is loaded in notebook.
" if has('gui_running')
" autocmd SessionLoadPost,GuiEnter * : call Maximize_Window()
" endif
" "{{{
" function Maximize_Window()
" " fixed bug: plugin vim-session command OpenSession blank a bottom line
" silent !wmctrl -r :ACTIVE: -b toggle,maximized_vert,maximized_horz
" " here is the originally main code only
" silent !wmctrl -r :ACTIVE: -b add,maximized_vert,maximized_horz
" " additional code
" lang en_US.utf8
" endfunction
"}}}
"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => search online"{{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}
"{{{
command! SearchOnline call OnlineDoc()
fun! OnlineDoc()
let s:browser = "google-chrome"
let s:wordUnderCursor = expand("<cword>")
let s:url = "https://www.google.com.hk/\\#newwindow=1\\&safe=strict\\&q=" . s:wordUnderCursor
let s:cmd = printf('%s %s', s:browser, s:url)
call system(s:cmd)
endfunction
"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => markdown preview"{{{
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""}}}
"{{{
" from http://www.jmlog.com/use-pandoc-to-preview-markdown-in-vim/
command! PreviewMarkdown call PreviewMarkdown()
"{{{
function! PreviewMarkdown()
if !executable('pandoc')
echohl ErrorMsg | echo 'Please install pandoc first.' | echohl None
return
endif
let BROWSER_COMMAND = 'xdg-open'
let output_file = tempname() . '.html'
let input_file = tempname() . '.md'
let css_file = 'file://' . expand('/opt/lampp/htdocs/tools/pandoc/markdown.css', 1)
" Convert buffer to UTF-8 before running pandoc
let original_encoding = &fileencoding
let original_bomb = &bomb
silent! execute 'set fileencoding=utf-8 nobomb'
" Generate html file for preview
let content = getline(1, '$')
let newContent = []
for line in content
let str = matchstr(line, '\(!\[.*\](\)\@<=.\+\.\%(png\|jpe\=g\|gif\)')
if str != "" && match(str, '^https\=:\/\/') == -1
let newLine = substitute(line, '\(!\[.*\]\)(' . str . ')',
\'\1(file://' . escape(expand("%:p:h", 1), '\') .
\('/') .
\escape(expand(str, 1), '\') . ')', 'g')
else
let newLine = line
endif
call add(newContent, newLine)
endfor
call writefile(newContent, input_file)
let cmd = printf('pandoc -f markdown_github -t html -s -S -c "%s" -o "%s" "%s"', css_file, output_file, input_file)
call system(cmd)
call delete(input_file)
" Change encoding back
silent! execute 'set fileencoding=' . original_encoding . ' ' . original_bomb
" Preview
let cmd = printf('%s "%s" &>/dev/null', BROWSER_COMMAND, output_file)
call system(cmd)
execute input('Press ENTER to continue...')
echo
call delete(output_file)
endfunction
"}}}
"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => open url under cursor with browser, replace default command 'gx'"{{{
" default commands 'gf' can open file under cursor, 'gd' can highlight word"}}}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"{{{
nmap gx :call XOpenURIUnderCursor()<CR>
nmap yu :call XCopyURIUnderCursor()<CR>
nmap gf :call EditURIUnderCursor()<CR>
"{{{
fun! EditURIUnderCursor()
let s:uri = GetURIUnderCursor()
if filereadable(expand(s:uri))
exe printf('e %s', fnameescape(s:uri))
endif
endf