From d834d6da82ba20e32699380d335a65936bad16f9 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Mon, 8 Jul 2024 22:49:59 +0800 Subject: Update --- vim/init/basic.vim | 195 ++++++++++++++++----------------------------------- vim/init/config.vim | 30 +++++++- vim/init/keymaps.vim | 181 ++++++++++++++++------------------------------- 3 files changed, 152 insertions(+), 254 deletions(-) (limited to 'vim/init') diff --git a/vim/init/basic.vim b/vim/init/basic.vim index 2867f69..b268c6b 100644 --- a/vim/init/basic.vim +++ b/vim/init/basic.vim @@ -1,36 +1,24 @@ "====================================================================== -" " init-basic.vim - Need vim tiny compatible " " Used for general usecases. No keymap and personal preference -" -" Use '*' to search for: -" VIM_BEHAVIOR -" VISUAL -" EDIT -" JUMP -" SEARCH -" BUFFERS -" TABSIZE -" ENCODING_PREFERENCE -" FOLDING -" BACKUP -" MISC "====================================================================== +" Vimscript file settings ---------------------- {{{ -"---------------------------------------------------------------------- -" VIM_BEHAVIOR -"---------------------------------------------------------------------- - +" Usage: type --- for foldmark +augroup filetype_vim + autocmd! + execute "autocmd FileType vim :inoreabbrev --- ----------------{".."{{" + autocmd FileType vim setlocal foldmethod=marker foldlevel=0 +augroup END +" }}} +" VIM_BEHAVIOR ----------------{{{ 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 -set cursorline -set whichwrap=b,s -" set autochdir " Automatically cd to current file " Turn persistent undo on " means that you can undo even when you close a buffer/VIM @@ -41,28 +29,44 @@ set conceallevel=1 " Apply plugin and indent by filetype filetype plugin indent on - -"---------------------------------------------------------------------- -" VISUAL -"---------------------------------------------------------------------- +" Set to auto read when a file is changed from the outside +" Unnamed buffer like CmdWindows should prevent this +set autoread +autocmd FocusGained,BufEnter .* checktime +" }}} +" VISUAL ----------------{{{ " colorscheme desert " I like desert! -" 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 showmatch " Show pairing brackets - +" Editing Area set number relativenumber " Use relativenumber -set wrap " Disable wrap by default +set wrap " enable wrap by default set scrolloff=3 " Leave some buffer when scrolling down -set ruler " Show cursor position -set laststatus=2 " Always show the status line +set showmatch " Show pairing brackets +set display=lastline +set lazyredraw +set formatoptions+=m " 遇到Unicode值大於255的文本,不必等到空格再折行 +set formatoptions+=B " 合併兩行中文時,不在中間加空格 +set whichwrap=b,s + +" Cursor +set cursorline set guicursor=n-v-c-sm:block,i-ci-ve:ver25,r-cr-o:hor20 +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 wildmenu wildoptions=pum,fuzzy +" Format of error message +set errorformat+=[%f:%l]\ ->\ %m,[%f:%l]:%m -"---------------------------------------------------------------------- -" EDIT -"---------------------------------------------------------------------- +" 顯示分隔符號 +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 @@ -76,32 +80,21 @@ imap l " Change IM to US when exit to Normal mode autocmd InsertLeave * :silent !fcitx-remote -c &>/dev/null || true - -"---------------------------------------------------------------------- -" JUMP -"---------------------------------------------------------------------- +" }}} +" 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 - -"---------------------------------------------------------------------- -" SEARCH -"---------------------------------------------------------------------- -set ignorecase " Search case without case sensation +" }}} +" SEARCH ----------------{{{ +set ignorecase " Search case without case sensation set smartcase set hlsearch " Hilight all matched texts set incsearch " Show matched strings when typing - -"---------------------------------------------------------------------- -" BUFFERS -"---------------------------------------------------------------------- - -" Set to auto read when a file is changed from the outside -" Unnamed buffer like CmdWindows should prevent this -set autoread -autocmd FocusGained,BufEnter .* checktime - +" }}} +" BUFFERS ----------------{{{ +" Use to quit the last buffer ----------------{{{ let g:quitVimWhenPressingCtrlC = 1 function! ToggleQuit() let g:quitVimWhenPressingCtrlC = g:quitVimWhenPressingCtrlC ? 0 : 1 @@ -111,8 +104,6 @@ endfunction nnoremap gl :call ToggleQuit() -" Simply exit when closing the last buffer - function! Bye() if len(getbufinfo({'buflisted': 1})) == 1 && len(getwininfo()) == 1 if g:quitVimWhenPressingCtrlC @@ -128,16 +119,15 @@ endfunction " Ctrl-C rules!!! nnoremap :call Bye() +" }}} + " Don't unload a buffer when no longer shown in a window " This allows you open a new buffer and leaves current buffer modified set hidden - -" Put these in an autocmd group, so that you can revert them with: " ":augroup vimStartup | au! | augroup END" augroup vimStartup au! - " When editing a file, always jump to the last known cursor position. " Don't do it when the position is invalid, when inside an event handler " (happens when dropping a file on gvim) and for a commit message @@ -148,38 +138,9 @@ augroup vimStartup \ | endif augroup END -" Set filetype for beancount -autocmd BufRead,BufNewFile *.bean call PrepareBean() -function PrepareBean() - set filetype=beancount - silent !setsid fava ~/bean/main.bean &>/dev/null - autocmd VimLeave * silent !killall fava -endfunction - -" Set filetype for index.html -autocmd BufWrite *.html,*.js,*.css call ReloadServer() -function ReloadServer() - silent !browser-sync reload &>/dev/null -endfunction - -" 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 -" files under /dev/shm as filter -autocmd BufRead /dev/shm/*.txt call SetPasswordFile() -function SetPasswordFile() - setlocal foldminlines=0 - setlocal foldmethod=manual - function s:custom() - return "Password" - endfunction - setlocal foldtext=s:custom() - norm! ggzfl -endfunction +" }}} +" TABSIZE ----------------{{{ - -"---------------------------------------------------------------------- -" TABSIZE -"---------------------------------------------------------------------- set expandtab set shiftwidth=2 set autoindent @@ -187,10 +148,9 @@ set tabstop=4 set softtabstop=0 set smartindent +" }}} +" ENCODING_PREFERENCE ----------------{{{ -"---------------------------------------------------------------------- -" ENCODING_PREFERENCE -"---------------------------------------------------------------------- if has('multi_byte') set encoding=utf-8 set fileencoding=utf-8 @@ -198,18 +158,13 @@ if has('multi_byte') set fileencodings=utf-8,big5,ucs-bom,gbk,gb18030,euc-jp,latin1 endif - -"---------------------------------------------------------------------- -" FOLDING -"---------------------------------------------------------------------- +" }}} +" FOLDING ----------------{{{ set foldenable " Allow fold set foldmethod=indent " Fold contents by indent -set foldlevel=2 " Expand all by default - - -"---------------------------------------------------------------------- -" BACKUP -"---------------------------------------------------------------------- +set foldlevel=2 +" }}} +" BACKUP ----------------{{{ " Allow backup set backup @@ -222,42 +177,13 @@ set backupdir=~/.vim/tmp " backup when write file set writebackup - -"---------------------------------------------------------------------- -" MISC -"---------------------------------------------------------------------- - -" 顯示括號匹配的時間 -set matchtime=2 - -" 顯示最後一行 -set display=lastline - -" 允許下方顯示目錄 -set wildmenu wildoptions=pum,fuzzy - -" Improve performance -set lazyredraw - -" Format of error message -set errorformat+=[%f:%l]\ ->\ %m,[%f:%l]:%m - -" 顯示分隔符號 -set listchars=tab:\|\ ,trail:.,extends:>,precedes:< - -" 遇到Unicode值大於255的文本,不必等到空格再折行 -set formatoptions+=m - -" 合併兩行中文時,不在中間加空格 -set formatoptions+=B +" }}} +" MISC ----------------{{{ " Use Unix way to add newline set ffs=unix,dos,mac - -"---------------------------------------------------------------------- " Ignore these suffixes when find/complete -"---------------------------------------------------------------------- set suffixes=.bak,~,.o,.h,.info,.swp,.obj,.pyc,.pyo,.egg-info,.class set wildignore=*.o,*.obj,*~,*.exe,*.a,*.pdb,*.lib "stuff to ignore when tab completing @@ -276,3 +202,4 @@ set wildignore+=*.ppt,*.pptx,*.docx,*.xlt,*.xls,*.xlsx,*.odt,*.wps set wildignore+=*.msi,*.crx,*.deb,*.vfd,*.apk,*.ipa,*.bin,*.msu set wildignore+=*.gba,*.sfc,*.078,*.nds,*.smd,*.smc set wildignore+=*.linux2,*.win32,*.darwin,*.freebsd,*.linux,*.android +" }}} diff --git a/vim/init/config.vim b/vim/init/config.vim index 2d37b66..ec9f090 100644 --- a/vim/init/config.vim +++ b/vim/init/config.vim @@ -184,8 +184,36 @@ augroup InitFileTypesGroup au FileType markdown setlocal foldexpr=MarkdownLevel() au FileType markdown setlocal foldmethod=expr + " 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 + " files under /dev/shm as filter + autocmd BufRead /dev/shm/*.txt call SetPasswordFile() + function SetPasswordFile() + setlocal foldminlines=0 + setlocal foldmethod=manual + function s:custom() + return "Password" + endfunction + setlocal foldtext=s:custom() + norm! ggzfl + endfunction + + " Set filetype for beancount + autocmd BufRead,BufNewFile *.bean call PrepareBean() + function PrepareBean() + set filetype=beancount + silent !setsid fava ~/bean/main.bean &>/dev/null + autocmd VimLeave * silent !killall fava + endfunction + + " Set filetype for index.html + autocmd BufWrite *.html,*.js,*.css call ReloadServer() + function ReloadServer() + silent !browser-sync reload &>/dev/null + endfunction + " quickfix: hide line number - au FileType qf setlocal nonumber + au FileType quickfix setlocal nonumber " 强制对某些扩展名的 filetype 进行纠正 au BufNewFile,BufRead *.as setlocal filetype=actionscript diff --git a/vim/init/keymaps.vim b/vim/init/keymaps.vim index 3360a53..f7098a6 100644 --- a/vim/init/keymaps.vim +++ b/vim/init/keymaps.vim @@ -2,28 +2,13 @@ " " Only for key mapping " -" COMMON_MAPPING -" MANAGE_VIMRC -" MOVING_WITH_READLINE -" INSERT_SURROUNDING -" JUMP_TO_TABS_WITH_ALT -" MANAGE_TABS -" MANAGE_BUFFERS -" MANAGE_WINDOWS -" FOLDING -" SURROURD_WITH_CHAR -" REDIRECTION_WITH_BUFFER -" QUICK_SUBSTITUTE -" GIT_TIG " 终端支持 " 编译运行 " "====================================================================== " vim: set ts=4 sw=4 tw=78 noet : -"---------------------------------------------------------------------- -" COMMON_MAPPING -"---------------------------------------------------------------------- +" COMMON_MAPPING ----------------{{{ " Space for searching map / @@ -135,18 +120,13 @@ nnoremap so V:so nnoremap so :source ~/.vimrc vnoremap so :source - -"---------------------------------------------------------------------- -" MANAGE_VIMRC -"---------------------------------------------------------------------- +" }}} +" MANAGE_VIMRC ----------------{{{ nnoremap e :scriptnames nnoremap ee :edit $MYVIMRC autocmd! BUFWRITEPOST $MYVIMRC source $MYVIMRC - - -"---------------------------------------------------------------------- -" MOVING_WITH_READLINE -"---------------------------------------------------------------------- +" }}} +" MOVING_WITH_READLINE ----------------{{{ inoremap inoremap inoremap 0 @@ -178,19 +158,15 @@ noremap gj noremap gk inoremap gj inoremap gk - -"---------------------------------------------------------------------- -" INSERT_SURROUNDING -"---------------------------------------------------------------------- +" }}} +" INSERT_SURROUNDING ----------------{{{ inoremap ' '' inoremap " "" inoremap ( () inoremap [ [] inoremap { {} - -"---------------------------------------------------------------------- -" JUMP_TO_TABS_WITH_ALT -"---------------------------------------------------------------------- +" }}} +" JUMP_TO_TABS_WITH_ALT ----------------{{{ noremap :tabn 1 noremap :tabn 2 noremap :tabn 3 @@ -209,11 +185,8 @@ inoremap :tabn 6 inoremap :tabn 7 inoremap :tabn 8 inoremap :tablast - - -"---------------------------------------------------------------------- -" MANAGE_TABS -"---------------------------------------------------------------------- +" }}} +" MANAGE_TABS ----------------{{{ " Useful mappings for managing tabs map tn :tabnew @@ -246,11 +219,8 @@ function! Tab_MoveRight() exec 'tabmove '.l:tabnr endif endfunc - - -"---------------------------------------------------------------------- -" MANAGE_BUFFERS -"---------------------------------------------------------------------- +" }}} +" MANAGE_BUFFERS ----------------{{{ " Open a new buffer nmap b :enew @@ -294,18 +264,43 @@ function! s:SwitchDiffForGitHEAD() endfunction com! SwitchDiffForGitHEAD call s:SwitchDiffForGitHEAD() nnoremap D silent! SwitchDiffForGitHEAD - - -"---------------------------------------------------------------------- -" MANAGE_WINDOWS -"---------------------------------------------------------------------- +" }}} +" MANAGE_WINDOWS ----------------{{{ nnoremap sb :windo set scrollbind! - -"---------------------------------------------------------------------- -" FOLDING +" 窗口切换:ALT+SHIFT+hjkl +" 传统的 CTRL+hjkl 移动窗口不适用于 vim 8.1 的终端模式,CTRL+hjkl 在 +" bash/zsh 及带文本界面的程序中都是重要键位需要保留,不能 tnoremap 的 "---------------------------------------------------------------------- +noremap h +noremap l +noremap j +noremap k +inoremap h +inoremap l +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 +elseif has('nvim') + " neovim 没有 termwinkey 支持,必须把 terminal 切换回 normal 模式 + tnoremap h + tnoremap l + tnoremap j + tnoremap k + tnoremap +endif +" }}} +" FOLDING ----------------{{{ " Set foldmethod noremap fm :e'set foldmethod='..&foldmethod @@ -317,11 +312,8 @@ nnoremap l foldclosed('.') == -1 ? 'l' : 'zo' " Open fold in next line nnoremap zo foldclosed('.') == -1 ? 'zjzo' : 'zo' nnoremap zO foldclosed('.') == -1 ? 'zjzO' : 'zO' - - -"---------------------------------------------------------------------- -" SURROURD_WITH_CHAR -"---------------------------------------------------------------------- +" }}} +" SURROURD_WITH_CHAR ----------------{{{ vnoremap S sa vnoremap ' ``>la' vnoremap q ``>la" @@ -331,11 +323,9 @@ vnoremap { ``>la} vnoremap ` ``>la` vnoremap ``>la vnoremap Q ``>la」 +" }}} +" REDIRECTION_WITH_BUFFER ----------------{{{ - -"---------------------------------------------------------------------- -" REDIRECTION_WITH_BUFFER -"---------------------------------------------------------------------- " Usage: " :Redir hi ............. show the full output of command ':hi' in a scratch window " :Redir !ls -al ........ show the full output of command ':!ls -al' in a scratch window @@ -362,12 +352,10 @@ endfunction command! -nargs=1 -complete=command Redir silent call Redir() nnoremap rr :Redir +" }}} +" QUICK_SUBSTITUTE ----------------{{{ - -"---------------------------------------------------------------------- -" QUICK_SUBSTITUTE -" Press n times for area, and for substitute -"---------------------------------------------------------------------- +" Usage: Press n times for area, and for substitute " substitute across file vnoremap s y:%s//0/g @@ -395,21 +383,16 @@ vnoremap call ExpandSelectionBySearch('/') vnoremap call ExpandSelectionBySearch('?') vnoremap call SubstituteBySearch() - -"---------------------------------------------------------------------- -" GIT_TIG -"---------------------------------------------------------------------- +" }}} +" GIT_TIG ----------------{{{ let g:tig_explorer_keymap_commit_split = '' let g:tig_explorer_keymap_commit_vsplit = '' nnoremap Tig nnoremap s TigStatus nnoremap b TigBlame nnoremap d :vertical TigOpenFileWithCommit + % 0 - - -"---------------------------------------------------------------------- -" Markdown items (temproray solution) -"---------------------------------------------------------------------- +" }}} +" Markdown items (temproray solution) ----------------{{{ " Toggle list item in markdown: "- [ ] XXX" -> "XXX" -> "- XXX" -> "- [ ] XXX" " autocmd FileType markdown nnoremap i V:!sed -E '/^ *- \[.\]/ { s/^( *)- \[.\] */\1/; q; }; /^ *[^[:space:]-]/ { s/^( *)/\1- /; q; }; /^ *- / { s/^( *)- /\1- [ ] /; q; }' @@ -417,53 +400,14 @@ nnoremap d :vertical TigOpenFileWithCommit + % 0 " Toggle task status: "- [ ] " -> "- [x]" -> "- [.] " -> "- [ ] " " nnoremap x V:!sed -E '/^ *- \[ \]/ { s/^( *)- \[ \]/\1- [x]/; q; }; /^ *- \[\x\]/ { s/^( *)- \[\x\]/\1- [.]/; q; }; /^ *- \[\.\]/ { s/^( *)- \[\.\]/\1- [ ]/; q; }' - - -"---------------------------------------------------------------------- -" Common command -"---------------------------------------------------------------------- +" }}} +" Common system command ----------------{{{ " Show date selector nnoremap dd :r !sh -c 'LANG=en zenity --calendar --date-format="\%Y.\%m.\%d" 2>/dev/null' nnoremap dD :r !sh -c 'LANG=en zenity --calendar --date-format="\%a \%b \%d" 2>/dev/null' nnoremap dt :r !date +\%H:\%mA - - -"---------------------------------------------------------------------- -" 窗口切换:ALT+SHIFT+hjkl -" 传统的 CTRL+hjkl 移动窗口不适用于 vim 8.1 的终端模式,CTRL+hjkl 在 -" bash/zsh 及带文本界面的程序中都是重要键位需要保留,不能 tnoremap 的 -"---------------------------------------------------------------------- -noremap h -noremap l -noremap j -noremap k -inoremap h -inoremap l -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 -elseif has('nvim') - " neovim 没有 termwinkey 支持,必须把 terminal 切换回 normal 模式 - tnoremap h - tnoremap l - tnoremap j - tnoremap k - tnoremap -endif - - - -"---------------------------------------------------------------------- +" }}} +" Compile ----------------{{{ " 编译运行 C/C++ 项目 " 详细见:http://www.skywind.me/blog/archives/2084 "---------------------------------------------------------------------- @@ -501,7 +445,6 @@ if has('win32') || has('win64') endif -"---------------------------------------------------------------------- " F5 运行当前文件:根据文件类型判断方法,并且输出到 quickfix 窗口 "---------------------------------------------------------------------- function! ExecuteFile() @@ -548,7 +491,6 @@ endfunc -"---------------------------------------------------------------------- " F2 在项目目录下 Grep 光标下单词,默认 C/C++/Py/Js ,扩展名自己扩充 " 支持 rg/grep/findstr ,其他类型可以自己扩充 " 不是在当前目录 grep,而是会去到当前文件所属的项目目录 project root @@ -569,3 +511,4 @@ else \ --include='*.js' --include='*.vim' \ '' endif +" }}} -- cgit v1.2.3-70-g09d2