From 3526eb6d756e3c37190814eca2e22ce9f7d9f1de Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Fri, 28 Jun 2024 01:24:13 +0800 Subject: Update --- vim/init/config.vim | 174 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 99 insertions(+), 75 deletions(-) (limited to 'vim/init/config.vim') diff --git a/vim/init/config.vim b/vim/init/config.vim index 489521f..20beab2 100644 --- a/vim/init/config.vim +++ b/vim/init/config.vim @@ -10,25 +10,49 @@ " Open help page in a new tab autocmd BufEnter *.txt if &filetype == 'help' | wincmd T | endif -" Quickly edit html tag class -function! s:ChangeAttr(pattern) - - let l:attr = matchstr(getline('.'), a:pattern.'="') - if l:attr == '' - let l:all_attrs = matchstr(getline('.'), '<[[:alnum:]]\+\zs\s\?[^>]*>\ze') - execute 's/'.l:all_attrs.'/ '.a:pattern.'=""'.l:all_attrs.'/' - noh - normal! 0f"l - startinsert - else - normal! 0 - call search(l:attr) - normal! f"l - noh - startinsert +" Automatically apply filetype by shebang +function! s:ApplyShebang() + let l:filetype = matchstr(getline(1), '^#!.*[ /]\zs[[:alnum:]]\+$') + let l:shebangMatch = #{node: "javascript"} + if l:filetype != "" + if has_key(shebangMatch, l:filetype) + let l:filetype = shebangMatch[l:filetype] endif + echo "filetype from shebang: ".l:filetype + execute "set filetype=".l:filetype + endif endfunction +autocmd BufReadPost * call ApplyShebang() + +" For vimscript +if executable('vim-language-server') + echo "set vim language server" + au User lsp_setup call lsp#register_server({ + \ 'name': 'vimscript-language-server', + \ 'cmd': {server_info->['vimscript-language-server']}, + \ 'whitelist': ['vim'], + \ }) +endif +" html +" Quickly edit html tag class +function! s:ChangeAttr(pattern) + let l:attr = matchstr(getline('.'), a:pattern.'="') + if l:attr == '' + let l:all_attrs = matchstr(getline('.'), '<[[:alnum:]]\+\zs\s\?[^>]*>\ze') + execute 's/'.l:all_attrs.'/ '.a:pattern.'=""'.l:all_attrs.'/' + noh + normal! 0f"l + startinsert + else + normal! 0 + call search(l:attr) + normal! f"l + noh + startinsert + endif +endfunction +" Edit class and id for javascript files autocmd FileType html nnoremap cl :call ChangeAttr("class") autocmd BufLeave nunmap cl autocmd FileType html nnoremap id :call ChangeAttr("id") @@ -38,9 +62,9 @@ autocmd BufLeave nunmap id " 有 tmux 何没有的功能键超时(毫秒) "---------------------------------------------------------------------- if $TMUX != '' - set ttimeoutlen=30 + set ttimeoutlen=30 elseif &ttimeoutlen > 80 || &ttimeoutlen <= 0 - set ttimeoutlen=80 + set ttimeoutlen=80 endif @@ -49,22 +73,22 @@ endif " 记得设置 ttimeout (见 init-basic.vim) 和 ttimeoutlen (上面) "---------------------------------------------------------------------- if has('nvim') == 0 && has('gui_running') == 0 - function! s:metacode(key) - exec "set =\e".a:key - endfunc - for i in range(10) - call s:metacode(nr2char(char2nr('0') + i)) - endfor - for i in range(26) - call s:metacode(nr2char(char2nr('a') + i)) - call s:metacode(nr2char(char2nr('A') + i)) - endfor - for c in [',', '.', '/', ';', '{', '}'] - call s:metacode(c) - endfor - for c in ['?', ':', '-', '_', '+', '=', "'"] - call s:metacode(c) - endfor + function! s:metacode(key) + exec "set =\e".a:key + endfunc + for i in range(10) + call s:metacode(nr2char(char2nr('0') + i)) + endfor + for i in range(26) + call s:metacode(nr2char(char2nr('a') + i)) + call s:metacode(nr2char(char2nr('A') + i)) + endfor + for c in [',', '.', '/', ';', '{', '}'] + call s:metacode(c) + endfor + for c in ['?', ':', '-', '_', '+', '=', "'"] + call s:metacode(c) + endfor endif @@ -72,9 +96,9 @@ endif " 终端下功能键设置 "---------------------------------------------------------------------- function! s:key_escape(name, code) - if has('nvim') == 0 && has('gui_running') == 0 - exec "set ".a:name."=\e".a:code - endif + if has('nvim') == 0 && has('gui_running') == 0 + exec "set ".a:name."=\e".a:code + endif endfunc @@ -104,10 +128,10 @@ call s:key_escape('', '[24;2~') " Refer: http://sunaku.github.io/vim-256color-bce.html "---------------------------------------------------------------------- if &term =~ '256color' && $TMUX != '' - " disable Background Color Erase (BCE) so that color schemes - " render properly when inside 256-color tmux and GNU screen. - " see also http://snk.tuxfamily.org/log/vim-256color-bce.html - set t_ut= + " disable Background Color Erase (BCE) so that color schemes + " render properly when inside 256-color tmux and GNU screen. + " see also http://snk.tuxfamily.org/log/vim-256color-bce.html + set t_ut= endif @@ -143,26 +167,26 @@ silent! call mkdir(expand('~/.vim/tmp'), "p", 0755) " 会令一些支持 xterm 不完全的终端解析错误,显示为错误的字符,比如 q 字符 " 如果你确认你的终端支持,不会在一些不兼容的终端上运行该配置,可以注释 if has('nvim') - set guicursor= + set guicursor= elseif (!has('gui_running')) && has('terminal') && has('patch-8.0.1200') - let g:termcap_guicursor = &guicursor - let g:termcap_t_RS = &t_RS - let g:termcap_t_SH = &t_SH - set guicursor= - set t_RS= - set t_SH= + let g:termcap_guicursor = &guicursor + let g:termcap_t_RS = &t_RS + let g:termcap_t_SH = &t_SH + set guicursor= + set t_RS= + set t_SH= endif " 打开文件时恢复上一次光标所在位置 autocmd BufReadPost * - \ if line("'\"") > 1 && line("'\"") <= line("$") | - \ exe "normal! g`\"" | - \ endif + \ if line("'\"") > 1 && line("'\"") <= line("$") | + \ exe "normal! g`\"" | + \ endif " 定义一个 DiffOrig 命令用于查看文件改动 if !exists(":DiffOrig") command DiffOrig vert new | set bt=nofile | r ++edit # | 0d_ | diffthis - \ | wincmd p | diffthis + \ | wincmd p | diffthis endif @@ -172,16 +196,16 @@ endif "---------------------------------------------------------------------- augroup InitFileTypesGroup - " 清除同组的历史 autocommand - au! + " 清除同组的历史 autocommand + au! - " C/C++ 文件使用 // 作为注释 - au FileType c,cpp setlocal commentstring=//\ %s + " C/C++ 文件使用 // 作为注释 + au FileType c,cpp setlocal commentstring=//\ %s - " markdown 允许自动换行 - au FileType markdown setlocal wrap - au FileType markdown set sw=2 - au FileType markdown set ts=2 + " markdown 允许自动换行 + au FileType markdown setlocal wrap + au FileType markdown set sw=2 + au FileType markdown set ts=2 " Fold markdown by heading level function MarkdownLevel() @@ -195,24 +219,24 @@ augroup InitFileTypesGroup au FileType markdown setlocal foldexpr=MarkdownLevel() au FileType markdown setlocal foldmethod=expr - " lisp 进行微调 - au FileType lisp setlocal ts=8 sts=2 sw=2 et + " lisp 进行微调 + au FileType lisp setlocal ts=8 sts=2 sw=2 et - " scala 微调 - au FileType scala setlocal sts=4 sw=4 noet + " scala 微调 + au FileType scala setlocal sts=4 sw=4 noet - " haskell 进行微调 - au FileType haskell setlocal et + " haskell 进行微调 + au FileType haskell setlocal et - " quickfix 隐藏行号 - au FileType qf setlocal nonumber + " quickfix 隐藏行号 + au FileType qf setlocal nonumber - " 强制对某些扩展名的 filetype 进行纠正 - au BufNewFile,BufRead *.as setlocal filetype=actionscript - au BufNewFile,BufRead *.pro setlocal filetype=prolog - au BufNewFile,BufRead *.es setlocal filetype=erlang - au BufNewFile,BufRead *.asc setlocal filetype=asciidoc - au BufNewFile,BufRead *.vl setlocal filetype=verilog - au BufRead /tmp/mutt-* set tw=72 + " 强制对某些扩展名的 filetype 进行纠正 + au BufNewFile,BufRead *.as setlocal filetype=actionscript + au BufNewFile,BufRead *.pro setlocal filetype=prolog + au BufNewFile,BufRead *.es setlocal filetype=erlang + au BufNewFile,BufRead *.asc setlocal filetype=asciidoc + au BufNewFile,BufRead *.vl setlocal filetype=verilog + au BufRead /tmp/mutt-* set tw=72 augroup END -- cgit v1.2.3-70-g09d2