From 058fa5ecbf152be3864b3a510aec418b18c35734 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Thu, 11 Jul 2024 23:49:29 +0800 Subject: Update --- vim/init/keymaps.vim | 188 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 124 insertions(+), 64 deletions(-) (limited to 'vim/init/keymaps.vim') diff --git a/vim/init/keymaps.vim b/vim/init/keymaps.vim index f71ec7c..0132706 100644 --- a/vim/init/keymaps.vim +++ b/vim/init/keymaps.vim @@ -7,6 +7,9 @@ " Space for searching map / +" Escape normal mode by +imap l + " Search for selected test vnoremap * y/\V=escape(@",'/\') @@ -23,10 +26,8 @@ nmap w :w! " (useful for handling the permission-denied error) command! W execute 'w !sudo -S tee %' edit! -" Fast quit with error +" Quit nmap q :q - -" Fast quit with error nmap cq :cq " Remap in Quickfix, Cmdwin Location list @@ -88,7 +89,8 @@ nnoremap ddkP " execute "set =\eh" " Spell -nnoremap sp :set spell! +nnoremap sp ": echo spell "..(echo &spell ? "on" : "off").."" +nnoremap sp ": echo spell " nnoremap ss ]s nnoremap S [s @@ -190,6 +192,10 @@ nnoremap ee :edit $MYVIMRC " Set options noremap st :set noremap ft :e'set filetype='..&filetype +noremap li :set list! +noremap sw :e'set shiftwidth='..&shiftwidth +noremap nu :set number! +noremap ru :set relativenumber! " Open a new buffer nmap B :enew @@ -213,43 +219,53 @@ function! ToggleQuit() endfunction nnoremap gl :call ToggleQuit() -function! CheckSave() +function! CloseBufferSafely() if &modified let answer = confirm("Save changes?", "&Yes\n&No\n&Cancel") if answer == 1 | write | endif if answer == 3 | return | endif endif - bdelete! - -endfunction -func! QuitWithCheck() - if g:quitVimWhenPressingCtrlC - silent! quit + let bufs = getbufinfo({'buflisted': 1}) + if len(bufs) == 1 + bdelete! else - echo "Press gl to allow quit with " + b# | bd! # 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 windows = gettabinfo(tabpagenr())[0]['windows'] +try let bufs = gettabinfo(tabpagenr())[0]['variables']['bufs'] - - if len(windows) == 1 && len(bufs) == 1 - call QuitWithCheck() - elseif &diff - call CloseBuffersForDiff() - else - call CheckSave() - endif +catch + let bufs = getbufinfo({'buflisted': 1}) +endtry + +if len(windows) == 1 && len(bufs) == 1 + call QuitWithCheck() +elseif &diff + call CloseBuffersForDiff() +elseif len(windows) >1 + quit +else + silent! call CloseBufferSafely() +endif endfunction nnoremap :call Bye() -"}}} -" Diff Mode ----------------{{{ + +" Diff Mode ---------------- function! CloseBuffersForDiff() - windo | if &diff && &buftype == "nofile" | bdelete | endif - norm! zv + windo | if &diff && &buftype == "nofile" | bdelete | endif + norm! zv endfunction command! DiffOrig vert new | set buftype=nofile nobuflisted | read ++edit # | 0d_ @@ -257,25 +273,25 @@ command! DiffOrig vert new | set buftype=nofile nobuflisted | read ++edit # | 0d " Uset d to toggle Diff mode function! s:SwitchDiff() - if &diff - call CloseBuffersForDiff() - else - DiffOrig - endif +if &diff + call CloseBuffersForDiff() +else + DiffOrig +endif endfunction com! SwitchDiff call s:SwitchDiff() nnoremap d silent! SwitchDiff function! s:SwitchDiffForGitHEAD() - 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 +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 command! SwitchDiffForGitHEAD call s:SwitchDiffForGitHEAD() nnoremap D silent! SwitchDiffForGitHEAD @@ -301,22 +317,22 @@ inoremap j inoremap k if has('terminal') && exists(':terminal') == 2 && has('patch-8.1.1') - " vim 8.1 支持 termwinkey ,不需要把 terminal 切换成 normal 模式 - " 设置 termwinkey 为 CTRL 加减号(GVIM),有些终端下是 CTRL+? - " 后面四个键位是搭配 termwinkey 的,如果 termwinkey 更改,也要改 - set termwinkey= - tnoremap h - tnoremap l - tnoremap j - tnoremap k - tnoremap +" vim 8.1 支持 termwinkey ,不需要把 terminal 切换成 normal 模式 +" 设置 termwinkey 为 CTRL 加减号(GVIM),有些终端下是 CTRL+? +" 后面四个键位是搭配 termwinkey 的,如果 termwinkey 更改,也要改 +set termwinkey= +tnoremap h +tnoremap l +tnoremap j +tnoremap k +tnoremap elseif has('nvim') - " neovim 没有 termwinkey 支持,必须把 terminal 切换回 normal 模式 - tnoremap h - tnoremap l - tnoremap j - tnoremap k - tnoremap +" neovim 没有 termwinkey 支持,必须把 terminal 切换回 normal 模式 +tnoremap h +tnoremap l +tnoremap j +tnoremap k +tnoremap endif " }}} " MANAGE_TABS ----------------{{{ @@ -341,35 +357,44 @@ 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 +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 +let l:tabnr = tabpagenr() + 1 +if l:tabnr <= tabpagenr('$') + exec 'tabmove '.l:tabnr +endif endfunc " }}} " FOLD ----------------{{{ " Set foldmethod noremap fm :e'set foldmethod='..&foldmethod +noremap fc :e'set foldcolumn='..&foldcolumn " Show fold level when it changes nnoremap zm zm:set foldlevel nnoremap zr zr:set foldlevel " Fold file except selection +let g:original_foldmethod = "" function! UnfoldSelectionOnly() - set foldmethod=manual + echo 'Unfold'..&foldmethod + let g:original_foldmethod = &foldmethod + let &foldmethod = "manual" norm! zE execute "0,'<-1fold" execute "'>+1,$fold" endfunction -vnoremap zF :call UnfoldSelectionOnly() +function! ResumeFoldmethod() + norm! zE + let &foldmethod = g:original_foldmethod ? g:original_foldmethod : "indent" +endfunc +vnoremap zF :call UnfoldSelectionOnly() +nnoremap zF :call ResumeFoldmethod() " Use l to open fold nnoremap l foldclosed('.') == -1 ? 'l' : 'zo' @@ -378,6 +403,36 @@ nnoremap l foldclosed('.') == -1 ? 'l' : 'zo' nnoremap zo foldclosed('.') == -1 ? 'zjzo' : 'zo' nnoremap zO foldclosed('.') == -1 ? 'zjzO' : 'zO' +" }}} +" HIGHLIGHT ----------------{{{ + +function! HiFile() + let i = 1 + while i <= line("$") + if strlen(getline(i)) > 0 && len(split(getline(i))) > 2 + let w = split(getline(i))[0] + let l:command = "syn match " . w . " /^" . w . "\\s\\+xxx/" + exe l:command + endif + let i += 1 + endwhile +endfunction + +function! GetHighlightGroupName() + let l:syntaxID = synID(line('.'), col('.'), 1) + let l:groupName = synIDattr(l:syntaxID, 'name') + echo "Highlight Group Name: " . l:groupName +endfunction +nnoremap H :call GetHighlightGroupName() + +" Persist visualized lines +" define line highlight color +highlight MultiLineHighlight ctermbg=LightYellow guibg=LightYellow ctermfg=Black guifg=Black +" highlight the current line +nnoremap gh :call matchadd('MultiLineHighlight', '\%'.line('.').'l') +" clear all the highlighted lines +nnoremap gH :call clearmatches() + " }}} " SURROUND ----------------{{{ @@ -397,6 +452,11 @@ vnoremap ` ``>la` vnoremap ``>la vnoremap Q ``>la」 +" }}} +" QUICKFIX ----------------{{{ +nnoremap cn :cn +nnoremap cp :cp +nnoremap cw :cw 10 " }}} " REDIRECTION_WITH_BUFFER ----------------{{{ -- cgit v1.2.3-70-g09d2