关于 vim 启动过程优化的问题

fevin
  • 1.1k

最近发现 vim 打开时有明显的延时。

记录 vim 打开过程的 log ,发现总耗时为 603.661 ms,详细日志如下:
(vim --startuptime vim.log test.php)

times in msec
 clock   self+sourced   self:  sourced script
 clock   elapsed:              other lines

000.013  000.013: --- VIM STARTING ---
000.151  000.138: Allocated generic buffers
000.486  000.335: locale set
000.490  000.004: clipboard setup
000.500  000.010: window checked
000.945  000.445: inits 1
001.051  000.106: parsing arguments
001.055  000.004: expanding arguments
004.120  003.065: shell init
004.384  000.264: Termcap init
004.402  000.018: inits 2
004.528  000.126: init highlight
004.934  000.049  000.049: sourcing /usr/local/share/vim/vim80/ftoff.vim
007.626  002.634  002.634: sourcing /Users/javin/.vim/autoload/plug.vim
033.285  022.033  022.033: sourcing /usr/local/share/vim/vim80/filetype.vim
033.453  000.041  000.041: sourcing /usr/local/share/vim/vim80/ftplugin.vim
033.586  000.029  000.029: sourcing /usr/local/share/vim/vim80/indent.vim
034.573  000.697  000.697: sourcing /usr/local/share/vim/vim80/syntax/syncolor.vim
034.671  000.905  000.208: sourcing /usr/local/share/vim/vim80/syntax/synload.vim
034.698  001.042  000.137: sourcing /usr/local/share/vim/vim80/syntax/syntax.vim
034.821  000.011  000.011: sourcing /usr/local/share/vim/vim80/filetype.vim
034.928  000.009  000.009: sourcing /usr/local/share/vim/vim80/ftplugin.vim
035.030  000.007  000.007: sourcing /usr/local/share/vim/vim80/indent.vim
035.136  000.009  000.009: sourcing /usr/local/share/vim/vim80/filetype.vim
035.237  000.008  000.008: sourcing /usr/local/share/vim/vim80/ftplugin.vim
035.636  000.225  000.225: sourcing /usr/local/Cellar/vim/8.0.1050/share/vim/vim80/syntax/nosyntax.vim
036.010  000.170  000.170: sourcing /usr/local/share/vim/vim80/syntax/syncolor.vim
036.106  000.372  000.202: sourcing /usr/local/share/vim/vim80/syntax/synload.vim
036.132  000.760  000.163: sourcing /usr/local/share/vim/vim80/syntax/syntax.vim
036.585  000.150  000.150: sourcing /usr/local/share/vim/vim80/syntax/syncolor.vim
036.931  000.142  000.142: sourcing /usr/local/share/vim/vim80/syntax/syncolor.vim
037.439  000.255  000.255: sourcing /usr/local/share/vim/vim80/syntax/syncolor.vim
038.036  001.795  001.248: sourcing /Users/javin/.vim/colors/monokai.vim
038.546  033.810  005.383: sourcing $HOME/.vimrc
038.559  000.221: sourcing vimrc file(s)
038.987  000.030  000.030: sourcing /Users/javin/.vim/plugin/phpcheck.vim
043.270  004.110  004.110: sourcing /Users/javin/.vim/plugged/vim-fugitive/plugin/fugitive.vim
043.647  000.146  000.146: sourcing /Users/javin/.vim/plugged/vim-airline/autoload/airline.vim
043.864  000.101  000.101: sourcing /Users/javin/.vim/plugged/vim-airline/autoload/airline/init.vim
044.323  000.100  000.100: sourcing /Users/javin/.vim/plugged/vim-airline/autoload/airline/parts.vim
045.045  001.643  001.296: sourcing /Users/javin/.vim/plugged/vim-airline/plugin/airline.vim
045.169  000.017  000.017: sourcing /Users/javin/.vim/plugged/vim-airline-themes/plugin/airline-themes.vim
045.768  000.132  000.132: sourcing /Users/javin/.vim/plugged/nerdtree/autoload/nerdtree.vim
046.802  000.423  000.423: sourcing /Users/javin/.vim/plugged/nerdtree/lib/nerdtree/path.vim
046.963  000.103  000.103: sourcing /Users/javin/.vim/plugged/nerdtree/lib/nerdtree/menu_controller.vim
047.083  000.069  000.069: sourcing /Users/javin/.vim/plugged/nerdtree/lib/nerdtree/menu_item.vim
047.224  000.091  000.091: sourcing /Users/javin/.vim/plugged/nerdtree/lib/nerdtree/key_map.vim
047.462  000.187  000.187: sourcing /Users/javin/.vim/plugged/nerdtree/lib/nerdtree/bookmark.vim
047.710  000.195  000.195: sourcing /Users/javin/.vim/plugged/nerdtree/lib/nerdtree/tree_file_node.vim
048.240  000.455  000.455: sourcing /Users/javin/.vim/plugged/nerdtree/lib/nerdtree/tree_dir_node.vim
048.502  000.206  000.206: sourcing /Users/javin/.vim/plugged/nerdtree/lib/nerdtree/opener.vim
048.762  000.207  000.207: sourcing /Users/javin/.vim/plugged/nerdtree/lib/nerdtree/creator.vim
048.857  000.044  000.044: sourcing /Users/javin/.vim/plugged/nerdtree/lib/nerdtree/flag_set.vim
049.125  000.144  000.144: sourcing /Users/javin/.vim/plugged/nerdtree/lib/nerdtree/nerdtree.vim
049.462  000.279  000.279: sourcing /Users/javin/.vim/plugged/nerdtree/lib/nerdtree/ui.vim
049.568  000.027  000.027: sourcing /Users/javin/.vim/plugged/nerdtree/lib/nerdtree/event.vim
049.656  000.038  000.038: sourcing /Users/javin/.vim/plugged/nerdtree/lib/nerdtree/notifier.vim
050.114  000.397  000.397: sourcing /Users/javin/.vim/plugged/nerdtree/autoload/nerdtree/ui_glue.vim
063.104  000.114  000.114: sourcing /Users/javin/.vim/plugged/nerdtree/nerdtree_plugin/exec_menuitem.vim
063.644  000.510  000.510: sourcing /Users/javin/.vim/plugged/nerdtree/nerdtree_plugin/fs_menu.vim
063.833  018.583  014.962: sourcing /Users/javin/.vim/plugged/nerdtree/plugin/NERD_tree.vim
064.441  000.304  000.304: sourcing /Users/javin/.vim/plugged/ctrlp.vim/autoload/ctrlp/mrufiles.vim
064.708  000.784  000.480: sourcing /Users/javin/.vim/plugged/ctrlp.vim/plugin/ctrlp.vim
065.091  000.019  000.019: sourcing /Users/javin/.vim/plugged/syntastic/plugin/syntastic/autoloclist.vim
065.130  000.017  000.017: sourcing /Users/javin/.vim/plugged/syntastic/plugin/syntastic/balloons.vim
065.168  000.016  000.016: sourcing /Users/javin/.vim/plugged/syntastic/plugin/syntastic/checker.vim
065.204  000.016  000.016: sourcing /Users/javin/.vim/plugged/syntastic/plugin/syntastic/cursor.vim
065.295  000.070  000.070: sourcing /Users/javin/.vim/plugged/syntastic/plugin/syntastic/highlighting.vim
065.401  000.060  000.060: sourcing /Users/javin/.vim/plugged/syntastic/plugin/syntastic/loclist.vim
065.474  000.023  000.023: sourcing /Users/javin/.vim/plugged/syntastic/plugin/syntastic/modemap.vim
065.518  000.019  000.019: sourcing /Users/javin/.vim/plugged/syntastic/plugin/syntastic/notifiers.vim
065.559  000.018  000.018: sourcing /Users/javin/.vim/plugged/syntastic/plugin/syntastic/registry.vim
065.600  000.020  000.020: sourcing /Users/javin/.vim/plugged/syntastic/plugin/syntastic/signs.vim
066.614  000.828  000.828: sourcing /Users/javin/.vim/plugged/syntastic/autoload/syntastic/util.vim
083.604  000.191  000.191: sourcing /Users/javin/.vim/plugged/syntastic/plugin/syntastic/autoloclist.vim
083.800  000.163  000.163: sourcing /Users/javin/.vim/plugged/syntastic/plugin/syntastic/balloons.vim
084.324  000.460  000.460: sourcing /Users/javin/.vim/plugged/syntastic/plugin/syntastic/checker.vim
084.619  000.251  000.251: sourcing /Users/javin/.vim/plugged/syntastic/plugin/syntastic/cursor.vim
084.824  000.176  000.176: sourcing /Users/javin/.vim/plugged/syntastic/plugin/syntastic/highlighting.vim
085.526  000.669  000.669: sourcing /Users/javin/.vim/plugged/syntastic/plugin/syntastic/loclist.vim
085.850  000.247  000.247: sourcing /Users/javin/.vim/plugged/syntastic/plugin/syntastic/modemap.vim
086.064  000.184  000.184: sourcing /Users/javin/.vim/plugged/syntastic/plugin/syntastic/notifiers.vim
086.587  000.466  000.466: sourcing /Users/javin/.vim/plugged/syntastic/plugin/syntastic/registry.vim
086.734  000.108  000.108: sourcing /Users/javin/.vim/plugged/syntastic/plugin/syntastic/signs.vim
088.015  022.394  018.651: sourcing /Users/javin/.vim/plugged/syntastic/plugin/syntastic.vim
088.837  000.612  000.612: sourcing /Users/javin/.vim/plugged/taglist.vim/plugin/taglist.vim
089.307  000.337  000.337: sourcing /Users/javin/.vim/plugged/YouCompleteMe/plugin/youcompleteme.vim
090.266  000.847  000.847: sourcing /Users/javin/.vim/plugged/DoxygenToolkit.vim/plugin/DoxygenToolkit.vim
091.080  000.700  000.700: sourcing /Users/javin/.vim/plugged/vim-dirdiff/plugin/dirdiff.vim
092.442  001.259  001.259: sourcing /Users/javin/.vim/plugged/LeaderF/plugin/leaderf.vim
093.402  000.487  000.487: sourcing /Users/javin/.vim/plugged/vim-javacomplete2/autoload/javacomplete/util.vim
094.008  001.233  000.746: sourcing /Users/javin/.vim/plugged/vim-javacomplete2/plugin/javacomplete.vim
094.632  000.072  000.072: sourcing /usr/local/share/vim/vim80/plugin/getscriptPlugin.vim
095.018  000.360  000.360: sourcing /usr/local/share/vim/vim80/plugin/gzip.vim
095.288  000.243  000.243: sourcing /usr/local/share/vim/vim80/plugin/logiPat.vim
095.334  000.017  000.017: sourcing /usr/local/share/vim/vim80/plugin/manpager.vim
095.559  000.174  000.174: sourcing /usr/local/share/vim/vim80/plugin/matchparen.vim
096.168  000.584  000.584: sourcing /usr/local/share/vim/vim80/plugin/netrwPlugin.vim
096.221  000.018  000.018: sourcing /usr/local/share/vim/vim80/plugin/rrhelper.vim
096.280  000.029  000.029: sourcing /usr/local/share/vim/vim80/plugin/spellfile.vim
096.455  000.147  000.147: sourcing /usr/local/share/vim/vim80/plugin/tarPlugin.vim
096.576  000.090  000.090: sourcing /usr/local/share/vim/vim80/plugin/tohtml.vim
096.767  000.162  000.162: sourcing /usr/local/share/vim/vim80/plugin/vimballPlugin.vim
097.005  000.201  000.201: sourcing /usr/local/share/vim/vim80/plugin/zipPlugin.vim
097.033  003.550: loading plugins
097.100  000.067: loading packages
097.127  000.027: loading after plugins
097.137  000.010: inits 3
097.437  000.300: reading viminfo
097.491  000.054: setting raw mode
097.496  000.005: start termcap
097.533  000.037: clearing screen
100.599  000.123  000.123: sourcing /usr/local/share/vim/vim80/ftplugin/java.vim
101.247  000.166  000.166: sourcing /usr/local/share/vim/vim80/indent/java.vim
103.442  001.243  001.243: sourcing /usr/local/Cellar/vim/8.0.1050/share/vim/vim80/syntax/html.vim
105.671  003.944  002.701: sourcing /usr/local/share/vim/vim80/syntax/java.vim
107.088  000.517  000.517: sourcing /Users/javin/.vim/plugged/vim-airline/autoload/airline/extensions.vim
107.575  000.177  000.177: sourcing /Users/javin/.vim/plugged/vim-airline/autoload/airline/extensions/quickfix.vim
108.050  000.175  000.175: sourcing /Users/javin/.vim/plugged/vim-airline/autoload/airline/extensions/netrw.vim
108.272  000.073  000.073: sourcing /Users/javin/.vim/plugged/vim-airline/autoload/airline/extensions/term.vim
108.622  000.122  000.122: sourcing /Users/javin/.vim/plugged/vim-airline/autoload/airline/extensions/ctrlp.vim
109.282  000.403  000.403: sourcing /Users/javin/.vim/plugged/vim-airline/autoload/airline/extensions/branch.vim
109.721  000.084  000.084: sourcing /Users/javin/.vim/plugged/vim-airline/autoload/airline/extensions/syntastic.vim
110.201  000.176  000.176: sourcing /Users/javin/.vim/plugged/vim-airline/autoload/airline/extensions/whitespace.vim
110.556  000.070  000.070: sourcing /Users/javin/.vim/plugged/vim-airline/autoload/airline/extensions/wordcount.vim
110.791  000.047  000.047: sourcing /Users/javin/.vim/plugged/vim-airline/autoload/airline/extensions/keymap.vim
114.004  000.100  000.100: sourcing /Users/javin/.vim/plugged/vim-airline/autoload/airline/section.vim
114.414  000.253  000.253: sourcing /Users/javin/.vim/plugged/vim-airline/autoload/airline/highlighter.vim
117.108  000.090  000.090: sourcing /Users/javin/.vim/plugged/vim-airline/autoload/airline/themes.vim
117.384  000.475  000.385: sourcing /Users/javin/.vim/plugged/vim-airline-themes/autoload/airline/themes/molokai.vim
128.999  000.129  000.129: sourcing /Users/javin/.vim/plugged/vim-airline/autoload/airline/util.vim
131.169  000.185  000.185: sourcing /Users/javin/.vim/plugged/vim-airline/autoload/airline/builder.vim
131.580  000.116  000.116: sourcing /Users/javin/.vim/plugged/vim-airline/autoload/airline/extensions/default.vim
177.715  000.065  000.065: sourcing /Users/javin/.vim/plugged/vim-javacomplete2/autoload/javacomplete/logger.vim
178.738  000.095  000.095: sourcing /Users/javin/.vim/plugged/vim-javacomplete2/autoload/javacomplete/classpath/classpath.vim
179.757  000.124  000.124: sourcing /Users/javin/.vim/plugged/vim-javacomplete2/autoload/javacomplete/classpath/maven.vim
180.040  000.116  000.116: sourcing /Users/javin/.vim/plugged/vim-javacomplete2/autoload/javacomplete/classpath/gradle.vim
180.090  003.267  002.867: sourcing /Users/javin/.vim/plugged/vim-javacomplete2/autoload/javacomplete.vim
180.551  000.336  000.336: sourcing /Users/javin/.vim/plugged/vim-javacomplete2/autoload/javacomplete/server.vim
412.762  000.392  000.392: sourcing /Users/javin/.vim/plugged/ctrlp.vim/autoload/ctrlp/utils.vim
415.664  000.392  000.392: sourcing /Users/javin/.vim/plugged/syntastic/autoload/syntastic/log.vim
415.737  306.482: opening buffers
422.917  005.679  005.679: sourcing /Users/javin/github/vimrc/.vim/plugged/taglist.vim/plugin/taglist.vim
440.433  000.445  000.445: sourcing /Users/javin/.vim/plugged/LeaderF/autoload/leaderf.vim
444.070  004.487  004.042: sourcing /Users/javin/.vim/plugged/LeaderF/autoload/lfMru.vim
445.060  019.157: BufEnter autocommands
445.068  000.008: editing files in windows
446.596  000.816  000.816: sourcing /Users/javin/.vim/plugged/YouCompleteMe/autoload/youcompleteme.vim
561.594  115.710: VimEnter autocommands
561.602  000.008: before starting main loop
585.980  000.669  000.669: sourcing /Users/javin/.vim/plugged/vim-airline/autoload/airline/async.vim
603.494  041.223: first screen update
603.661  000.167: --- VIM STARTED ---

将耗时进行排序(sort -nrk 2),我发现 opening buffersVimEnter autocommands 耗时占比较大,值在 100ms 以上的。
请问,如何优化这个问题。

回复
阅读 3.8k
1 个回答
liuchengxu
  • 720
✓ 已被采纳

主要的解决方案有两种:

利用插件管理器的 on-demanding loading

现在插件管理器用户比较多的是 vim-plugdein,都支持这种按需加载. 我用的是 vim-plug, 以 vim-plug 为例:

Plug 'scrooloose/nerdtree', { 'on': ['NERDTreeToggle', 'NERDTreeFind'] }
augroup loadNerdtree
autocmd!
autocmd VimEnter * silent! autocmd! FileExplorer
autocmd BufEnter,BufNew *
          \  if isdirectory(expand('<amatch>'))
          \|   call plug#load('nerdtree')
          \|   call nerdtree#checkForBrowse(expand("<amatch>"))
          \| endif
augroup END

详情见 这里 ,在用到该命令时再进行加载。

使用这一方法来加载那些不常用或者无须启动时加载的插件,比如 nerdtree, vim-javacomplete2, ctrlp 等等。更多内容可以参考 space-vim 中 layer 下面的 packages.vim 里面的用法。


利用定时器 api timer_start

space-vim 就用了这个方法进行了加速,详情见 这里 , 灵感来自于 Reddit 上的一个帖子timer_start() 对 vim 的版本有一定要求,不过 vim8 和 neovim 都是已经有了,:echo exists('*timer_start') 看一下是否存在。

基本用法是在插件管理器中设置不加载该插件,然后在timer_start 的回调中进行加载:

" 'on':[] 在 vim-plug 中表示不加载该插件
Plug 'easymotion/vim-easymotion'           , { 'on': [] }

" 500 毫秒后调用 LoadPlug,且只调用一次, 见 `:h timer_start()`
call timer_start(500, 'LoadPlug')

function! LoadPlug(timer) abort
  " 手动加载 vim-easymotion
  call plug#load('vim-easymotion')
endfunction

space-vim 用到的插件应当说不少,但是启动时间基本不超过 100 ms。

宣传栏