vim启动速度优化小结(基于vim9script)
源码编译
./configure --prefix=$HOME/.usr/local/ --without-x --enable-gui=no
测试版本
VIM - Vi IMproved 9.1 (2024 Jan 02, compiled Jan 7 2025 14:30:02)
Included patches: 1-993
测试脚本
#!/usr/bin/env bash
cnt=0
> startup.txt
while true
do
vim --not-a-term --startuptime startup.txt -c q
cnt=$((cnt+1))
if [[ "$cnt" -gt "$1" ]];then
break
fi
done
total=$(awk '/editing files in windows/ {sum += $1} END {print sum}' startup.txt)
printf "%.3f ms\n" $(echo "$total/$cnt" | bc -l)
声明
个人优化方面基于vim9script,且需要牺牲一定实用性,不过也有所收获
优化之前
- 使用测试脚本测试1000次
sh test.sh 1000
21.504 ms
- 最后一次启动信息
000.004 000.004: --- VIM STARTING ---
000.076 000.072: Allocated generic buffers
000.148 000.072: locale set
000.151 000.003: window checked
000.491 000.340: inits 1
000.510 000.019: parsing arguments
000.511 000.001: expanding arguments
000.536 000.025: shell init
000.769 000.233: Termcap init
000.778 000.009: inits 2
000.889 000.111: init highlight
009.720 008.646 008.646: sourcing /home/rothwell/.usr/local/share/vim/vim91/filetype.vim
009.910 000.059 000.059: sourcing /home/rothwell/.usr/local/share/vim/vim91/ftplugin.vim
009.983 000.034 000.034: sourcing /home/rothwell/.usr/local/share/vim/vim91/indent.vim
010.168 009.132 000.393: sourcing /home/rothwell/.vim/vimrc/option.vim
012.368 000.755 000.755: sourcing /home/rothwell/.usr/local/share/vim/vim91/colors/lists/default.vim
012.858 000.409 000.409: sourcing /home/rothwell/.vim/pack/vimpack/opt/color.vim/colors/gruvbox.vim
012.938 001.426 000.262: sourcing /home/rothwell/.usr/local/share/vim/vim91/syntax/synload.vim
013.013 001.600 000.174: sourcing /home/rothwell/.usr/local/share/vim/vim91/syntax/syntax.vim
013.174 000.078 000.078: sourcing /home/rothwell/.usr/local/share/vim/vim91/ftplugin/man.vim
013.486 003.294 001.616: sourcing /home/rothwell/.vim/vimrc/plugin.vim
013.861 000.354 000.354: sourcing /home/rothwell/.vim/vimrc/misc.vim
014.531 000.645 000.645: sourcing /home/rothwell/.vim/vimrc/keymap.vim
014.536 013.537 000.112: sourcing $HOME/.vimrc
014.538 000.112: sourcing vimrc file(s)
014.820 000.147 000.147: sourcing /home/rothwell/.vim/pack/vimpack/opt/matchparen.vim/plugin/matchparen.vim
014.954 000.072 000.072: sourcing /home/rothwell/.vim/pack/vimpack/opt/fzy.vim/plugin/fzf.vim
015.049 000.045 000.045: sourcing /home/rothwell/.vim/pack/vimpack/opt/floaterm.vim/plugin/floaterm.vim
015.152 000.056 000.056: sourcing /home/rothwell/.vim/pack/vimpack/opt/git.vim/plugin/git.vim
015.387 000.165 000.165: sourcing /home/rothwell/.vim/pack/vimpack/opt/sign.vim/plugin/sign.vim
015.562 000.122 000.122: sourcing /home/rothwell/.vim/pack/vimpack/opt/mark.vim/plugin/mark.vim
015.676 000.057 000.057: sourcing /home/rothwell/.vim/pack/vimpack/opt/j.vim/plugin/j.vim
015.883 000.141 000.141: sourcing /home/rothwell/.vim/pack/vimpack/opt/auto-pairs.vim/plugin/auto-pairs.vim
015.987 000.043 000.043: sourcing /home/rothwell/.vim/pack/vimpack/opt/line.vim/plugin/line.vim
016.319 000.077 000.077: sourcing /home/rothwell/.usr/local/share/vim/vim91/plugin/getscriptPlugin.vim
016.538 000.200 000.200: sourcing /home/rothwell/.usr/local/share/vim/vim91/plugin/gzip.vim
016.789 000.234 000.234: sourcing /home/rothwell/.usr/local/share/vim/vim91/plugin/logiPat.vim
016.847 000.038 000.038: sourcing /home/rothwell/.usr/local/share/vim/vim91/plugin/manpager.vim
017.217 000.355 000.355: sourcing /home/rothwell/.usr/local/share/vim/vim91/plugin/matchparen.vim
017.827 000.589 000.589: sourcing /home/rothwell/.usr/local/share/vim/vim91/plugin/netrwPlugin.vim
017.875 000.013 000.013: sourcing /home/rothwell/.usr/local/share/vim/vim91/plugin/rrhelper.vim
017.919 000.024 000.024: sourcing /home/rothwell/.usr/local/share/vim/vim91/plugin/spellfile.vim
018.116 000.179 000.179: sourcing /home/rothwell/.usr/local/share/vim/vim91/plugin/tarPlugin.vim
018.337 000.186 000.186: sourcing /home/rothwell/.usr/local/share/vim/vim91/plugin/tohtml.vim
018.396 000.027 000.027: sourcing /home/rothwell/.usr/local/share/vim/vim91/plugin/tutor.vim
018.685 000.268 000.268: sourcing /home/rothwell/.usr/local/share/vim/vim91/plugin/vimballPlugin.vim
018.949 000.216 000.216: sourcing /home/rothwell/.usr/local/share/vim/vim91/plugin/zipPlugin.vim
019.442 000.388 000.388: sourcing /home/rothwell/.usr/local/share/vim/vim91/pack/dist/opt/matchit/plugin/matchit.vim
019.449 001.269: loading plugins
019.523 000.074: loading packages
019.546 000.023: loading after plugins
019.553 000.007: inits 3
019.997 000.444: reading viminfo
020.007 000.010: setting raw mode
020.063 000.056: start termcap
020.103 000.040: clearing screen
021.841 000.211 000.211: sourcing /home/rothwell/.vim/pack/vimpack/opt/mark.vim/autoload/mark.vim
021.957 001.643: opening buffers
021.982 000.025: BufEnter autocommands
021.984 000.002: editing files in windows
优化之后
- 使用测试脚本测试1000次
sh test.sh 1000
9.040 ms
- 最后一次启动信息
000.004 000.004: --- VIM STARTING ---
000.085 000.081: Allocated generic buffers
000.172 000.087: locale set
000.176 000.004: window checked
000.495 000.319: inits 1
000.530 000.035: parsing arguments
000.531 000.001: expanding arguments
000.562 000.031: shell init
000.941 000.379: Termcap init
000.953 000.012: inits 2
001.061 000.108: init highlight
001.599 000.313 000.313: sourcing /home/rothwell/.usr/local/share/vim/vim91/filetype.vim
001.715 000.067 000.067: sourcing /home/rothwell/.usr/local/share/vim/vim91/ftplugin.vim
001.786 000.032 000.032: sourcing /home/rothwell/.usr/local/share/vim/vim91/indent.vim
002.198 000.957 000.545: sourcing /home/rothwell/.vim/vimrc/option.vim
004.402 000.004 000.004: sourcing /home/rothwell/.usr/local/share/vim/vim91/colors/lists/default.vim
004.887 000.412 000.412: sourcing /home/rothwell/.vim/pack/vimpack/opt/color.vim/colors/gruvbox.vim
004.978 000.682 000.266: sourcing /home/rothwell/.usr/local/share/vim/vim91/syntax/synload.vim
005.077 000.894 000.212: sourcing /home/rothwell/.usr/local/share/vim/vim91/syntax/syntax.vim
005.243 000.083 000.083: sourcing /home/rothwell/.usr/local/share/vim/vim91/ftplugin/man.vim
005.552 003.297 002.320: sourcing /home/rothwell/.vim/vimrc/plugin.vim
005.654 000.078 000.078: sourcing /home/rothwell/.vim/vimrc/misc.vim
006.329 000.655 000.655: sourcing /home/rothwell/.vim/vimrc/keymap.vim
006.334 005.141 000.154: sourcing $HOME/.vimrc
006.336 000.134: sourcing vimrc file(s)
006.557 000.075 000.075: sourcing /home/rothwell/.vim/pack/vimpack/opt/matchparen.vim/plugin/matchparen.vim
006.689 000.072 000.072: sourcing /home/rothwell/.vim/pack/vimpack/opt/fzy.vim/plugin/fzf.vim
006.810 000.071 000.071: sourcing /home/rothwell/.vim/pack/vimpack/opt/floaterm.vim/plugin/floaterm.vim
006.922 000.060 000.060: sourcing /home/rothwell/.vim/pack/vimpack/opt/git.vim/plugin/git.vim
007.105 000.108 000.108: sourcing /home/rothwell/.vim/pack/vimpack/opt/sign.vim/plugin/sign.vim
007.251 000.077 000.077: sourcing /home/rothwell/.vim/pack/vimpack/opt/mark.vim/plugin/mark.vim
007.364 000.057 000.057: sourcing /home/rothwell/.vim/pack/vimpack/opt/j.vim/plugin/j.vim
007.582 000.139 000.139: sourcing /home/rothwell/.vim/pack/vimpack/opt/auto-pairs.vim/plugin/auto-pairs.vim
007.734 000.071 000.071: sourcing /home/rothwell/.vim/pack/vimpack/opt/line.vim/plugin/line.vim
008.292 000.453 000.453: sourcing /home/rothwell/.usr/local/share/vim/vim91/plugin/netrwPlugin.vim
008.868 000.447 000.447: sourcing /home/rothwell/.usr/local/share/vim/vim91/pack/dist/opt/matchit/plugin/matchit.vim
008.873 000.907: loading plugins
008.959 000.086: loading packages
008.991 000.032: loading after plugins
008.996 000.005: inits 3
009.472 000.476: reading viminfo
009.481 000.009: setting raw mode
009.502 000.021: start termcap
009.518 000.016: clearing screen
010.040 000.137 000.137: sourcing /home/rothwell/.vim/pack/vimpack/opt/mark.vim/autoload/mark.vim
010.124 000.469: opening buffers
010.146 000.022: BufEnter autocommands
010.147 000.001: editing files in windows
个人优化思路
要想优化启动速度,个人认为首先是尽可能减少默认加载的耗时,其次是尽可能优化使用的插件数量。
目前我的优化思路是默认耗时较长的尽可能直接替换或者精简,个人插件全部使用vim9script版本
默认加载优化
默认加载比较耗时的,目前我修改了filetype.vim以及优化了语法高亮的一些逻辑
- filetype.vim
目前我测试的版本,filetype.vim文件有3353行内容,该版本提供的全面的文件类型,同时也增加了启动速度。
个人根据日常使用精简了一个版本,可以根据实际需要添加
vim9script noclear
if exists("g:did_load_filetypes")
finish
endif
g:did_load_filetypes = 1
augroup filetypedetect
au BufNewFile,BufRead *.asm,*.[sS] setf asm
au BufNewFile,BufRead *.c setf c
au BufNewFile,BufRead *.rs setf rust
au BufNewFile,BufRead *.cxx,*.c++,*.cc,*.hpp,*.h setf cpp
au BufNewFile,BufRead *.py,*.pyw setf python
au BufNewFile,BufRead *.md setf markdown
au BufNewFile,BufRead .shrc,.bashrc,bashrc,bash.bashrc,.bash[_-]profile,.bash[_-]logout,.bash[_-]aliases,.bash[_-]history,*.bash setf bash
au BufNewFile,BufRead [mM]akefile.am,GNUmakefile.am setf automake
au BufNewFile,BufRead CMakeLists.txt,*.cmake,*.cmake.in setf cmake
au BufNewFile,BufRead configure.in,configure.ac setf config
au BufNewFile,BufRead *.diff,*.rej,*.patch setf diff
au BufNewFile,BufRead *.dts,*.dtsi setf dts
au BufNewFile,BufRead COMMIT_EDITMSG,MERGE_MSG,TAG_EDITMSG,NOTES_EDITMSG,EDIT_DESCRIPTION setf gitcommit
au BufNewFile,BufRead *[mM]akefile*,*.mk,*.mak,Kbuild setf make
au BufNewFile,BufRead {.,}tmux*.conf* setf tmux
au BufNewFile,BufRead Kconfig,Kconfig.*,Config.in.* setf kconfig
au BufNewFile,BufRead $VIMRUNTIME/doc/*.txt setf help
au BufNewFile,BufRead *.txt
\ if getline('$') !~ 'vim:.*ft=help'
\| setf text
\| endif
runtime! ftdetect/*.vim
augroup END
au filetypedetect BufNewFile,BufRead,StdinReadPost *
\ if !did_filetype() && empty(win_gettype())
\| try | exe 'setf ' .. expand("<amatch>:e") | catch | endtry
\| endif
需要注意的是,最后一个事件会将没有指明文件类型的使用文件名后缀设置。比如我没有指定sh后缀的文件会根据规则设置成setf sh,个人感觉简单够用
对于检测不对的情况我一般遇到手动设置,如果次数多了就手动增加到filetype.vim里,会存在一定麻烦
- 语法高亮
个人配色(基于gruvbox-material)
vim9script noclear
set background=dark
g:colors_name = 'gruvbox'
g:terminal_ansi_colors = ['#5a524c', '#ea6962', '#a9b665', '#d8a657', '#7daea3', '#d3869b', '#89b482', '#d4be98', '#5a524c', '#ea6962', '#a9b665', '#d8a657', '#7daea3', '#d3869b', '#89b482', '#d4be98']
hi! link lCursor Cursor
hi! link CursorIM Cursor
hi! link CurSearch IncSearch
hi! link Terminal Normal
hi! link NormalNC Normal
hi! link PmenuKindSel PmenuSel
hi! link WildMenu PmenuSel
hi! link PmenuExtraSel PmenuSel
hi! link ComplMatchIns Comment
hi! link StatusLineTerm StatusLine
hi! link StatusLineTermNC StatusLineNC
hi! link CursorLineFold FoldColumn
hi! link CursorLineSign SignColumn
hi Normal guifg=#d4be98 guibg=#282828 gui=NONE ctermfg=223 ctermbg=235 cterm=NONE
hi Added guifg=#a9b665 guibg=NONE gui=NONE ctermfg=142 ctermbg=NONE cterm=NONE
hi Changed guifg=#7daea3 guibg=NONE gui=NONE ctermfg=109 ctermbg=NONE cterm=NONE
hi Removed guifg=#ea6962 guibg=NONE gui=NONE ctermfg=167 ctermbg=NONE cterm=NONE
hi Boolean guifg=#d3869b guibg=NONE gui=NONE ctermfg=175 ctermbg=NONE cterm=NONE
hi CursorLineNr guifg=#928374 guibg=NONE gui=NONE ctermfg=245 ctermbg=NONE cterm=NONE
hi CursorLine guifg=NONE guibg=#32302f gui=NONE ctermfg=NONE ctermbg=236 cterm=NONE
hi CursorColumn guifg=NONE guibg=#32302f gui=NONE ctermfg=NONE ctermbg=236 cterm=NONE
hi ColorColumn guifg=NONE guibg=#32302f gui=NONE ctermfg=NONE ctermbg=236 cterm=NONE
hi Conceal guifg=#5a524c guibg=NONE gui=NONE ctermfg=239 ctermbg=NONE cterm=NONE
hi Cursor gui=reverse cterm=reverse
hi Comment guifg=#928374 guibg=NONE gui=italic ctermfg=245 ctermbg=NONE cterm=italic
hi Constant guifg=#89b482 guibg=NONE gui=NONE ctermfg=108 ctermbg=NONE cterm=NONE
hi Character guifg=#a9b665 guibg=NONE gui=NONE ctermfg=142 ctermbg=NONE cterm=NONE
hi Conditional guifg=#ea6962 guibg=NONE gui=NONE ctermfg=167 ctermbg=NONE cterm=NONE
hi Directory guifg=#a9b665 guibg=NONE gui=NONE ctermfg=142 ctermbg=NONE cterm=NONE
hi Debug guifg=#e78a4e guibg=NONE gui=NONE ctermfg=208 ctermbg=NONE cterm=NONE
hi DiffAdd guifg=NONE guibg=#34381b gui=NONE ctermfg=NONE ctermbg=22 cterm=NONE
hi DiffChange guifg=NONE guibg=#0e363e gui=NONE ctermfg=NONE ctermbg=17 cterm=NONE
hi DiffDelete guifg=NONE guibg=#402120 gui=NONE ctermfg=NONE ctermbg=52 cterm=NONE
hi DiffText guifg=#7daea3 guibg=#282828 gui=reverse ctermfg=109 ctermbg=235 cterm=reverse
hi Define guifg=#d3869b guibg=NONE gui=NONE ctermfg=175 ctermbg=NONE cterm=NONE
hi Delimiter guifg=#d4be98 guibg=NONE gui=NONE ctermfg=223 ctermbg=NONE cterm=NONE
hi debugPC guifg=#282828 guibg=#a9b665 gui=NONE ctermfg=235 ctermbg=142 cterm=NONE
hi debugBreakpoint guifg=#282828 guibg=#ea6962 gui=NONE ctermfg=235 ctermbg=167 cterm=NONE
hi EndOfBuffer guifg=#5a524c guibg=NONE gui=NONE ctermfg=239 ctermbg=NONE cterm=NONE
hi ErrorMsg guifg=#ea6962 guibg=NONE gui=bold,underline ctermfg=167 ctermbg=NONE cterm=bold,underline
hi Error guifg=#ea6962 guibg=NONE gui=NONE ctermfg=167 ctermbg=NONE cterm=NONE
hi Exception guifg=#ea6962 guibg=NONE gui=italic ctermfg=167 ctermbg=NONE cterm=italic
hi Identifier guifg=#7daea3 guibg=NONE gui=NONE ctermfg=109 ctermbg=NONE cterm=NONE
hi Include guifg=#d3869b guibg=NONE gui=italic ctermfg=175 ctermbg=NONE cterm=italic
hi Folded guifg=#928374 guibg=#32302f gui=NONE ctermfg=245 ctermbg=236 cterm=NONE
hi FoldColumn guifg=#5a524c guibg=NONE gui=NONE ctermfg=239 ctermbg=NONE cterm=NONE
hi FloatermBorder guifg=#928374 guibg=NONE gui=NONE ctermfg=245 ctermbg=NONE cterm=NONE
hi Float guifg=#d3869b guibg=NONE gui=NONE ctermfg=175 ctermbg=NONE cterm=NONE
hi Function guifg=#a9b665 guibg=NONE gui=bold ctermfg=142 ctermbg=NONE cterm=bold
hi IncSearch guifg=#ea6962 guibg=#282828 gui=reverse ctermfg=167 ctermbg=235 cterm=reverse
hi Ignore guifg=#928374 guibg=NONE gui=NONE ctermfg=245 ctermbg=NONE cterm=NONE
hi Keyword guifg=#ea6962 guibg=NONE gui=italic ctermfg=167 ctermbg=NONE cterm=italic
hi LineNr guifg=#5a524c guibg=NONE gui=NONE ctermfg=239 ctermbg=NONE cterm=NONE
hi Label guifg=#e78a4e guibg=NONE gui=NONE ctermfg=208 ctermbg=NONE cterm=NONE
hi MatchParen guifg=NONE guibg=#45403d gui=NONE ctermfg=NONE ctermbg=237 cterm=NONE
hi ModeMsg guifg=#d4be98 guibg=NONE gui=bold ctermfg=223 ctermbg=NONE cterm=bold
hi MsgArea guifg=#d4be98 guibg=NONE gui=bold,italic ctermfg=237 ctermbg=NONE cterm=bold,italic
hi MoreMsg guifg=#d8a657 guibg=NONE gui=bold ctermfg=214 ctermbg=NONE cterm=bold
hi Macro guifg=#89b482 guibg=NONE gui=NONE ctermfg=108 ctermbg=NONE cterm=NONE
hi NonText guifg=#5a524c guibg=NONE gui=NONE ctermfg=239 ctermbg=NONE cterm=NONE
hi Number guifg=#d3869b guibg=NONE gui=NONE ctermfg=175 ctermbg=NONE cterm=NONE
hi Operator guifg=#e78a4e guibg=NONE gui=NONE ctermfg=208 ctermbg=NONE cterm=NONE
hi Pmenu guifg=#ddc7a1 guibg=#45403d gui=NONE ctermfg=223 ctermbg=237 cterm=NONE
hi PmenuSel guifg=#45403d guibg=#a89984 gui=NONE ctermfg=237 ctermbg=246 cterm=NONE
hi PmenuKind guifg=#a9b665 guibg=#45403d gui=NONE ctermfg=142 ctermbg=237 cterm=NONE
hi PmenuExtra guifg=#a89984 guibg=#45403d gui=NONE ctermfg=246 ctermbg=237 cterm=NONE
hi PmenuSbar guifg=NONE guibg=#45403d gui=NONE ctermfg=NONE ctermbg=237 cterm=NONE
hi PmenuThumb guifg=NONE guibg=#7c6f64 gui=NONE ctermfg=NONE ctermbg=243 cterm=NONE
hi PreProc guifg=#d3869b guibg=NONE gui=italic ctermfg=175 ctermbg=NONE cterm=italic
hi PreCondit guifg=#d3869b guibg=NONE gui=NONE ctermfg=175 ctermbg=NONE cterm=NONE
hi Question guifg=#d8a657 guibg=NONE gui=NONE ctermfg=214 ctermbg=NONE cterm=NONE
hi QuickFixLine guifg=#d3869b guibg=NONE gui=bold ctermfg=175 ctermbg=NONE cterm=bold
hi Repeat guifg=#ea6962 guibg=NONE gui=NONE ctermfg=167 ctermbg=NONE cterm=NONE
hi SignColumn guifg=#d4be98 guibg=NONE gui=NONE ctermfg=223 ctermbg=NONE cterm=NONE
hi Search guifg=#a9b665 guibg=#282828 gui=reverse ctermfg=142 ctermbg=235 cterm=reverse
hi SpecialKey guifg=#5a524c guibg=NONE gui=NONE ctermfg=239 ctermbg=NONE cterm=NONE
hi SpellBad guifg=NONE guibg=NONE gui=undercurl ctermfg=NONE ctermbg=NONE cterm=undercurl
hi SpellCap guifg=NONE guibg=NONE gui=undercurl ctermfg=NONE ctermbg=NONE cterm=undercurl
hi SpellLocal guifg=NONE guibg=NONE gui=undercurl ctermfg=NONE ctermbg=NONE cterm=undercurl
hi SpellRare guifg=NONE guibg=NONE gui=undercurl ctermfg=NONE ctermbg=NONE cterm=undercurl
hi StatusLine guifg=#d4be98 guibg=NONE gui=NONE ctermfg=223 ctermbg=NONE cterm=NONE
hi StatusLineNC guifg=#928374 guibg=NONE gui=NONE ctermfg=245 ctermbg=NONE cterm=NONE
hi String guifg=#a9b665 guibg=NONE gui=NONE ctermfg=142 ctermbg=NONE cterm=NONE
hi Statement guifg=#ea6962 guibg=NONE gui=italic ctermfg=167 ctermbg=NONE cterm=italic
hi Special guifg=#d8a657 guibg=NONE gui=NONE ctermfg=214 ctermbg=NONE cterm=NONE
hi SpecialChar guifg=#d8a657 guibg=NONE gui=NONE ctermfg=214 ctermbg=NONE cterm=NONE
hi SpecialComment guifg=#928374 guibg=NONE gui=italic ctermfg=245 ctermbg=NONE cterm=italic
hi StorageClass guifg=#e78a4e guibg=NONE gui=NONE ctermfg=208 ctermbg=NONE cterm=NONE
hi Structure guifg=#e78a4e guibg=NONE gui=NONE ctermfg=208 ctermbg=NONE cterm=NONE
hi Todo guifg=#7daea3 guibg=#282828 gui=bold,reverse ctermfg=109 ctermbg=235 cterm=bold,reverse
hi TabLineSel guifg=NONE guibg=NONE gui=NONE ctermfg=NONE ctermbg=NONE cterm=NONE
hi TabLine guifg=NONE guibg=#3c3836 gui=NONE ctermfg=NONE ctermbg=236 cterm=NONE
hi TabLineFill guifg=NONE guibg=#3c3836 gui=NONE ctermfg=NONE ctermbg=236 cterm=NONE
hi Title guifg=#e78a4e guibg=NONE gui=bold ctermfg=208 ctermbg=NONE cterm=bold
hi Type guifg=#d8a657 guibg=NONE gui=NONE ctermfg=214 ctermbg=NONE cterm=NONE
hi Typedef guifg=#ea6962 guibg=NONE gui=italic ctermfg=167 ctermbg=NONE cterm=italic
hi Tag guifg=#e78a4e guibg=NONE gui=NONE ctermfg=208 ctermbg=NONE cterm=NONE
hi Underlined guifg=NONE guibg=NONE gui=undercurl ctermfg=NONE ctermbg=NONE cterm=undercurl
hi VertSplit guifg=#5a524c guibg=NONE gui=NONE ctermfg=239 ctermbg=NONE cterm=NONE
hi Visual guifg=NONE guibg=#45403d gui=NONE ctermfg=NONE ctermbg=237 cterm=NONE
hi VisualNOS guifg=NONE guibg=#45403d gui=NONE ctermfg=NONE ctermbg=237 cterm=NONE
hi WarningMsg guifg=#d8a657 guibg=NONE gui=bold ctermfg=214 ctermbg=NONE cterm=bold
hi ToolbarButton guifg=#282828 guibg=#a89984 gui=NONE ctermfg=235 ctermbg=246 cterm=NONE
hi ToolbarLine guifg=#ddc7a1 guibg=#45403d gui=NONE ctermfg=223 ctermbg=237 cterm=NONE
需要注意,该配色并非移植,所以并不通用。另外结合实际场景省略了highlight clear减少不必要加载时间
一般情况下使用配色的步骤是在.vimrc如下配置
syntax on
colorscheme xxx
我研究了一下syntax.txt里关于加载顺序时发现,可以如下设置
g:colors_name = 'gruvbox'
syntax enable
机制在于syntax enable或者syntax on会调用VIMRUNTIME/syntax/syntax.vim,然后在VIMRUNTIME/syntax/synload.vim18行如下:
" Set the default highlighting colors. Use a color scheme if specified.
if exists("colors_name")
exe "colors " . colors_name
else
runtime! syntax/syncolor.vim
endif
此外根据说明发现colorscheme命令每次都会调用$VIMRUNTIME/colors/lists/default.vim,该脚本个人测试耗时0.7ms
个人优化加载顺序,并且配色统一使用rgb数值指定颜色的情况下,直接删除内容保存文件就可以节约不必要启动时间
减少不必要插件启动
vim启动后通过scriptnames可以查看加载的vim脚本,个人只保留$VIMRUNTIME/plugin/目录下的netrwPlugin.vim(虽然保留也基本不用),删除其余的
默认情况下建议同时保留matchparen.vim,个人是因为精简了一版vim9script版本,主要原因是工作场景下会偶现报错(使用0tab term和1tab term开启两个终端,tabnew打开文件,使用tabnext来回切换)
有类似问题的可以留意注释,插件内容如下
- plugin/matchparen.vim
vim9script noclear
if exists("g:matchparen")
finish
endif
g:matchparen = 1
import autoload '../autoload/matchparen.vim'
augroup matchparen
autocmd!
autocmd! CursorMoved,CursorMovedI * matchparen.Highlight_Matching_Pair()
augroup END
- autoload/matchparen.vim
vim9script noclear
export def Highlight_Matching_Pair()
var [c, c2, flags] = ['', '', '']
var [idx, stopline, timeout] = [0, 0, 300]
var c_lnum = line('.')
var c_col = col('.')
var stoplinebottom = line('w$')
var stoplinetop = line('w0')
var plist = ['(', ')', '{', '}', '[', ']']
if !exists("w:matchparen_ids")
w:matchparen_ids = []
w:paren_hl_on = 0
endif
Remove_Matches()
#个人filetype.vim里检测win_gettype()类型,所以用empty(&ft)判断,常规可以用!empty(win_gettype())
if pumvisible() || empty(&ft)
return
endif
c = getline(c_lnum)->strpart(c_col - 1)->slice(0, 1)
idx = index(plist, c)
if idx < 0
return
endif
if idx % 2 == 0
flags = 'nW'
c2 = plist[idx + 1]
stopline = stoplinebottom
else
flags = 'nbW'
c2 = c
c = plist[idx - 1]
stopline = stoplinetop
endif
if c == '['
c = '\['
c2 = '\]'
endif
if mode() == 'i' || mode() == 'R'
timeout = 60
endif
var [m_lnum, m_col] = searchpairpos(c, '', c2, flags, '', stopline, timeout)
if m_lnum > 0 && m_lnum >= stoplinetop && m_lnum <= stoplinebottom
add(w:matchparen_ids, matchaddpos('MatchParen', [[c_lnum, c_col], [m_lnum, m_col]], 10))
w:paren_hl_on = 1
endif
enddef
export def Remove_Matches()
if w:paren_hl_on
while !empty(w:matchparen_ids)
silent! remove(w:matchparen_ids, 0)->matchdelete()
endwhile
w:paren_hl_on = 0
endif
enddef
关于文件替换个人封装了一个命令
def Clean()
var from = expand("<script>:p:h") .. "/filetype.vim"
var to = expand("$VIMRUNTIME/filetype.vim")
var dir = expand("$VIMRUNTIME/plugin/")
var whitelist = ["netrwPlugin.vim"]
if filereadable(from) && filereadable(to)
delete(to)
filecopy(from, to)
endif
from = expand("<script>:p:h") .. "/default.vim"
to = expand("$VIMRUNTIME/colors/lists/default.vim")
if filereadable(from) && filereadable(to)
delete(to)
filecopy(from, to)
endif
for file in readdir(dir)
(whitelist->index(file) != -1) ?? delete(dir .. file)
endfor
enddef
command! -nargs=0 PluginClean Clean()
对于大型插件懒加载的一种方式
例如lsp补全插件
vim9script
# yegappan/lsp
def LazyLoad()
packadd lsp
inoremap <expr> <Tab> pumvisible() ? "\<C-n>" : "\<Tab>"
inoremap <M-n> <Cmd>LspShowSignature<CR>
nnoremap <M-k> <Cmd>LspHover<CR>
set tagfunc=lsp#lsp#TagFunc
var lspOpts = {
autoHighlightDiags: false,
showSignature: false,
noNewlineInCompletion: true,
showInlayHints: true,
}
g:LspOptionsSet(lspOpts)
var lspServers = [
{
filetype: ['c', 'cpp'],
path: 'clangd',
args: ['--background-index'],
},
# {
# filetype: ['rust'],
# path: 'rust-analyzer',
# args: [],
# syncInit: v:true,
# },
]
g:LspAddServer(lspServers)
enddef
timer_start(0, (timer) => {
LazyLoad()
timer_stop(timer)
})
最后
多年前的憧憬,目前看来也只是个起点。个人插件也是按需实现,就不做介绍
现在入门也更推荐neovim(不由想起刚工作那会,别人开发用vim7.4,我只是编了个vim8.0就暗自窃喜)

浙公网安备 33010602011771号