aboutsummaryrefslogtreecommitdiffhomepage
path: root/vim/init/keymaps.vim
diff options
context:
space:
mode:
Diffstat (limited to 'vim/init/keymaps.vim')
-rw-r--r--vim/init/keymaps.vim174
1 files changed, 92 insertions, 82 deletions
diff --git a/vim/init/keymaps.vim b/vim/init/keymaps.vim
index 1eb2b20..ce5d315 100644
--- a/vim/init/keymaps.vim
+++ b/vim/init/keymaps.vim
@@ -3,7 +3,7 @@
3"====================================================================== 3"======================================================================
4" vim: sw=2 ts=2 foldmethod=marker foldmarker={{{,}}} 4" vim: sw=2 ts=2 foldmethod=marker foldmarker={{{,}}}
5 5
6" COMMON_MAPPING ----------------{{{ 6" COMMON_MAPPING {{{
7 7
8" Space for searching 8" Space for searching
9map <space> / 9map <space> /
@@ -11,9 +11,6 @@ map <space> /
11" Escape normal mode by <C-c> 11" Escape normal mode by <C-c>
12inoremap <C-c> <Esc>l 12inoremap <C-c> <Esc>l
13 13
14" Search for selected test
15vnoremap * y/\V<C-R>=escape(@",'/\')<CR><CR>
16
17" Set wrap 14" Set wrap
18nnoremap <leader>W :set wrap!<CR> 15nnoremap <leader>W :set wrap!<CR>
19 16
@@ -39,23 +36,6 @@ augroup vimrc_CRfix
39 autocmd CmdwinEnter * nnoremap <buffer> <C-c> <C-c> 36 autocmd CmdwinEnter * nnoremap <buffer> <C-c> <C-c>
40augroup END 37augroup END
41 38
42" In case ALT key is not working
43" execute "set <M-2>=\e2"
44" execute "set <M-1>=\e1"
45" execute "set <M-3>=\e3"
46" execute "set <M-4>=\e4"
47" execute "set <M-5>=\e5"
48" execute "set <M-6>=\e6"
49" execute "set <M-7>=\e7"
50" execute "set <M-8>=\e8"
51" execute "set <M-9>=\e9"
52" execute "set <M-0>=\e0"
53" execute "set <M-f>=\ef"
54" execute "set <M-b>=\eb"
55" execute "set <M-d>=\ed"
56" execute "set <M-l>=\el"
57" execute "set <M-h>=\eh"
58
59" Spell 39" Spell
60nnoremap \s :set spell!<CR>:set spell?<CR> 40nnoremap \s :set spell!<CR>:set spell?<CR>
61nnoremap <leader>ss ]s 41nnoremap <leader>ss ]s
@@ -68,9 +48,22 @@ nnoremap <C-g> 1<C-g>
68vnoremap Tz :!trans -t zh-TW -b<CR> 48vnoremap Tz :!trans -t zh-TW -b<CR>
69vnoremap Te :!trans -t en-US -b<CR> 49vnoremap Te :!trans -t en-US -b<CR>
70 50
51let g:alacritty_extra_padding = 0
52function! ToggleWinPadding()
53 if g:alacritty_extra_padding
54 !alacritty msg config --window-id $WINDOWID --reset
55 else
56 redir => output | hi Normal | redir END
57 let bg_color = matchstr(output, 'guibg=\zs[^\s]\+\ze')
58 exe "!alacritty msg config --window-id $WINDOWID window.padding.x=300 'colors.primary.background=\"\\"..bg_color.."\"'"
59 endif
60
61 let g:alacritty_extra_padding = !g:alacritty_extra_padding
62endfunc
63nnoremap <leader>Z <Cmd>silent call ToggleWinPadding()<CR>
71 64
72" }}} 65" }}}
73" WORKING_DIR ----------------{{{ 66" WORKING_DIR {{{
74 67
75let g:last_path = execute("pwd") 68let g:last_path = execute("pwd")
76augroup SaveLatestDir 69augroup SaveLatestDir
@@ -103,7 +96,7 @@ function! InCaseCdToLatestDir()
103endfunction 96endfunction
104 97
105" }}} 98" }}}
106" MOTION ----------------{{{ 99" MOTION {{{
107 100
108" j/k will move virtual lines (lines that wrap) 101" j/k will move virtual lines (lines that wrap)
109nnoremap <silent> <expr> j (v:count == 0 ? 'gj' : 'j') 102nnoremap <silent> <expr> j (v:count == 0 ? 'gj' : 'j')
@@ -121,7 +114,7 @@ xnoremap iq i"
121xnoremap aq a" 114xnoremap aq a"
122 115
123 116
124" READLINE_FEATURES ----------------{{{ 117" READLINE_FEATURES {{{
125 118
126inoremap <C-f> <Right> 119inoremap <C-f> <Right>
127inoremap <C-b> <Left> 120inoremap <C-b> <Left>
@@ -155,7 +148,7 @@ nnoremap <m-k> gk
155inoremap <m-j> <c-\><c-o>gj 148inoremap <m-j> <c-\><c-o>gj
156inoremap <m-k> <c-\><c-o>gk 149inoremap <m-k> <c-\><c-o>gk
157" }}} 150" }}}
158" JUMP_TO_TABS_WITH_ALT ----------------{{{ 151" JUMP_TO_TABS_WITH_ALT {{{
159 152
160nnoremap <silent><A-1> :tabn 1<CR> 153nnoremap <silent><A-1> :tabn 1<CR>
161nnoremap <silent><A-2> :tabn 2<CR> 154nnoremap <silent><A-2> :tabn 2<CR>
@@ -200,11 +193,29 @@ nnoremap <C-k> ddkP
200nnoremap S S<ESC> 193nnoremap S S<ESC>
201 194
202" }}} 195" }}}
203" MANAGE_VIMRC ----------------{{{ 196" TERMINAL {{{
197" In case ALT key is not working
198" execute "set <M-2>=\e2"
199" execute "set <M-1>=\e1"
200" execute "set <M-3>=\e3"
201" execute "set <M-4>=\e4"
202" execute "set <M-5>=\e5"
203" execute "set <M-6>=\e6"
204" execute "set <M-7>=\e7"
205" execute "set <M-8>=\e8"
206" execute "set <M-9>=\e9"
207" execute "set <M-0>=\e0"
208" execute "set <M-f>=\ef"
209" execute "set <M-b>=\eb"
210" execute "set <M-d>=\ed"
211" execute "set <M-l>=\el"
212" execute "set <M-h>=\eh"
213"}}}
214" MANAGE_VIMRC {{{
204 215
205" source .vimrc 216" source .vimrc
206nnoremap <leader>so V:so<CR> 217nnoremap <leader>so V:so<CR>
207nnoremap <leader><leader>so :source ~/.vimrc<CR> 218nnoremap <leader><leader>so :source %<CR>
208vnoremap so :source<CR> 219vnoremap so :source<CR>
209autocmd! BUFWRITEPOST $MYVIMRC source $MYVIMRC 220autocmd! BUFWRITEPOST $MYVIMRC source $MYVIMRC
210 221
@@ -213,7 +224,7 @@ nnoremap <leader>e :scriptnames<space>
213nnoremap <leader>ee :edit $MYVIMRC<CR> 224nnoremap <leader>ee :edit $MYVIMRC<CR>
214 225
215" }}} 226" }}}
216" MANAGE_BUFFERS ----------------{{{ 227" MANAGE_BUFFERS {{{
217 228
218" Set options 229" Set options
219nnoremap so :set<space> 230nnoremap so :set<space>
@@ -236,19 +247,25 @@ augroup SaveLastBuffer
236augroup END 247augroup END
237nnoremap <leader>l :exe "buffer ".g:lastbuffer<CR> 248nnoremap <leader>l :exe "buffer ".g:lastbuffer<CR>
238 249
239" Use Ctrl-C for buffer delete or quit vim ----------------{{{ 250" Use Ctrl-C for buffer delete or quit vim {{{
240 251
241" Toggle behavior for the last buffer in the last window 252" Toggle behavior for the last buffer in the last window
242let g:quitVimWhenPressingCtrlC = 1 253let g:quitVimWhenPressingCtrlC = 1
243function! ToggleQuit() 254function! ToggleQuit()
244 let g:quitVimWhenPressingCtrlC = g:quitVimWhenPressingCtrlC ? 0 : 1 255 let g:quitVimWhenPressingCtrlC = !g:quitVimWhenPressingCtrlC
245 let message = g:quitVimWhenPressingCtrlC ? "Unlock" : "Lock" 256 let message = g:quitVimWhenPressingCtrlC ? "Unlock" : "Lock"
246 echo message 257 echo message
247endfunction 258endfunction
248nnoremap \q :call ToggleQuit()<CR> 259nnoremap \q :call ToggleQuit()<CR>
249 260
261func! QuitWithCheck()
262 if g:quitVimWhenPressingCtrlC
263 silent! quit
264 else
265 echo "Press \\q to allow quit with <C-c>"
266 endif
267endfunc
250function! CloseBufferSafely() 268function! CloseBufferSafely()
251 let l:bufnr = bufnr()
252 " Ask Saving 269 " Ask Saving
253 if &modified 270 if &modified
254 let answer = confirm("Save changes?", "&Yes\n&No\n&Cancel") 271 let answer = confirm("Save changes?", "&Yes\n&No\n&Cancel")
@@ -257,31 +274,37 @@ function! CloseBufferSafely()
257 if answer == "" | return | endif 274 if answer == "" | return | endif
258 endif 275 endif
259 276
260 if g:tab_group[tabpagenr()] == [l:bufnr] 277 let l:bufnr = bufnr()
261 bdelete 278
279 if len(t:bufs) == 1
280 " Close tab for last buffer
281 tabclose
262 else 282 else
263 bprevious | bd # 283 " Switch to proper buffer
284 let l:next_buf = get(t:bufs, bufnr('#')) ? bufnr('#') : filter(t:bufs, 'v:val != '..l:bufnr)[0]
285 exe "b "..l:next_buf
286 call filter(t:bufs, 'v:val != '..l:bufnr)
264 endif 287 endif
288
289 " Delete buffer if every t:buf doesn't have it
290 for tab in gettabinfo()
291 if get(tab.variables.bufs, l:bufnr) | return | endif
292 endfor
293 exe "bd! "..l:bufnr
294
265endfunction 295endfunction
266func! QuitWithCheck()
267 if g:quitVimWhenPressingCtrlC
268 silent! quit
269 else
270 echo "Press \\q to allow quit with <C-c>"
271 endif
272endfunc
273function! Bye() 296function! Bye()
274 let windows = gettabinfo(tabpagenr())[0]['windows'] 297 let windows = gettabinfo(tabpagenr())[0]['windows']
275 let bufs = getbufinfo({'buflisted': 1})
276 298
277 if len(windows) == 1 && len(bufs) == 1 299 if len(t:bufs) <= 1 && len(windows) == 1
278 call QuitWithCheck() 300 call QuitWithCheck()
279 elseif &diff 301 elseif &diff
280 silent call CloseBuffersForDiff() 302 silent call CloseBuffersForDiff()
281 elseif len(windows) >1 303 elseif len(windows) >1
282 quit 304 quit
283 else 305 else
284 silent! call CloseBufferSafely() 306 call CloseBufferSafely()
307 " silent! call CloseBufferSafely()
285 endif 308 endif
286endfunction 309endfunction
287nnoremap <silent> <C-c> :call Bye()<CR> 310nnoremap <silent> <C-c> :call Bye()<CR>
@@ -325,7 +348,7 @@ nnoremap <C-w>D <Cmd>silent! SwitchDiffForGitHEAD<CR>
325" }}} 348" }}}
326 349
327" }}} 350" }}}
328" MANAGE_WINDOWS ----------------{{{ 351" MANAGE_WINDOWS {{{
329 352
330nnoremap <leader><leader>sb :windo set scrollbind!<CR> 353nnoremap <leader><leader>sb :windo set scrollbind!<CR>
331 354
@@ -361,7 +384,7 @@ elseif has('nvim')
361 tnoremap <m-q> <c-\><c-n> 384 tnoremap <m-q> <c-\><c-n>
362endif 385endif
363" }}} 386" }}}
364" MANAGE_TABS ----------------{{{ 387" MANAGE_TABS {{{
365 388
366" Useful mappings for managing tabs 389" Useful mappings for managing tabs
367map <leader>tn :tabnew<CR> 390map <leader>tn :tabnew<CR>
@@ -395,13 +418,13 @@ function! Tab_MoveRight()
395 endif 418 endif
396endfunc 419endfunc
397" }}} 420" }}}
398" FOLD ----------------{{{ 421" FOLD {{{
399 422
400" Set fold options 423" Set fold options
401nnoremap <leader><leader>fm :<C-\>e'set foldmethod='..&foldmethod<CR> 424nnoremap <leader><leader>fm :<C-\>e'set foldmethod='..&foldmethod<CR>
402nnoremap <leader><leader>fc :<C-\>e'set foldcolumn='..&foldcolumn<CR> 425nnoremap <leader><leader>fc :<C-\>e'set foldcolumn='..&foldcolumn<CR>
403 426
404nnoremap zi zizz 427nnoremap zi zizz:silent exe &foldenable ? "set foldcolumn=auto:3" : "set foldcolumn=0"<CR>
405 428
406" Show fold level when it changes 429" Show fold level when it changes
407nnoremap zm zm:set foldlevel<CR> 430nnoremap zm zm:set foldlevel<CR>
@@ -415,7 +438,9 @@ nnoremap zF :call ToggleUnfoldSelection()<CR>zv
415nnoremap \z :call GrayOutOtherFolds()<CR> 438nnoremap \z :call GrayOutOtherFolds()<CR>
416 439
417" Select current fold 440" Select current fold
441onoremap az :<C-U>silent! keepjumps normal![zV]z<CR>
418xnoremap az :<C-U>silent! keepjumps normal![zV]z<CR> 442xnoremap az :<C-U>silent! keepjumps normal![zV]z<CR>
443onoremap iz :<C-U>silent! keepjumps normal![zjV]zk<CR>
419xnoremap iz :<C-U>silent! keepjumps normal![zjV]zk<CR> 444xnoremap iz :<C-U>silent! keepjumps normal![zjV]zk<CR>
420 445
421" Use l to open fold 446" Use l to open fold
@@ -464,7 +489,7 @@ function! GrayOutOtherFolds()
464endfunction 489endfunction
465 490
466" }}} 491" }}}
467" HIGHLIGHT ----------------{{{ 492" HIGHLIGHT {{{
468 493
469" Disable highlight when <leader><CR> is pressed 494" Disable highlight when <leader><CR> is pressed
470nnoremap <silent> <leader><CR> :noh<CR> 495nnoremap <silent> <leader><CR> :noh<CR>
@@ -497,7 +522,7 @@ nnoremap <silent> <leader>gh :call matchadd('MultiLineHighlight', '\%'.line('.')
497nnoremap <silent> <leader>gH :call clearmatches()<CR> 522nnoremap <silent> <leader>gH :call clearmatches()<CR>
498 523
499" }}} 524" }}}
500" SURROUND ----------------{{{ 525" SURROUND {{{
501 526
502inoremap ' ''<Left> 527inoremap ' ''<Left>
503inoremap " ""<Left> 528inoremap " ""<Left>
@@ -530,14 +555,14 @@ endfunction
530vnoremap <space> :<C-u>call AddSpaceForSelection()<CR> 555vnoremap <space> :<C-u>call AddSpaceForSelection()<CR>
531 556
532" }}} 557" }}}
533" QUICKFIX ----------------{{{ 558" QUICKFIX {{{
534 559
535nnoremap <leader>cn :cn<CR> 560nnoremap <leader>cn :cn<CR>
536nnoremap <leader>cp :cp<CR> 561nnoremap <leader>cp :cp<CR>
537nnoremap <leader>cw :cw 10<CR> 562nnoremap <leader>cw :cw 10<CR>
538 563
539" }}} 564" }}}
540" REDIRECTION_WITH_BUFFER ----------------{{{ 565" REDIRECTION_WITH_BUFFER {{{
541 566
542" Usage: 567" Usage:
543" :Redir hi ............. show the full output of command ':hi' in a scratch window 568" :Redir hi ............. show the full output of command ':hi' in a scratch window
@@ -561,38 +586,23 @@ command! -nargs=1 -complete=command Redir silent call Redir(<q-args>)
561command! -nargs=1 -complete=command R silent call Redir(<q-args>) 586command! -nargs=1 -complete=command R silent call Redir(<q-args>)
562nnoremap <leader>rr :Redir<space> 587nnoremap <leader>rr :Redir<space>
563" }}} 588" }}}
564" QUICK_SUBSTITUTE ----------------{{{ 589" SEARCH/SUBSTITUTE {{{
565 590
566" Usage: Press <TAB> n times for area, and <CR> for substitute 591" Search for selected test
592vnoremap * y/\V<C-R>=escape(@",'/\')<CR><CR>
567 593
568" substitute across file
569vnoremap <leader>s y:%s//<C-R>0/g<LEFT><LEFT> 594vnoremap <leader>s y:%s//<C-R>0/g<LEFT><LEFT>
570 595
571let g:search_not_in_register = 1 596" Usage: Press <TAB> n times for area, and <CR> for substitute
572" When leaving visual mode, resume search_not_in_register 597let g:search_selection = 0
573autocmd Modechanged [vV\x16]*:* let g:search_not_in_register = 1 598" When leaving visual mode, resume search_selection
574 599autocmd Modechanged [vV\x16]*:* let g:search_selection = 0
575function! ExpandSelectionBySearch(sep) 600xmap <expr> <TAB> g:search_selection ? "//e<CR>" : "*:let g:search_selection = 1<CR>gv//e<CR>"
576 if g:search_not_in_register 601xmap <expr> <S-TAB> g:search_selection ? "??<CR>" : "*:let g:search_selection = 1<CR>gv??<CR>"
577 " Save current selection to register, and keep selection 602vnoremap <CR> :s//<C-R>0/g<Left><Left>
578 norm! ygv
579 let g:search_not_in_register = 0
580 endif
581 " Use register s to go to next search, counts/total is displayed in
582 " statusline
583 call feedkeys(a:sep.."\<C-R>0"..a:sep.."e\<CR>")
584endfunction
585function! SubstituteBySearch()
586 " Apply current search for default substitute text
587 call feedkeys(":s//\<C-R>0/g\<Left>\<Left>")
588endfunction
589
590vnoremap <TAB> <Cmd>call ExpandSelectionBySearch('/')<CR>
591vnoremap <S-TAB> <Cmd>call ExpandSelectionBySearch('?')<CR>
592vnoremap <CR> <Cmd>call SubstituteBySearch()<CR>
593 603
594" }}} 604" }}}
595" SIGN ----------------{{{ 605" SIGN {{{
596 606
597nnoremap <leader><leader>sc :<C-\>e'set signcolumn='..&signcolumn<CR> 607nnoremap <leader><leader>sc :<C-\>e'set signcolumn='..&signcolumn<CR>
598 608
@@ -600,7 +610,7 @@ nnoremap <leader>si :exe ":sign place " .. line('.') .. " line=" .. line('.') ..
600nnoremap <leader>sI :exe ":sign unplace * file=" .. expand("%:p")<CR> 610nnoremap <leader>sI :exe ":sign unplace * file=" .. expand("%:p")<CR>
601 611
602" }}} 612" }}}
603" GIT_TIG ----------------{{{ 613" GIT_TIG {{{
604 614
605let g:tig_explorer_keymap_commit_split = '<C-s>' 615let g:tig_explorer_keymap_commit_split = '<C-s>'
606let g:tig_explorer_keymap_commit_vsplit = '<C-v>' 616let g:tig_explorer_keymap_commit_vsplit = '<C-v>'
@@ -609,7 +619,7 @@ nnoremap <C-t>s <Cmd>TigStatus<CR>
609nnoremap <C-t>b <Cmd>TigBlame<CR> 619nnoremap <C-t>b <Cmd>TigBlame<CR>
610 620
611" }}} 621" }}}
612" Tmp: Markdown items (temproray solution) ----------------{{{ 622" Tmp: Markdown items (temproray solution) {{{
613 623
614" Toggle list item in markdown: "- [ ] XXX" -> "XXX" -> "- XXX" -> "- [ ] XXX" 624" Toggle list item in markdown: "- [ ] XXX" -> "XXX" -> "- XXX" -> "- [ ] XXX"
615" autocmd FileType markdown nnoremap <buffer> <leader>i V:!sed -E '/^ *- \[.\]/ { s/^( *)- \[.\] */\1/; q; }; /^ *[^[:space:]-]/ { s/^( *)/\1- /; q; }; /^ *- / { s/^( *)- /\1- [ ] /; q; }'<CR><CR> 625" autocmd FileType markdown nnoremap <buffer> <leader>i V:!sed -E '/^ *- \[.\]/ { s/^( *)- \[.\] */\1/; q; }; /^ *[^[:space:]-]/ { s/^( *)/\1- /; q; }; /^ *- / { s/^( *)- /\1- [ ] /; q; }'<CR><CR>
@@ -618,14 +628,14 @@ nnoremap <C-t>b <Cmd>TigBlame<CR>
618" Toggle task status: "- [ ] " -> "- [x]" -> "- [.] " -> "- [ ] " 628" Toggle task status: "- [ ] " -> "- [x]" -> "- [.] " -> "- [ ] "
619" nnoremap <leader>x V:!sed -E '/^ *- \[ \]/ { s/^( *)- \[ \]/\1- [x]/; q; }; /^ *- \[\x\]/ { s/^( *)- \[\x\]/\1- [.]/; q; }; /^ *- \[\.\]/ { s/^( *)- \[\.\]/\1- [ ]/; q; }'<CR><CR> 629" nnoremap <leader>x V:!sed -E '/^ *- \[ \]/ { s/^( *)- \[ \]/\1- [x]/; q; }; /^ *- \[\x\]/ { s/^( *)- \[\x\]/\1- [.]/; q; }; /^ *- \[\.\]/ { s/^( *)- \[\.\]/\1- [ ]/; q; }'<CR><CR>
620" }}} 630" }}}
621" Tmp: Common system command ----------------{{{ 631" Tmp: Common system command {{{
622" Show date selector 632" Show date selector
623nnoremap <leader>dd :r !sh -c 'LANG=en zenity --calendar --date-format="\%Y.\%m.\%d" 2>/dev/null'<CR><CR> 633nnoremap <leader>dd :r !sh -c 'LANG=en zenity --calendar --date-format="\%Y.\%m.\%d" 2>/dev/null'<CR><CR>
624nnoremap <leader>dD :r !sh -c 'LANG=en zenity --calendar --date-format="\%a \%b \%d" 2>/dev/null'<CR><CR> 634nnoremap <leader>dD :r !sh -c 'LANG=en zenity --calendar --date-format="\%a \%b \%d" 2>/dev/null'<CR><CR>
625nnoremap <leader>dt :r !date +\%H:\%m<CR>A 635nnoremap <leader>dt :r !date +\%H:\%m<CR>A
626 636
627" }}} 637" }}}
628" Tmp: Compile ----------------{{{ 638" Tmp: Compile {{{
629 639
630" 编译运行 C/C++ 项目 640" 编译运行 C/C++ 项目
631" 详细见:http://www.skywind.me/blog/archives/2084 641" 详细见:http://www.skywind.me/blog/archives/2084