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/basic.vim | 65 ++++++--------- vim/init/config.vim | 1 + vim/init/keymaps.vim | 230 ++++++++++++++++++++++++++++++++------------------- vim/lua/chadrc.lua | 21 +++-- vim/lua/mappings.lua | 2 +- 5 files changed, 179 insertions(+), 140 deletions(-) diff --git a/vim/init/basic.vim b/vim/init/basic.vim index a54bf59..c066f07 100644 --- a/vim/init/basic.vim +++ b/vim/init/basic.vim @@ -12,8 +12,10 @@ augroup filetype_vim execute "autocmd FileType vim :inoreabbrev --- ----------------{".."{{" autocmd FileType vim setlocal foldmethod=marker foldlevel=0 augroup END + " }}} " GERERNAL ----------------{{{ + let mapleader = "," " Always use comma as leader key set nocompatible " Disable vi compatible, today is 20XX set path=.,** " Allow :find with completion @@ -32,6 +34,7 @@ filetype plugin indent on " Unnamed buffer like CmdWindows should prevent this set autoread autocmd FocusGained,BufEnter .* checktime + " }}} " VISUAL ----------------{{{ @@ -64,14 +67,12 @@ set wildmenu wildoptions=pum,fuzzy " Format of error message set errorformat+=[%f:%l]\ ->\ %m,[%f:%l]:%m -" 顯示分隔符號 -set listchars=tab:\|\ ,trail:.,extends:>,precedes:< - " }}} " EDIT ----------------{{{ set backspace=eol,start,indent " Set Backspace behaviors -set autoindent " If current line has indent, automatically set indent for next line +set autoindent smartindent +set shiftwidth=2 set cindent set ttimeout set ttimeoutlen=50 @@ -80,14 +81,14 @@ set ttimeoutlen=50 imap l -" TABSIZE ----------------{{{ +" TAB ----------------{{{ set expandtab -set shiftwidth=2 -set autoindent -set tabstop=4 -set softtabstop=0 -set smartindent +set softtabstop=-1 + +" 顯示分隔符號 +set list +set listchars=tab:▷▷,extends:>,precedes:< " }}} @@ -99,39 +100,15 @@ set suffixesadd=.md " Enable reference markdown file without extension " }}} " SEARCH ----------------{{{ -set ignorecase " Search case without case sensation + +set ignorecase " Search case without case sensation set smartcase -set hlsearch " Hilight all matched texts -set incsearch " Show matched strings when typing +set hlsearch " Hilight all matched texts +set incsearch " Show matched strings when typing + " }}} " BUFFERS ----------------{{{ -" Use to quit the last buffer ----------------{{{ -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! Bye() - if len(getbufinfo({'buflisted': 1})) == 1 && len(getwininfo()) == 1 - if g:quitVimWhenPressingCtrlC - :silent! quit - else - :echo "Press gl to allow quit with " - endif - else - :bdelete - endif -endfunction - -" Ctrl-C rules!!! -nnoremap :call Bye() - -" }}} " Go to last cursor position ----------------{{{ augroup vimStartup au! @@ -141,7 +118,7 @@ augroup vimStartup " (it's likely a different one than last time). autocmd BufReadPost * \ if line("'\"") >= 1 && line("'\"") <= line("$") && &ft !~# 'commit' - \ | exe "normal! g`\"" + \ | exe "normal! g`\"zv" \ | endif augroup END @@ -158,10 +135,12 @@ if has('multi_byte') endif " }}} -" FOLDING ----------------{{{ +" FOLD ----------------{{{ + set foldenable " Allow fold set foldmethod=indent " Fold contents by indent set foldlevel=2 + " }}} " BACKUP ----------------{{{ @@ -176,8 +155,10 @@ set backupdir=~/.vim/tmp " backup when write file set writebackup + " }}} " HIGHLIGHT ----------------{{{ + syntax enable set conceallevel=1 @@ -186,6 +167,7 @@ function! GetHighlightGroupName() 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 !? @@ -203,6 +185,7 @@ highlight MultiLineHighlight ctermbg=LightYellow guibg=LightYellow ctermfg=Black 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 ae623ec..074f09d 100644 --- a/vim/init/config.vim +++ b/vim/init/config.vim @@ -6,6 +6,7 @@ " Unnamed Buffer ----------------{{{ augroup DeleteUnnamedEmptBuffer! + au! au BufLeave {} if getline(1, '$') == [''] | setlocal bufhidden=wipe | endif augroup END 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) ----------------{{{ diff --git a/vim/lua/chadrc.lua b/vim/lua/chadrc.lua index 6006371..f9c2357 100644 --- a/vim/lua/chadrc.lua +++ b/vim/lua/chadrc.lua @@ -5,7 +5,7 @@ local M = {} M.ui = { - theme = "bearded-arc", + theme = "onedark", -- hl_override = { -- Comment = { italic = true }, @@ -33,15 +33,15 @@ M.ui = { -- For tabufline if M.ui.tabufline.enabled then - vim.keymap.set("n", "", function() - local bufnrs = vim.tbl_filter(function(b) return 1 == vim.fn.buflisted(b) end, vim.api.nvim_list_bufs()) - if #bufnrs == 1 or #vim.fn.getwininfo() > 1 then - vim.cmd("silent quit") - else - -- require("nvchad.tabufline").close_buffer() - vim.cmd("bdelete") - end - end, { desc = "buffer close" }) + -- vim.keymap.set("n", "", function() + -- local bufnrs = vim.tbl_filter(function(b) return 1 == vim.fn.buflisted(b) end, vim.api.nvim_list_bufs()) + -- if #bufnrs == 1 or #vim.fn.getwininfo() > 1 then + -- vim.cmd("silent quit") + -- else + -- require("nvchad.tabufline").close_buffer() + -- -- vim.cmd("bdelete") + -- end + -- end, { desc = "buffer close" }) for i = 1, 9, 1 do vim.keymap.set("n", string.format("", i), function() @@ -57,5 +57,4 @@ if M.ui.tabufline.enabled then end - return M diff --git a/vim/lua/mappings.lua b/vim/lua/mappings.lua index 159971c..07107e5 100644 --- a/vim/lua/mappings.lua +++ b/vim/lua/mappings.lua @@ -1,7 +1,7 @@ -- For NvChad -- require "nvchad.mappings" vim.keymap.set("n", "ch", "NvCheatsheet", { desc = "toggle nvcheatsheet" }) -vim.keymap.set("n", "th", "Telescope themes", { desc = "telescope nvchad themes" }) +vim.keymap.set("n", "th", "Telescope themes", { desc = "telescope nvchad themes" }) -- General vim.keymap.set("i", "jk", "") -- cgit v1.2.3-70-g09d2