From b57c461014f585655d897abf0e0ac982807396b7 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Mon, 5 Aug 2024 18:41:51 +0800 Subject: Update --- vim/init/basic.vim | 35 ++++++++++++++++++-------- vim/init/config.vim | 24 +++++++++++------- vim/init/keymaps.vim | 71 ++++++++++++++++++++++++++-------------------------- 3 files changed, 75 insertions(+), 55 deletions(-) (limited to 'vim/init') diff --git a/vim/init/basic.vim b/vim/init/basic.vim index c4dfa61..fd20476 100644 --- a/vim/init/basic.vim +++ b/vim/init/basic.vim @@ -4,6 +4,17 @@ " Used for general usecases. No keymap and personal preference "====================================================================== +" Initial for terminal emulator {{{ +augroup Enter + au! + function! SetEmulaterBackground() + redir => output | hi Normal | redir END + let bg_color = matchstr(output, 'guibg=\zs[^\s]\+\ze') + exe "!alacritty msg config 'colors.primary.background=\"\\"..bg_color.."\"'" + endfunc + autocmd VimEnter * call SetEmulaterBackground() +augroup END +"}}} " For Vimscript {{{ " Usage: type --- for foldmark @@ -27,7 +38,9 @@ augroup tabinfo endfunc function! RemoveBufFromTabs() for tab in gettabinfo() - call filter(tab.variables.bufs, "v:val != "..expand('')) + if has_key(t:, 'bufs') + call filter(tab.variables.bufs, "v:val != "..expand('')) + endif endfor endfunc @@ -78,7 +91,7 @@ set showtabline=2 set number relativenumber " Cursor -set cursorline +" set cursorline set guicursor=n-v-c-sm:block,i-ci-ve:ver25,r-cr-o:hor20 set matchtime=2 @@ -95,7 +108,7 @@ set errorformat+=[%f:%l]\ ->\ %m,[%f:%l]:%m set splitright " Set signcolumn -set signcolumn=auto:3 +set signcolumn=yes:3 " Custom sign from help page :h sign sign define piet text=>> texthl=Search @@ -107,7 +120,7 @@ sign define piet text=>> texthl=Search " 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 +autocmd Filetype * set formatoptions+=mB formatoptions-=cro set shiftwidth=2 set autoindent smartindent @@ -140,10 +153,9 @@ set suffixesadd=.md " Enable reference markdown file without " }}} " SEARCH {{{ -set ignorecase " Search case without case sensation -set smartcase -set hlsearch " Highlight all matched texts -set incsearch " Show matched strings when typing +set ignorecase smartcase " Search case without case sensation +set hlsearch " Highlight all matched texts +set incsearch " Show matched strings when typing " }}} " BUFFERS {{{ @@ -209,16 +221,17 @@ set writebackup " HIGHLIGHT {{{ syntax enable -set conceallevel=1 +set syntax=filetype +set conceallevel=2 " 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 -highlight LuaParen ctermfg=NONE ctermbg=darkgrey cterm=NONE +hi LuaParen ctermfg=NONE ctermbg=darkgrey cterm=NONE " Show trailing spaces -highlight ExtraWhitespace ctermbg=red guibg=red match ExtraWhitespace /\s\+$/ +hi ExtraWhitespace ctermbg=red guibg=red " }}} " MISC {{{ diff --git a/vim/init/config.vim b/vim/init/config.vim index 318bc58..3870ac4 100644 --- a/vim/init/config.vim +++ b/vim/init/config.vim @@ -20,7 +20,7 @@ augroup TerminalSize if &lines < a:bound || g:alacritty_extra_padding silent! set laststatus=0 showtabline=0 signcolumn=0 nowrap scrolloff=1 else - silent! set laststatus& showtabline& signcolumn& scrolloff& + silent! set laststatus& showtabline=2 signcolumn& scrolloff& endif endfunc autocmd VimEnter,VimResized * silent call LayoutForSmallTerminal(20) @@ -147,17 +147,23 @@ augroup InitFileTypes augroup Config_Markdown au! - au FileType markdown call InitMarkdown() - au FileType markdown let b:in_frontmatter = 0 - function! InitMarkdown() + au FileType markdown call InitMarkdownFile() + function! InitMarkdownFile() setlocal wrap sw=2 ts=2 + + let b:in_frontmatter = 0 setlocal foldexpr=MarkdownLevel() foldmethod=expr setlocal foldtext=MarkdownFoldTextHeading() - syn match Details '^
' conceal cchar=▶ - syn match Summary '' conceal cchar= - syn match SummaryEnd '' conceal - syn match DetailsEnd '^
' conceal cchar=E + + call MarkdownHighlights() + endfunc + + function MarkdownHighlights() + syn match MarkdownHtmlDetails '^
' conceal cchar=▶ + syn match MarkdownHtmlSummary '' conceal cchar= + syn match MarkdownHtmlSummaryEnd '' conceal + syn match MarkdownHtmlDetailsEnd '^
' conceal cchar=E endfunc function! MarkdownLevel() @@ -196,7 +202,7 @@ augroup InitFileTypes let origin = split(MarkdownFoldText()[2:], ' ') let heading = substitute(join(origin[:-3], ' '), '\#', ' ', 'g') let lines = join(origin[-2:], ' ')[1:-2] - let fills = repeat('.', 48 - len(heading) - len(lines)) + let fills = repeat('.', 48 - strwidth(heading) - len(lines)) return heading.." "..fills.." "..lines endfunc diff --git a/vim/init/keymaps.vim b/vim/init/keymaps.vim index a423c20..e8cb61d 100644 --- a/vim/init/keymaps.vim +++ b/vim/init/keymaps.vim @@ -3,7 +3,7 @@ "====================================================================== " vim: sw=2 ts=2 foldmethod=marker foldmarker={{{,}}} -" COMMON_MAPPING {{{ +" COMMON {{{ " Space for searching map / @@ -37,9 +37,7 @@ augroup vimrc_CRfix augroup END " Spell -nnoremap \s :set spell!:set spell? -nnoremap ss ]s -nnoremap S [s +nnoremap \sp :set spell!:set spell? " Show full path by default nnoremap 1 @@ -48,6 +46,8 @@ nnoremap 1 vnoremap Tz :!trans -t zh-TW -b vnoremap Te :!trans -t en-US -b +nnoremap q: : + " }}} " WORKING_DIR {{{ @@ -157,6 +157,16 @@ inoremap :tablast " }}} +" }}} +" EDIT {{{ + +" Move one line up and down +nnoremap ddp +nnoremap ddkP + +" Clear current line +nnoremap S S + " }}} " REGISTER {{{ " Paste register 0 @@ -176,7 +186,7 @@ vnoremap Y "+y function! DeleteMark(mark) let mark = nr2char(a:mark) echo mark - if mark =~ '[a-Z]' + if mark =~ '\a' execute "delmarks " . mark endif endfunc @@ -259,7 +269,7 @@ function! ChangeUnfold(downward, count) let foldend -= move endif norm! zd - exe foldstart..","..foldend.."fold" + try | silent! exe foldstart..","..foldend.."fold" | endtry " Get back to origin cursor position norm! '' @@ -268,16 +278,6 @@ nnoremap z> ":\call ChangeUnfold(1,"..v:count..")\" nnoremap z< ":\call ChangeUnfold(0,"..v:count..")\" "}}} -" EDIT {{{ - -" Move one line up and down -nnoremap ddp -nnoremap ddkP - -" Clear current line -nnoremap S S - -" }}} " TERMINAL {{{ " Use z to toggle @@ -285,13 +285,17 @@ let g:alacritty_extra_padding = 0 function! ToggleWinPadding() if g:alacritty_extra_padding !alacritty msg config --window-id $WINDOWID --reset + call SetEmulaterBackground() hi EndOfBuffer None hi MsgArea None else redir => output | hi LineNr | redir END let bg_color = matchstr(output, 'guibg=\zs[^\s]\+\ze') - exe "hi EndOfBuffer guifg="..bg_color.." guibg="..bg_color - exe "hi MsgArea guibg="..bg_color + + try + exe "hi EndOfBuffer guifg="..bg_color.." guibg="..bg_color + exe "hi MsgArea guibg="..bg_color + endtry exe "!alacritty msg config --window-id $WINDOWID window.padding.x=300 'colors.primary.background=\"\\"..bg_color.."\"'" endif @@ -380,24 +384,16 @@ function! CloseBufferSafely() if answer == "" | return | endif endif - let l:bufnr = bufnr() - + let bufnr = bufnr() if len(t:bufs) == 1 " Close tab for last buffer tabclose else " Switch to proper buffer - let l:next_buf = get(t:bufs, bufnr('#')) ? bufnr('#') : filter(t:bufs, 'v:val != '..l:bufnr)[0] - exe "b "..l:next_buf - call filter(t:bufs, 'v:val != '..l:bufnr) + let next_buf = get(t:bufs, bufnr('#')) ? bufnr('#') : filter(t:bufs, 'v:val != '..bufnr)[0] + exe "b "..next_buf + call filter(t:bufs, 'v:val != '..bufnr) endif - - " Delete buffer if every t:buf doesn't have it - for tab in gettabinfo() - if get(tab.variables.bufs, l:bufnr) | return | endif - endfor - exe "bd! "..l:bufnr - endfunction function! Bye() let windows = gettabinfo(tabpagenr())[0]['windows'] @@ -493,10 +489,10 @@ endif " MANAGE_TABS {{{ " Useful mappings for managing tabs -map tn :tabnew -map tc :tabclose -map tm :tabmove -map to :tabonly +nnoremap tn :tabnew +nnoremap tc :tabclose +nnoremap tm :tabmove +nnoremap to :tabonly nnoremap :call Tab_MoveLeft() nnoremap :call Tab_MoveRight() @@ -508,7 +504,7 @@ 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") +nnoremap te :tabedit =expand("%:p:h") " Tab move functions function! Tvab_MoveLeft() @@ -598,6 +594,11 @@ endfunction " }}} " HIGHLIGHT {{{ +nnoremap \sy exists("g:syntax_on") ? ":syntax off " : ":syntax enable" + +" Toggle conceallevel0/2 +nnoremap \c ":set conceallevel="..(&cole ? 0 : 2)..":set cole?" + " Disable highlight when is pressed nnoremap :noh -- cgit v1.2.3-70-g09d2