From e3ef197ddd07f5711db97bb7fdbe838c474cba7f Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Wed, 10 Jul 2024 18:07:25 +0800 Subject: Update --- vim/init/keymaps.vim | 230 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 143 insertions(+), 87 deletions(-) (limited to 'vim/init/keymaps.vim') diff --git a/vim/init/keymaps.vim b/vim/init/keymaps.vim index f46e67b..38bdd0e 100644 --- a/vim/init/keymaps.vim +++ b/vim/init/keymaps.vim @@ -1,7 +1,6 @@ "====================================================================== " Only for key mapping "====================================================================== -" vim: set ts=4 sw=4 tw=78 noet : " COMMON_MAPPING ----------------{{{ @@ -14,29 +13,22 @@ vnoremap * y/\V=escape(@",'/\') " Disable highlight when is pressed map :noh -" Paste register 0 -nnoremap "0p +" Set wrap +nnoremap W :set wrap!:set wrap? " Fast saving nmap w :w! +" :W sudo saves the file +" (useful for handling the permission-denied error) +command! W execute 'w !sudo -S tee %' edit! + " Fast quit with error nmap q :q " Fast quit with error nmap cq :cq -" Switch wrap -nmap W :set wrap! - -" :W sudo saves the file -" (useful for handling the permission-denied error) -command! W execute 'w !sudo -S tee %' edit! - -" Enter to open file -nnoremap gf -nnoremap gF :e - " Remap in Quickfix, Cmdwin Location list augroup vimrc_CRfix au! @@ -49,13 +41,25 @@ augroup END " nnoremap , :terminal ++noclose vnoremap , :terminal +" Paste register 0 +nnoremap "0p + " Toggle paste mode on and off map pp :setlocal paste! +" Copy from system clipboard +nnoremap P :r !xsel -ob +vnoremap Y :w !xsel -ib + " Switch CWD to the directory of the open buffer nnoremap cd :cd %:p:h:pwd " Switch CWD to root git directory -nnoremap cdg :execute 'cd' fnameescape(fnamemodify(finddir('.git', escape(expand('%:p:h'), ' ') . ';'), ':h')):pwd +function! CdToGitRepo() + let l:git_dir = finddir('.git', escape(expand('%:p:h'), ' ') . ';') + let l:repo = fnameescape(fnamemodify(l:git_dir, ':h')) + execute "cd" l:repo +endfunction +nnoremap cdg :call CdToGitRepo():pwd " alias for cd nnoremap cdd :cd @@ -83,10 +87,6 @@ nnoremap ddkP " execute "set =\el" " execute "set =\eh" -" Copy from system clipboard -nnoremap P :r !xsel -ob -vnoremap Y :w !xsel -ib - " Spell nnoremap sp :set spell! nnoremap ss ]s @@ -99,11 +99,6 @@ nnoremap 1 vnoremap Tz :!trans -t zh-TW -b vnoremap Te :!trans -t en-US -b -" source .vimrc -nnoremap so V:so -nnoremap so :source ~/.vimrc -vnoremap so :source - " }}} " MOVE ----------------{{{ @@ -115,15 +110,11 @@ noremap k (v:count == 0 ? 'gk' : 'k') noremap 30h noremap 30l -" }}} -" MANAGE_VIMRC ----------------{{{ +" File under the cursor +nnoremap gf +nnoremap gF :e -nnoremap e :scriptnames -nnoremap ee :edit $MYVIMRC -autocmd! BUFWRITEPOST $MYVIMRC source $MYVIMRC - -" }}} -" MOVING_WITH_READLINE ----------------{{{ +" READLINE_FEATURES ----------------{{{ inoremap inoremap @@ -156,15 +147,6 @@ noremap gj noremap gk inoremap gj inoremap gk -" }}} -" INSERT_SURROUNDING ----------------{{{ - -inoremap ' '' -inoremap " "" -inoremap ( () -inoremap [ [] -inoremap { {} - " }}} " JUMP_TO_TABS_WITH_ALT ----------------{{{ @@ -188,87 +170,122 @@ inoremap :tabn 8 inoremap :tablast " }}} -" MANAGE_TABS ----------------{{{ -" Useful mappings for managing tabs -map tn :tabnew -map tc :tabclose -map tm :tabmove -map to :tabonly - -noremap :call Tab_MoveLeft() -noremap :call Tab_MoveRight() +" }}} +" MANAGE_VIMRC ----------------{{{ -" Let tl toggle between this and the last accessed tab -let g:lasttab = 1 -nmap tl :exe "tabn ".g:lasttab -autocmd TabLeave * let g:lasttab = tabpagenr() +" source .vimrc +nnoremap so V:so +nnoremap so :source ~/.vimrc +vnoremap so :source +autocmd! BUFWRITEPOST $MYVIMRC source $MYVIMRC -" Opens a new tab with the current buffer's path -" Super useful when editing files in the same directory -map te :tabedit =expand("%:p:h") +" Find scripts +nnoremap e :scriptnames +nnoremap ee :edit $MYVIMRC -" Tab move functions -function! Tab_MoveLeft() - let l:tabnr = tabpagenr() - 2 - if l:tabnr >= 0 - exec 'tabmove '.l:tabnr - endif -endfunc -function! Tab_MoveRight() - let l:tabnr = tabpagenr() + 1 - if l:tabnr <= tabpagenr('$') - exec 'tabmove '.l:tabnr - endif -endfunc " }}} " MANAGE_BUFFERS ----------------{{{ -" set buflisted +" Set options noremap st :set +noremap ft :e'set filetype='..&filetype " Open a new buffer -nmap b :enew nmap B :enew nmap O :e /tmp/buffer -" Next buffer -noremap l :exe "buffer ".g:lastbuffer -" noremap :exe 'buffer '.g:lastbuffer - -" set filetype -noremap ft :e'set filetype='..&filetype - " Let l toggle between this and the last accessed buffer augroup SaveLastBuffer let g:lastbuffer = 1 au BufLeave * let g:lastbuffer = bufnr() augroup END +noremap l :exe "buffer ".g:lastbuffer + +" 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 message = g:quitVimWhenPressingCtrlC ? "Unlock" : "Lock" + echo message +endfunction +nnoremap gl :call ToggleQuit() + +function! CheckSave() + if &modified + let answer = confirm("Save changes?", "&Yes\n&No\n&Cancel") + if answer == 1 | write | endif + if answer == 3 | return | endif + endif + + if len(getbufinfo({'buflisted': 1})) == 2 + try | buffer # | bdelete! # | catch | endtry + else + bdelete! + endif +endfunction +func! QuitWithCheck() + if g:quitVimWhenPressingCtrlC + silent! quit + else + echo "Press gl to allow quit with " + endif +endfunc +function! Bye() + let windows = gettabinfo(tabpagenr())[0]['windows'] + let bufs = gettabinfo(tabpagenr())[0]['variables']['bufs'] + if len(windows) == 1 && len(bufs) == 1 + echo 'quit' + call QuitWithCheck() + elseif &diff + call CloseBuffersForDiff() + else + echo 'bdelete' + call CheckSave() + endif +endfunction +nnoremap :call Bye() + +"}}} +" Diff Mode ----------------{{{ + +function! CloseBuffersForDiff() + windo | if &diff && &buftype == "nofile" | bdelete | endif + norm! zv +endfunction + +command! DiffOrig vert new | set buftype=nofile nobuflisted | read ++edit # | 0d_ +\ | diffthis | wincmd p | diffthis " Uset d to toggle Diff mode function! s:SwitchDiff() if &diff - windo | if &buftype == "nofile" | bdelete | endif + call CloseBuffersForDiff() else DiffOrig - wincmd p | set nobuflisted | wincmd p endif endfunction com! SwitchDiff call s:SwitchDiff() nnoremap d silent! SwitchDiff function! s:SwitchDiffForGitHEAD() - nmap cdg + norm cdg if &diff windo | if &buftype == "nofile" | bdelete | endif + norm! zv else vert new | set buftype=nofile nobuflisted read !git show HEAD:# 0d_ | diffthis | wincmd p | diffthis endif endfunction -com! SwitchDiffForGitHEAD call s:SwitchDiffForGitHEAD() +command! SwitchDiffForGitHEAD call s:SwitchDiffForGitHEAD() nnoremap D silent! SwitchDiffForGitHEAD + +" }}} + " }}} " MANAGE_WINDOWS ----------------{{{ @@ -306,7 +323,41 @@ elseif has('nvim') tnoremap endif " }}} -" FOLDING ----------------{{{ +" MANAGE_TABS ----------------{{{ + +" Useful mappings for managing tabs +map tn :tabnew +map tc :tabclose +map tm :tabmove +map to :tabonly + +noremap :call Tab_MoveLeft() +noremap :call Tab_MoveRight() + +" Let tl toggle between this and the last accessed tab +let g:lasttab = 1 +nmap tl :exe "tabn ".g:lasttab +autocmd TabLeave * let g:lasttab = tabpagenr() + +" Opens a new tab with the current buffer's path +" Super useful when editing files in the same directory +map te :tabedit =expand("%:p:h") + +" Tab move functions +function! Tvab_MoveLeft() + let l:tabnr = tabpagenr() - 2 + if l:tabnr >= 0 + exec 'tabmove '.l:tabnr + endif +endfunc +function! Tab_MoveRight() + let l:tabnr = tabpagenr() + 1 + if l:tabnr <= tabpagenr('$') + exec 'tabmove '.l:tabnr + endif +endfunc +" }}} +" FOLD ----------------{{{ " Set foldmethod noremap fm :e'set foldmethod='..&foldmethod @@ -317,13 +368,19 @@ nnoremap zr zr:set foldlevel " Use l to open fold nnoremap l foldclosed('.') == -1 ? 'l' : 'zo' -sourcesourcesource + " Open fold in next line nnoremap zo foldclosed('.') == -1 ? 'zjzo' : 'zo' nnoremap zO foldclosed('.') == -1 ? 'zjzO' : 'zO' " }}} -" SURROURD_WITH_CHAR ----------------{{{ +" SURROUND ----------------{{{ + +inoremap ' '' +inoremap " "" +inoremap ( () +inoremap [ [] +inoremap { {} vnoremap S sa vnoremap ' ``>la' @@ -403,7 +460,6 @@ let g:tig_explorer_keymap_commit_vsplit = '' nnoremap Tig nnoremap s TigStatus nnoremap b TigBlame -nnoremap d :vertical TigOpenFileWithCommit + % 0 " }}} " Markdown items (temproray solution) ----------------{{{ -- cgit v1.2.3-70-g09d2