From 9b564fcfeec95ed965252ef4acc6ee42702ded88 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Sun, 28 Jul 2024 21:01:19 +0800 Subject: Update --- vim/init/basic.vim | 133 +++++++++++++++++++++++++++++++++++++-------------- vim/init/config.vim | 30 ++++++------ vim/init/keymaps.vim | 115 +++++++++++++++++++++++--------------------- 3 files changed, 171 insertions(+), 107 deletions(-) (limited to 'vim/init') diff --git a/vim/init/basic.vim b/vim/init/basic.vim index 2394045..e18d51f 100644 --- a/vim/init/basic.vim +++ b/vim/init/basic.vim @@ -4,7 +4,7 @@ " Used for general usecases. No keymap and personal preference "====================================================================== -" Vimscript file settings ---------------------- {{{ +" For Vimscript {{{ " Usage: type --- for foldmark augroup filetype_vim @@ -14,13 +14,63 @@ augroup filetype_vim augroup END " }}} -" GERERNAL ----------------{{{ +" For Buffer and Tab {{{ +augroup tabinfo + au! + let g:tab_group = {} + " BufEnter {{{ + function! AddBufToTabGroup(tab_group) + let l:tabId = tabpagenr() + let l:bufnr = bufnr() + + if has_key(a:tab_group, l:tabId) + for v in a:tab_group[l:tabId] + if v == l:bufnr + return + endif + endfor + call add(a:tab_group[l:tabId], l:bufnr) + else + let a:tab_group[l:tabId] = [l:bufnr] + endif + + endfunc + autocmd BufWinEnter * if &buflisted | call AddBufToTabGroup(g:tab_group) | endif + " }}} + " BufDelete {{{ + function! RemoveBufFromTabGroup(tab_group) + let l:tabId = tabpagenr() + + if has_key(a:tab_group, l:tabId) + let l:new_tab_group = {} + + for [k, tab_list] in items(a:tab_group) + let l:list = [] + for buf in tab_list + if buflisted(buf) > 0 && buf != expand('') + call add(l:list, buf) + end + endfor + if !empty(l:list) + let l:new_tab_group[k] = l:list + endif + endfor + let g:tab_group = l:new_tab_group + + endif + + endfunc + autocmd BufDelete * call RemoveBufFromTabGroup(g:tab_group) + "}}} +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 -set mouse= " Disable mouse selection -set winaltkeys=no " Allow alt key for mapping +let mapleader = "," " Always use comma as leader key +set nocompatible " Disable vi compatible, today is 20XX +set path=.,** " Allow :find with completion +set mouse= " Disable mouse selection +set winaltkeys=no " Allow alt key for mapping " Turn persistent undo on " means that you can undo even when you close a buffer/VIM @@ -43,22 +93,20 @@ set spellfile="/tmp/spell" sign define piet text=>> texthl=Search " }}} -" VISUAL ----------------{{{ +" VISUAL {{{ " colorscheme desert " Editing Area -set wrap " enable wrap by default -set scrolloff=3 " Leave some buffer when scrolling down -set showmatch " Show pairing brackets +set wrap " enable wrap by default +set scrolloff=3 " Leave some buffer when scrolling down +set showmatch " Show pairing brackets set display=lastline set lazyredraw -set formatoptions+=m " 遇到Unicode值大於255的文本,不必等到空格再折行 -set formatoptions+=B " 合併兩行中文時,不在中間加空格 set whichwrap=b,s " Side column -set signcolumn=yes number relativenumber +set number relativenumber " Cursor set cursorline @@ -67,30 +115,40 @@ set matchtime=2 " In most of the cases, it is overrides by lightline.vim set statusline=\ %F%m%r%h\ %w\ \ CWD:\ %r%{getcwd()}%h\ \ \ Line:\ %l\ \ Column:\ %c -set laststatus=2 " Always show the status line -set ruler " Show cursor position +set laststatus=2 " Always show the status line +set ruler " Show cursor position set wildmenu wildoptions=pum,fuzzy " Format of error message set errorformat+=[%f:%l]\ ->\ %m,[%f:%l]:%m +" Direction for new window +set splitright + " }}} -" EDIT ----------------{{{ +" EDIT {{{ + +" overrides ftplugin in runtimepath +" Don't wrap line when typing CJK characters +" Don't add spaces for CJK +" Don't add comment at next line +autocmd Filetype * set fo+=mB fo-=cro -set backspace=eol,start,indent " Set Backspace behaviors -set autoindent smartindent set shiftwidth=2 +set autoindent smartindent set cindent set ttimeout set timeoutlen=500 + +set backspace=eol,start,indent " Set Backspace behaviors + " set updatetime=4000 " autocmd CursorHold * normal! m' -" TAB and special Chars ----------------{{{ +" TAB and special Chars {{{ -set tabstop=8 +set tabstop=8 softtabstop=8 set expandtab -set softtabstop=-1 " Invisible chars set nolist @@ -99,23 +157,23 @@ set listchars=tab:»·,extends:>,precedes:< " }}} " }}} -" JUMP to anoterh file ----------------{{{ +" JUMP to anoterh file {{{ set isfname=@,48-57,/,.,-,_,+,,,#,$,%,~ " This affects filename recognition for gf (go to file) -set suffixesadd=.md " Enable reference markdown file without extension +set suffixesadd=.md " Enable reference markdown file without extension " }}} -" SEARCH ----------------{{{ +" 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 " Highlight all matched texts +set incsearch " Show matched strings when typing " }}} -" BUFFERS ----------------{{{ +" BUFFERS {{{ -" Go to last cursor position ----------------{{{ +" Go to last cursor position {{{ augroup vimStartup au! " When editing a file, always jump to the last known cursor position. @@ -131,21 +189,22 @@ augroup END " }}} " }}} -" FOLD ----------------{{{ +" 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" + let g:defaut_foldcolumn = "auto:3" else - let g:defaut_foldcolumn = 5 + let g:defaut_foldcolumn = 3 endif let &foldcolumn = g:defaut_foldcolumn " }}} -" ENCODING_PREFERENCE ----------------{{{ +" ENCODING_PREFERENCE {{{ if has('multi_byte') set encoding=utf-8 @@ -155,7 +214,7 @@ if has('multi_byte') endif " }}} -" BACKUP ----------------{{{ +" BACKUP {{{ " Allow backup set backup @@ -170,7 +229,7 @@ set backupdir=~/.vim/tmp set writebackup " }}} -" HIGHLIGHT ----------------{{{ +" HIGHLIGHT {{{ syntax enable set conceallevel=1 @@ -185,7 +244,7 @@ highlight ExtraWhitespace ctermbg=red guibg=red match ExtraWhitespace /\s\+$/ " }}} -" MISC ----------------{{{ +" MISC {{{ " Use Unix way to add newline set ffs=unix,dos,mac diff --git a/vim/init/config.vim b/vim/init/config.vim index 4e1c06c..4181ad3 100644 --- a/vim/init/config.vim +++ b/vim/init/config.vim @@ -3,7 +3,7 @@ " Do some autocommand for by contexts "====================================================================== -" Unnamed Buffer ----------------{{{ +" Unnamed Buffer {{{ " Automatically delete unnamed empty buffer when leaving augroup DeleteUnnamedEmptBuffer! @@ -12,7 +12,7 @@ augroup DeleteUnnamedEmptBuffer! augroup END " }}} -" Small Terminal ----------------{{{ +" Small Terminal {{{ augroup TerminalSize au! @@ -27,13 +27,13 @@ augroup TerminalSize augroup END " }}} -" X11 ----------------{{{ +" X11 {{{ " Change IM to US when exit to Normal mode autocmd InsertLeave * :silent !fcitx-remote -c &>/dev/null || true " }}} -" TMUX ----------------{{{ +" TMUX {{{ " 有 tmux 何没有的功能键超时(毫秒) if $TMUX != '' @@ -60,7 +60,7 @@ if !has('gui_running') && &term =~ '^\%(screen\|tmux\)' endif " }}} -" KeyCode ----------------{{{ +" KeyCode {{{ "---------------------------------------------------------------------- " 终端下允许 ALT,详见:http://www.skywind.me/blog/archives/2021 @@ -114,22 +114,22 @@ call s:key_escape('', '[23;2~') call s:key_escape('', '[24;2~') " }}} -" Filetype ----------------{{{ +" Filetype {{{ augroup InitFileTypes au! - " Filetype for Vim ----------------{{{ + " Filetype for Vim {{{ " Help page - autocmd BufEnter *.txt if &filetype == 'help' | wincmd T | endif + autocmd BufEnter * if &filetype == 'help' | wincmd T | set buflisted | endif " quickfix: hide line number autocmd FileType quickfix setlocal nonumber " }}} - " Shebeng: Set filetype from shebeng ----------------{{{ + " Shebeng: Set filetype from shebeng {{{ function! s:ApplyShebang() let l:filetype = matchstr(getline(1), '^#!.*[ /]\zs[[:alnum:]]\+$') let l:shebangMatch = #{ node: "javascript" } @@ -143,7 +143,7 @@ augroup InitFileTypes endfunction autocmd BufReadPost * call ApplyShebang() " }}} - " Markdown ----------------{{{ + " Markdown {{{ augroup Config_Markdown au! @@ -168,7 +168,7 @@ augroup InitFileTypes augroup END " }}} - " HTML ----------------{{{ + " HTML {{{ " Usage: cl(ass) or id to edit html tag attribute function! s:ChangeAttr(pattern) @@ -192,17 +192,17 @@ augroup InitFileTypes " Reload preview server autocmd BufWrite *.html,*.js,*.css call ReloadServer() - function ReloadServer() + function! ReloadServer() silent !browser-sync reload &>/dev/null endfunction " }}} - " Mail ----------------{{{ + " Mail {{{ autocmd BufRead /tmp/mutt-* set tw=72 " }}} - " Password ----------------{{{ + " Password {{{ " Hide the first line of a file if editing password file " TODO a better way to determine a file is related to password-store, now use @@ -218,7 +218,7 @@ augroup InitFileTypes norm! ggzfl endfunction " }}} - " Beancount ----------------{{{ + " Beancount {{{ autocmd BufRead,BufNewFile *.bean call PrepareBean() function PrepareBean() diff --git a/vim/init/keymaps.vim b/vim/init/keymaps.vim index f13756b..1eb2b20 100644 --- a/vim/init/keymaps.vim +++ b/vim/init/keymaps.vim @@ -21,15 +21,15 @@ nnoremap W :set wrap! function! s:WriteOrEnterFileName() if !empty(expand('%')) | write! | else | call feedkeys(":w ") | endif endfunction -nmap w :call WriteOrEnterFileName() +nnoremap w :call WriteOrEnterFileName() " :W sudo saves the file " (useful for handling the permission-denied error) command! W execute 'w !sudo -S tee %' edit! " Quit -nmap q :q -nmap cq :cq +nnoremap q :q +nnoremap cq :cq " Remap in Quickfix, Cmdwin Location list augroup vimrc_CRfix @@ -57,7 +57,7 @@ augroup END " execute "set =\eh" " Spell -nnoremap sp :set spell!:set spell? +nnoremap \s :set spell!:set spell? nnoremap ss ]s nnoremap S [s @@ -68,6 +68,7 @@ nnoremap 1 vnoremap Tz :!trans -t zh-TW -b vnoremap Te :!trans -t en-US -b + " }}} " WORKING_DIR ----------------{{{ @@ -82,8 +83,8 @@ nnoremap cd :cd %:p:h:pwd nnoremap cd :cd nnoremap cdg :call CdToGitRepo():pwd -noremap :cd ..:pwd -noremap :call InCaseCdToLatestDir() +nnoremap :cd ..:pwd +nnoremap :call InCaseCdToLatestDir() " Switch CWD to root git directory function! CdToGitRepo() @@ -105,17 +106,21 @@ endfunction " MOTION ----------------{{{ " j/k will move virtual lines (lines that wrap) -noremap j (v:count == 0 ? 'gj' : 'j') -noremap k (v:count == 0 ? 'gk' : 'k') +nnoremap j (v:count == 0 ? 'gj' : 'j') +nnoremap k (v:count == 0 ? 'gk' : 'k') " Quick move in a line -noremap 30h -noremap 30l +nnoremap 30h +nnoremap 30l " File under the cursor nnoremap gf nnoremap gF :e +xnoremap iq i" +xnoremap aq a" + + " READLINE_FEATURES ----------------{{{ inoremap @@ -145,22 +150,22 @@ cnoremap d$ cnoremap de " Moving with wrap -noremap gj -noremap gk +nnoremap gj +nnoremap gk inoremap gj inoremap gk " }}} " JUMP_TO_TABS_WITH_ALT ----------------{{{ -noremap :tabn 1 -noremap :tabn 2 -noremap :tabn 3 -noremap :tabn 4 -noremap :tabn 5 -noremap :tabn 6 -noremap :tabn 7 -noremap :tabn 8 -noremap :tablast +nnoremap :tabn 1 +nnoremap :tabn 2 +nnoremap :tabn 3 +nnoremap :tabn 4 +nnoremap :tabn 5 +nnoremap :tabn 6 +nnoremap :tabn 7 +nnoremap :tabn 8 +nnoremap :tablast inoremap :tabn 1 inoremap :tabn 2 inoremap :tabn 3 @@ -211,23 +216,25 @@ nnoremap ee :edit $MYVIMRC " MANAGE_BUFFERS ----------------{{{ " 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! +nnoremap so :set +nnoremap ft :e'set filetype='..&filetype +nnoremap sw :e'set shiftwidth='..&shiftwidth +nnoremap ts :e'set tabstop='..&tabstop +nnoremap \e :set expandtab!:set expandtab? +nnoremap \l :set list!:set list? +nnoremap \n :set nu!:set nu? +nnoremap \r :set relativenumber!:set rnu? " Open a new buffer -nmap B :enew -nmap O :e /tmp/buffer +nnoremap B :enew +nnoremap O :e /tmp/buffer " Let l toggle between this and the last accessed buffer augroup SaveLastBuffer let g:lastbuffer = 1 - au BufLeave * let g:lastbuffer = bufnr() + au BufLeave * if &buflisted | let g:lastbuffer = expand('') | endif augroup END -noremap l :exe "buffer ".g:lastbuffer +nnoremap l :exe "buffer ".g:lastbuffer " Use Ctrl-C for buffer delete or quit vim ----------------{{{ @@ -238,9 +245,11 @@ function! ToggleQuit() let message = g:quitVimWhenPressingCtrlC ? "Unlock" : "Lock" echo message endfunction -nnoremap gl :call ToggleQuit() +nnoremap \q :call ToggleQuit() function! CloseBufferSafely() + let l:bufnr = bufnr() + " Ask Saving if &modified let answer = confirm("Save changes?", "&Yes\n&No\n&Cancel") if answer == 1 | write | endif @@ -248,27 +257,22 @@ function! CloseBufferSafely() if answer == "" | return | endif endif - let bufs = getbufinfo({'buflisted': 1}) - if len(bufs) == 1 - bdelete! + if g:tab_group[tabpagenr()] == [l:bufnr] + bdelete else - b# | bd! # + bprevious | bd # endif endfunction func! QuitWithCheck() if g:quitVimWhenPressingCtrlC silent! quit else - echo "Press gl to allow quit with " + echo "Press \\q to allow quit with " endif endfunc function! Bye() let windows = gettabinfo(tabpagenr())[0]['windows'] - try - let bufs = gettabinfo(tabpagenr())[0]['variables']['bufs'] - catch - let bufs = getbufinfo({'buflisted': 1}) - endtry + let bufs = getbufinfo({'buflisted': 1}) if len(windows) == 1 && len(bufs) == 1 call QuitWithCheck() @@ -282,8 +286,8 @@ function! Bye() endfunction nnoremap :call Bye() - -" Diff Mode ---------------- +" }}} +" Diff Mode {{{ function! CloseBuffersForDiff() windo | if &diff && &buftype == "nofile" | bdelete | endif @@ -329,10 +333,10 @@ nnoremap sb :windo set scrollbind! " 传统的 CTRL+hjkl 移动窗口不适用于 vim 8.1 的终端模式,CTRL+hjkl 在 " bash/zsh 及带文本界面的程序中都是重要键位需要保留,不能 tnoremap 的 "---------------------------------------------------------------------- -noremap h -noremap l -noremap j -noremap k +nnoremap h +nnoremap l +nnoremap j +nnoremap k inoremap h inoremap l inoremap j @@ -365,12 +369,12 @@ map tc :tabclose map tm :tabmove map to :tabonly -noremap :call Tab_MoveLeft() -noremap :call Tab_MoveRight() +nnoremap :call Tab_MoveLeft() +nnoremap :call Tab_MoveRight() " Let tl toggle between this and the last accessed tab let g:lasttab = 1 -nmap tl :exe "tabn ".g:lasttab +nnoremap tl :exe "tabn ".g:lasttab autocmd TabLeave * let g:lasttab = tabpagenr() " Opens a new tab with the current buffer's path @@ -394,8 +398,8 @@ endfunc " FOLD ----------------{{{ " Set fold options -noremap fm :e'set foldmethod='..&foldmethod -noremap fc :e'set foldcolumn='..&foldcolumn +nnoremap fm :e'set foldmethod='..&foldmethod +nnoremap fc :e'set foldcolumn='..&foldcolumn nnoremap zi zizz @@ -463,7 +467,7 @@ endfunction " HIGHLIGHT ----------------{{{ " Disable highlight when is pressed -noremap :noh +nnoremap :noh function! HiFile() let i = 1 @@ -501,7 +505,6 @@ inoremap ( () inoremap [ [] inoremap { {} -vnoremap S sa vnoremap ' ``>la' vnoremap q ``>la" vnoremap ( ``>la) @@ -591,6 +594,8 @@ vnoremap call SubstituteBySearch() " }}} " SIGN ----------------{{{ +nnoremap sc :e'set signcolumn='..&signcolumn + nnoremap si :exe ":sign place " .. line('.') .. " line=" .. line('.') .. " name=piet file=" .. expand("%:p") nnoremap sI :exe ":sign unplace * file=" .. expand("%:p") -- cgit v1.2.3-70-g09d2