From c680ac2a4b06ebdc2da9a05311f01495c73f9c01 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Tue, 30 Jul 2024 17:56:29 +0800 Subject: Update --- vim/init/keymaps.vim | 174 +++++++++++++++++++++++++++------------------------ 1 file changed, 92 insertions(+), 82 deletions(-) (limited to 'vim/init/keymaps.vim') 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 @@ "====================================================================== " vim: sw=2 ts=2 foldmethod=marker foldmarker={{{,}}} -" COMMON_MAPPING ----------------{{{ +" COMMON_MAPPING {{{ " Space for searching map / @@ -11,9 +11,6 @@ map / " Escape normal mode by inoremap l -" Search for selected test -vnoremap * y/\V=escape(@",'/\') - " Set wrap nnoremap W :set wrap! @@ -39,23 +36,6 @@ augroup vimrc_CRfix autocmd CmdwinEnter * nnoremap augroup END -" In case ALT key is not working -" execute "set =\e2" -" execute "set =\e1" -" execute "set =\e3" -" execute "set =\e4" -" execute "set =\e5" -" execute "set =\e6" -" execute "set =\e7" -" execute "set =\e8" -" execute "set =\e9" -" execute "set =\e0" -" execute "set =\ef" -" execute "set =\eb" -" execute "set =\ed" -" execute "set =\el" -" execute "set =\eh" - " Spell nnoremap \s :set spell!:set spell? nnoremap ss ]s @@ -68,9 +48,22 @@ nnoremap 1 vnoremap Tz :!trans -t zh-TW -b vnoremap Te :!trans -t en-US -b +let g:alacritty_extra_padding = 0 +function! ToggleWinPadding() + if g:alacritty_extra_padding + !alacritty msg config --window-id $WINDOWID --reset + else + redir => output | hi Normal | redir END + let bg_color = matchstr(output, 'guibg=\zs[^\s]\+\ze') + exe "!alacritty msg config --window-id $WINDOWID window.padding.x=300 'colors.primary.background=\"\\"..bg_color.."\"'" + endif + + let g:alacritty_extra_padding = !g:alacritty_extra_padding +endfunc +nnoremap Z silent call ToggleWinPadding() " }}} -" WORKING_DIR ----------------{{{ +" WORKING_DIR {{{ let g:last_path = execute("pwd") augroup SaveLatestDir @@ -103,7 +96,7 @@ function! InCaseCdToLatestDir() endfunction " }}} -" MOTION ----------------{{{ +" MOTION {{{ " j/k will move virtual lines (lines that wrap) nnoremap j (v:count == 0 ? 'gj' : 'j') @@ -121,7 +114,7 @@ xnoremap iq i" xnoremap aq a" -" READLINE_FEATURES ----------------{{{ +" READLINE_FEATURES {{{ inoremap inoremap @@ -155,7 +148,7 @@ nnoremap gk inoremap gj inoremap gk " }}} -" JUMP_TO_TABS_WITH_ALT ----------------{{{ +" JUMP_TO_TABS_WITH_ALT {{{ nnoremap :tabn 1 nnoremap :tabn 2 @@ -200,11 +193,29 @@ nnoremap ddkP nnoremap S S " }}} -" MANAGE_VIMRC ----------------{{{ +" TERMINAL {{{ +" In case ALT key is not working +" execute "set =\e2" +" execute "set =\e1" +" execute "set =\e3" +" execute "set =\e4" +" execute "set =\e5" +" execute "set =\e6" +" execute "set =\e7" +" execute "set =\e8" +" execute "set =\e9" +" execute "set =\e0" +" execute "set =\ef" +" execute "set =\eb" +" execute "set =\ed" +" execute "set =\el" +" execute "set =\eh" +"}}} +" MANAGE_VIMRC {{{ " source .vimrc nnoremap so V:so -nnoremap so :source ~/.vimrc +nnoremap so :source % vnoremap so :source autocmd! BUFWRITEPOST $MYVIMRC source $MYVIMRC @@ -213,7 +224,7 @@ nnoremap e :scriptnames nnoremap ee :edit $MYVIMRC " }}} -" MANAGE_BUFFERS ----------------{{{ +" MANAGE_BUFFERS {{{ " Set options nnoremap so :set @@ -236,19 +247,25 @@ augroup SaveLastBuffer augroup END nnoremap l :exe "buffer ".g:lastbuffer -" Use Ctrl-C for buffer delete or quit vim ----------------{{{ +" Use Ctrl-C for buffer delete or quit vim {{{ " Toggle behavior for the last buffer in the last window let g:quitVimWhenPressingCtrlC = 1 function! ToggleQuit() - let g:quitVimWhenPressingCtrlC = g:quitVimWhenPressingCtrlC ? 0 : 1 + let g:quitVimWhenPressingCtrlC = !g:quitVimWhenPressingCtrlC let message = g:quitVimWhenPressingCtrlC ? "Unlock" : "Lock" echo message endfunction nnoremap \q :call ToggleQuit() +func! QuitWithCheck() + if g:quitVimWhenPressingCtrlC + silent! quit + else + echo "Press \\q to allow quit with " + endif +endfunc function! CloseBufferSafely() - let l:bufnr = bufnr() " Ask Saving if &modified let answer = confirm("Save changes?", "&Yes\n&No\n&Cancel") @@ -257,31 +274,37 @@ function! CloseBufferSafely() if answer == "" | return | endif endif - if g:tab_group[tabpagenr()] == [l:bufnr] - bdelete + let l:bufnr = bufnr() + + if len(t:bufs) == 1 + " Close tab for last buffer + tabclose else - bprevious | bd # + " Switch to proper buffer + let l:next_buf = get(t:bufs, bufnr('#')) ? bufnr('#') : filter(t:bufs, 'v:val != '..l:bufnr)[0] + exe "b "..l:next_buf + call filter(t:bufs, 'v:val != '..l:bufnr) endif + + " Delete buffer if every t:buf doesn't have it + for tab in gettabinfo() + if get(tab.variables.bufs, l:bufnr) | return | endif + endfor + exe "bd! "..l:bufnr + endfunction -func! QuitWithCheck() - if g:quitVimWhenPressingCtrlC - silent! quit - else - echo "Press \\q to allow quit with " - endif -endfunc function! Bye() let windows = gettabinfo(tabpagenr())[0]['windows'] - let bufs = getbufinfo({'buflisted': 1}) - if len(windows) == 1 && len(bufs) == 1 + if len(t:bufs) <= 1 && len(windows) == 1 call QuitWithCheck() elseif &diff silent call CloseBuffersForDiff() elseif len(windows) >1 quit else - silent! call CloseBufferSafely() + call CloseBufferSafely() + " silent! call CloseBufferSafely() endif endfunction nnoremap :call Bye() @@ -325,7 +348,7 @@ nnoremap D silent! SwitchDiffForGitHEAD " }}} " }}} -" MANAGE_WINDOWS ----------------{{{ +" MANAGE_WINDOWS {{{ nnoremap sb :windo set scrollbind! @@ -361,7 +384,7 @@ elseif has('nvim') tnoremap endif " }}} -" MANAGE_TABS ----------------{{{ +" MANAGE_TABS {{{ " Useful mappings for managing tabs map tn :tabnew @@ -395,13 +418,13 @@ function! Tab_MoveRight() endif endfunc " }}} -" FOLD ----------------{{{ +" FOLD {{{ " Set fold options nnoremap fm :e'set foldmethod='..&foldmethod nnoremap fc :e'set foldcolumn='..&foldcolumn -nnoremap zi zizz +nnoremap zi zizz:silent exe &foldenable ? "set foldcolumn=auto:3" : "set foldcolumn=0" " Show fold level when it changes nnoremap zm zm:set foldlevel @@ -415,7 +438,9 @@ nnoremap zF :call ToggleUnfoldSelection()zv nnoremap \z :call GrayOutOtherFolds() " Select current fold +onoremap az :silent! keepjumps normal![zV]z xnoremap az :silent! keepjumps normal![zV]z +onoremap iz :silent! keepjumps normal![zjV]zk xnoremap iz :silent! keepjumps normal![zjV]zk " Use l to open fold @@ -464,7 +489,7 @@ function! GrayOutOtherFolds() endfunction " }}} -" HIGHLIGHT ----------------{{{ +" HIGHLIGHT {{{ " Disable highlight when is pressed nnoremap :noh @@ -497,7 +522,7 @@ nnoremap gh :call matchadd('MultiLineHighlight', '\%'.line('.') nnoremap gH :call clearmatches() " }}} -" SURROUND ----------------{{{ +" SURROUND {{{ inoremap ' '' inoremap " "" @@ -530,14 +555,14 @@ endfunction vnoremap :call AddSpaceForSelection() " }}} -" QUICKFIX ----------------{{{ +" QUICKFIX {{{ nnoremap cn :cn nnoremap cp :cp nnoremap cw :cw 10 " }}} -" REDIRECTION_WITH_BUFFER ----------------{{{ +" REDIRECTION_WITH_BUFFER {{{ " Usage: " :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() command! -nargs=1 -complete=command R silent call Redir() nnoremap rr :Redir " }}} -" QUICK_SUBSTITUTE ----------------{{{ +" SEARCH/SUBSTITUTE {{{ -" Usage: Press n times for area, and for substitute +" Search for selected test +vnoremap * y/\V=escape(@",'/\') -" substitute across file vnoremap s y:%s//0/g -let g:search_not_in_register = 1 -" When leaving visual mode, resume search_not_in_register -autocmd Modechanged [vV\x16]*:* let g:search_not_in_register = 1 - -function! ExpandSelectionBySearch(sep) - if g:search_not_in_register - " Save current selection to register, and keep selection - norm! ygv - let g:search_not_in_register = 0 - endif - " Use register s to go to next search, counts/total is displayed in - " statusline - call feedkeys(a:sep.."\0"..a:sep.."e\") -endfunction -function! SubstituteBySearch() - " Apply current search for default substitute text - call feedkeys(":s//\0/g\\") -endfunction - -vnoremap call ExpandSelectionBySearch('/') -vnoremap call ExpandSelectionBySearch('?') -vnoremap call SubstituteBySearch() +" Usage: Press n times for area, and for substitute +let g:search_selection = 0 +" When leaving visual mode, resume search_selection +autocmd Modechanged [vV\x16]*:* let g:search_selection = 0 +xmap g:search_selection ? "//e" : "*:let g:search_selection = 1gv//e" +xmap g:search_selection ? "??" : "*:let g:search_selection = 1gv??" +vnoremap :s//0/g " }}} -" SIGN ----------------{{{ +" SIGN {{{ nnoremap sc :e'set signcolumn='..&signcolumn @@ -600,7 +610,7 @@ nnoremap si :exe ":sign place " .. line('.') .. " line=" .. line('.') .. nnoremap sI :exe ":sign unplace * file=" .. expand("%:p") " }}} -" GIT_TIG ----------------{{{ +" GIT_TIG {{{ let g:tig_explorer_keymap_commit_split = '' let g:tig_explorer_keymap_commit_vsplit = '' @@ -609,7 +619,7 @@ nnoremap s TigStatus nnoremap b TigBlame " }}} -" Tmp: Markdown items (temproray solution) ----------------{{{ +" Tmp: Markdown items (temproray solution) {{{ " Toggle list item in markdown: "- [ ] XXX" -> "XXX" -> "- XXX" -> "- [ ] XXX" " autocmd FileType markdown nnoremap i V:!sed -E '/^ *- \[.\]/ { s/^( *)- \[.\] */\1/; q; }; /^ *[^[:space:]-]/ { s/^( *)/\1- /; q; }; /^ *- / { s/^( *)- /\1- [ ] /; q; }' @@ -618,14 +628,14 @@ nnoremap b TigBlame " Toggle task status: "- [ ] " -> "- [x]" -> "- [.] " -> "- [ ] " " nnoremap x V:!sed -E '/^ *- \[ \]/ { s/^( *)- \[ \]/\1- [x]/; q; }; /^ *- \[\x\]/ { s/^( *)- \[\x\]/\1- [.]/; q; }; /^ *- \[\.\]/ { s/^( *)- \[\.\]/\1- [ ]/; q; }' " }}} -" Tmp: Common system command ----------------{{{ +" Tmp: Common system command {{{ " Show date selector nnoremap dd :r !sh -c 'LANG=en zenity --calendar --date-format="\%Y.\%m.\%d" 2>/dev/null' nnoremap dD :r !sh -c 'LANG=en zenity --calendar --date-format="\%a \%b \%d" 2>/dev/null' nnoremap dt :r !date +\%H:\%mA " }}} -" Tmp: Compile ----------------{{{ +" Tmp: Compile {{{ " 编译运行 C/C++ 项目 " 详细见:http://www.skywind.me/blog/archives/2084 -- cgit v1.2.3-70-g09d2