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/basic.vim | 48 ++++++------- vim/init/config.vim | 48 +++++++------ vim/init/keymaps.vim | 188 +++++++++++++++++++++++++++++++++------------------ vim/init/style.vim | 1 + 4 files changed, 170 insertions(+), 115 deletions(-) (limited to 'vim/init') diff --git a/vim/init/basic.vim b/vim/init/basic.vim index c066f07..42dab16 100644 --- a/vim/init/basic.vim +++ b/vim/init/basic.vim @@ -76,19 +76,17 @@ set shiftwidth=2 set cindent set ttimeout set ttimeoutlen=50 -" set updatetime=1000 +" set updatetime=4000 " autocmd CursorHold * normal! m' -imap l - " TAB ----------------{{{ set expandtab set softtabstop=-1 -" 顯示分隔符號 -set list -set listchars=tab:▷▷,extends:>,precedes:< +" Invisible chars +set nolist +set listchars=tab:»·,extends:>,precedes:< " }}} @@ -118,12 +116,26 @@ augroup vimStartup " (it's likely a different one than last time). autocmd BufReadPost * \ if line("'\"") >= 1 && line("'\"") <= line("$") && &ft !~# 'commit' - \ | exe "normal! g`\"zv" + \ | try | silent execute 'normal! g`"zv' | endtry \ | endif augroup END " }}} +" }}} +" FOLD ----------------{{{ +set foldenable " Allow fold +set foldmethod=indent " Fold contents by indent +set foldlevel=2 +set fillchars+=foldopen:▽,foldsep:│,foldclose:▶ +let g:defaut_foldcolumn = "" +if has('nvim') + let g:defaut_foldcolumn = "auto:5" +else + let g:defaut_foldcolumn = 5 +endif +let &foldcolumn = g:defaut_foldcolumn + " }}} " ENCODING_PREFERENCE ----------------{{{ @@ -134,13 +146,6 @@ if has('multi_byte') set fileencodings=utf-8,big5,ucs-bom,gbk,gb18030,euc-jp,latin1 endif -" }}} -" FOLD ----------------{{{ - -set foldenable " Allow fold -set foldmethod=indent " Fold contents by indent -set foldlevel=2 - " }}} " BACKUP ----------------{{{ @@ -162,13 +167,6 @@ set writebackup syntax enable set conceallevel=1 -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() - " Defualt highlight for matched parenthesis is so weird in many colorscheme " Why the background color is lighter than my caret !? " highlight MatchParen ctermfg=NONE ctermbg=darkgrey cterm=NONE @@ -178,14 +176,6 @@ highlight LuaParen ctermfg=NONE ctermbg=darkgrey cterm=NONE highlight ExtraWhitespace ctermbg=red guibg=red match ExtraWhitespace /\s\+$/ -" 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() - " }}} " MISC ----------------{{{ diff --git a/vim/init/config.vim b/vim/init/config.vim index 903d8a0..4e1c06c 100644 --- a/vim/init/config.vim +++ b/vim/init/config.vim @@ -5,6 +5,7 @@ " Unnamed Buffer ----------------{{{ +" Automatically delete unnamed empty buffer when leaving augroup DeleteUnnamedEmptBuffer! au! au BufLeave {} if getline(1, '$') == [''] | setlocal bufhidden=wipe | endif @@ -15,15 +16,14 @@ augroup END augroup TerminalSize au! - function! LayoutForSmall() - echo 'vim resized' - if &lines < 18 - set cmdheight=0 laststatus=0 showtabline=0 signcolumn=no nowrap scrolloff=1 + function! LayoutForSmallTerminal() + if &lines < 19 + silent! set cmdheight=0 laststatus=0 showtabline=0 signcolumn=no nowrap scrolloff=1 else - set cmdheight& laststatus& showtabline=2 signcolumn=yes scrolloff=3 + silent! set cmdheight& laststatus& showtabline=2 signcolumn=yes scrolloff=3 endif endfunction - autocmd VimResized * call LayoutForSmall() + autocmd VimEnter,VimResized * call LayoutForSmallTerminal() augroup END " }}} @@ -144,24 +144,28 @@ augroup InitFileTypes autocmd BufReadPost * call ApplyShebang() " }}} " Markdown ----------------{{{ - au FileType markdown setlocal wrap - au FileType markdown set sw=2 ts=2 - " Fold by heading level - function! MarkdownLevel() - let hash_num = matchstr(getline(v:lnum), '^#\+') - let hash_num_at_top = matchstr(getline(v:lnum-1), '^#\+') - if empty(hash_num) - if empty(hash_num_at_top) - return "=" + + augroup Config_Markdown + au! + au FileType markdown setlocal wrap sw=2 ts=2 + au FileType markdown setlocal foldexpr=MarkdownLevel() foldmethod=expr + + " Fold by heading level + function! MarkdownLevel() + let hash_num = matchstr(getline(v:lnum), '^#\+') + let hash_num_at_top = matchstr(getline(v:lnum-1), '^#\+') + if empty(hash_num) + if empty(hash_num_at_top) + return "=" + else + return ">"..(len(hash_num_at_top)) + endif else - return ">"..(len(hash_num_at_top)) + return len(hash_num) - 1 endif - else - return len(hash_num)-1 - endif - endfunction - au FileType markdown setlocal foldexpr=MarkdownLevel() - au FileType markdown setlocal foldmethod=expr + endfunction + + augroup END " }}} " HTML ----------------{{{ 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 ----------------{{{ diff --git a/vim/init/style.vim b/vim/init/style.vim index f2b1a5a..7c65e82 100644 --- a/vim/init/style.vim +++ b/vim/init/style.vim @@ -8,6 +8,7 @@ "====================================================================== " vim: set ts=4 sw=4 tw=78 noet : +colorscheme desert "---------------------------------------------------------------------- " 显示设置 -- cgit v1.2.3-70-g09d2