首页 > 代码库 > 对vim/gvim进行启动精简的探讨.

对vim/gvim进行启动精简的探讨.

前言: 

     用了很久的vim, 插件装得越来越多, 输入vim/gvim后打开项目越来越慢? 这个时候我们有必要引入一些方法来精简vim了.


导致vim变慢的情况, 需要具体分析一下.  是不是在不久前装了几款插件后, 发现启动gtk的vim卡了不少? 打开单行超过80字符的时候非常卡? 




vim启动的时候有一个参数, --startuptime 我们可以利用它来查看启动加载.

#gvim --startuptime ‘time.txt‘ 

然后打开当前目录下的time.txt 这个文件,  就是下面的这个文件. 左边第一列 A 是总耗时, 单位是微秒, 以 . 隔开, 比如 438.809 表示的是0.438809秒 , 第二列 B 是 该步骤加载耗时, 第三列 C 是该步骤加载完耗时, 第四列 D 是来源. 

times in msec
 clock   self+sourced   self:  sourced script
 clock   elapsed:              other lines
  A        B           C            D
000.011  000.011: --- VIM STARTING ---
000.156  000.145: Allocated generic buffers
000.235  000.079: locale set
000.254  000.019: GUI prepared
000.258  000.004: clipboard setup
000.268  000.010: window checked
000.893  000.625: inits 1
000.921  000.028: parsing arguments
000.925  000.004: expanding arguments
000.938  000.013: shell init
001.192  000.254: Termcap init
001.231  000.039: inits 2
001.331  000.100: init highlight
015.966  000.046  000.046: sourcing /usr/share/vim/vim74/autoload/paste.vim
024.677  009.375  009.329: sourcing /usr/share/vim/vim74/menu.vim
024.737  023.248  013.873: sourcing /usr/share/vim/vim74/filetype.vim
024.821  000.015  000.015: sourcing /usr/share/vim/vim74/filetype.vim
024.918  000.057  000.057: sourcing /usr/share/vim/vim74/ftplugin.vim
024.973  000.013  000.013: sourcing /usr/share/vim/vim74/filetype.vim
025.058  000.047  000.047: sourcing /usr/share/vim/vim74/indent.vim
025.113  000.013  000.013: sourcing /usr/share/vim/vim74/filetype.vim
025.162  000.013  000.013: sourcing /usr/share/vim/vim74/ftplugin.vim
025.208  000.012  000.012: sourcing /usr/share/vim/vim74/indent.vim
025.982  000.268  000.268: sourcing /usr/share/vim/vim74/delmenu.vim
026.996  000.732  000.732: sourcing /usr/share/vim/vim74/lang/menu_zh_cn.utf-8.vim
040.745  014.666  013.934: sourcing /usr/share/vim/vim74/menu.vim
041.243  000.344  000.344: sourcing /usr/share/vim/vim74/colors/desert.vim
041.542  000.261  000.261: sourcing /usr/share/vim/vim74/colors/desert.vim
041.967  000.183  000.183: sourcing /usr/share/vim/vim74/syntax/syncolor.vim
042.211  000.155  000.155: sourcing /usr/share/vim/vim74/syntax/syncolor.vim
042.353  000.688  000.350: sourcing /usr/share/vim/vim74/colors/desert.vim
042.456  000.830  000.142: sourcing /usr/share/vim/vim74/syntax/synload.vim
042.491  000.903  000.073: sourcing /usr/share/vim/vim74/syntax/syntax.vim
042.761  000.200  000.200: sourcing /usr/share/vim/vim74/syntax/nosyntax.vim
043.140  000.159  000.159: sourcing /usr/share/vim/vim74/syntax/syncolor.vim
043.396  000.160  000.160: sourcing /usr/share/vim/vim74/syntax/syncolor.vim
043.540  000.696  000.377: sourcing /usr/share/vim/vim74/colors/desert.vim
043.638  000.834  000.138: sourcing /usr/share/vim/vim74/syntax/synload.vim
043.682  001.155  000.121: sourcing /usr/share/vim/vim74/syntax/syntax.vim
045.988  000.449  000.449: sourcing /usr/share/vim/vim74/syntax/javascript.vim
049.264  003.119  003.119: sourcing /usr/share/vim/vim74/syntax/vb.vim
050.719  001.329  001.329: sourcing /usr/share/vim/vim74/syntax/css.vim
051.168  006.383  001.486: sourcing /usr/share/vim/vim74/syntax/html.vim
051.518  000.207  000.207: sourcing /usr/share/vim/vim74/syntax/sqloracle.vim
051.528  000.277  000.070: sourcing /usr/share/vim/vim74/syntax/sql.vim
054.714  010.014  003.354: sourcing /usr/share/vim/vim74/syntax/php.vim
056.148  054.779  003.750: sourcing $VIM/vimrc
056.180  000.070: sourcing vimrc file(s)
089.372  032.824  032.824: sourcing /usr/share/vim/vim74/plugin/EasyGrep.vim
089.810  000.342  000.342: sourcing /usr/share/vim/vim74/autoload/EasyMotion.vim
091.358  001.935  001.593: sourcing /usr/share/vim/vim74/plugin/EasyMotion.vim
094.438  003.051  003.051: sourcing /usr/share/vim/vim74/plugin/NERD_tree.vim
095.425  000.274  000.274: sourcing /usr/share/vim/vim74/autoload/acp.vim
095.628  001.153  000.879: sourcing /usr/share/vim/vim74/plugin/acp.vim
095.776  000.124  000.124: sourcing /usr/share/vim/vim74/plugin/authorinfo.vim
096.881  001.078  001.078: sourcing /usr/share/vim/vim74/plugin/bufexplorer.vim
097.771  000.859  000.859: sourcing /usr/share/vim/vim74/plugin/calendar.vim
098.011  000.212  000.212: sourcing /usr/share/vim/vim74/plugin/closetag.vim
106.485  008.451  008.451: sourcing /usr/share/vim/vim74/plugin/colorizer.vim
107.131  000.306  000.306: sourcing /usr/share/vim/vim74/autoload/colorv/data.vim
135.301  000.204  000.204: sourcing /usr/share/vim/vim74/autoload/colorv/mark.vim
135.421  000.076  000.076: sourcing /usr/share/vim/vim74/autoload/colorv/cache.vim
136.253  000.660  000.660: sourcing /usr/share/vim/vim74/autoload/colorv/scheme.vim
136.487  029.797  028.551: sourcing /usr/share/vim/vim74/autoload/colorv.vim
138.182  031.660  001.863: sourcing /usr/share/vim/vim74/plugin/colorv.vim
138.352  000.135  000.135: sourcing /usr/share/vim/vim74/plugin/csExplorer.vim
138.918  000.405  000.405: sourcing /usr/share/vim/vim74/autoload/ctrlp/mrufiles.vim
139.077  000.703  000.298: sourcing /usr/share/vim/vim74/plugin/ctrlp.vim
139.180  000.081  000.081: sourcing /usr/share/vim/vim74/plugin/getscriptPlugin.vim
139.949  000.748  000.748: sourcing /usr/share/vim/vim74/plugin/grep.vim
140.169  000.197  000.197: sourcing /usr/share/vim/vim74/plugin/gzip.vim
140.902  000.712  000.712: sourcing /usr/share/vim/vim74/plugin/matchit.vim
141.095  000.169  000.169: sourcing /usr/share/vim/vim74/plugin/matchparen.vim
142.512  001.394  001.394: sourcing /usr/share/vim/vim74/plugin/minibufexpl.vim
145.397  002.862  002.862: sourcing /usr/share/vim/vim74/plugin/mru.vim
150.262  004.828  004.828: sourcing /usr/share/vim/vim74/plugin/multisearch.vim
150.751  000.460  000.460: sourcing /usr/share/vim/vim74/plugin/netrwPlugin.vim
150.817  000.039  000.039: sourcing /usr/share/vim/vim74/plugin/rrhelper.vim
150.870  000.032  000.032: sourcing /usr/share/vim/vim74/plugin/spellfile.vim
151.016  000.126  000.126: sourcing /usr/share/vim/vim74/plugin/statusline.vim
151.555  000.516  000.516: sourcing /usr/share/vim/vim74/plugin/surround.vim
152.075  000.493  000.493: sourcing /usr/share/vim/vim74/plugin/taglist.vim
152.279  000.177  000.177: sourcing /usr/share/vim/vim74/plugin/tarPlugin.vim
152.371  000.068  000.068: sourcing /usr/share/vim/vim74/plugin/tohtml.vim
152.415  000.023  000.023: sourcing /usr/share/vim/vim74/plugin/txt.vim
152.595  000.157  000.157: sourcing /usr/share/vim/vim74/plugin/vimballPlugin.vim
152.640  000.019  000.019: sourcing /usr/share/vim/vim74/plugin/vimcdoc.vim
153.554  000.886  000.886: sourcing /usr/share/vim/vim74/plugin/winfileexplorer.vim
154.417  000.832  000.832: sourcing /usr/share/vim/vim74/plugin/winmanager.vim
154.725  000.280  000.280: sourcing /usr/share/vim/vim74/plugin/wintagexplorer.vim
156.538  001.788  001.788: sourcing /usr/share/vim/vim74/plugin/zencoding.vim
156.762  000.193  000.193: sourcing /usr/share/vim/vim74/plugin/zipPlugin.vim
156.787  001.342: loading plugins
156.797  000.010: inits 3
169.794  001.861  001.861: sourcing $VIMRUNTIME/menu.vim
170.075  000.202  000.202: sourcing $VIM/gvimrc
411.511  252.651: starting GUI
412.239  000.728: reading viminfo
412.594  000.355: GUI delay
412.598  000.004: setting raw mode
412.599  000.001: start termcap
412.637  000.038: clearing screen
419.274  000.701  000.701: sourcing /usr/share/vim/vim74/scripts.vim
420.916  007.578: opening buffers
421.829  000.913: BufEnter autocommands
421.831  000.002: editing files in windows
425.198  003.367: VimEnter autocommands
425.202  000.004: before starting main loop
433.756  008.554: first screen update
433.759  000.003: --- VIM STARTED ---



可以看见, 上面比较耗时的有GUI, menu,filetype,php,colorv 等插件和组件. 对于插件, 如果不是特别需要, 可以直接删除掉.

现在开始作实验:

首先是针对GUI, 我们使用vim 来代替刚才的启动,  可以得到结果, 没有GUI这项后启动时间大幅缩减.  结论, 没有gui的vim目前是最快的.

143.895  000.018: inits 3
144.595  000.700: reading viminfo
149.386  004.791: setup clipboard
149.420  000.034: setting raw mode
149.431  000.011: start termcap
149.468  000.037: clearing screen
150.476  001.008: opening buffers
151.790  001.314: BufEnter autocommands
151.794  000.004: editing files in windows
186.932  035.138: VimEnter autocommands
186.936  000.004: before starting main loop
188.833  001.897: first screen update
188.835  000.002: --- VIM STARTED ---

针对filetype, menu 这几个功能上的选项, 因为是功能上的, 如果精简掉, 会导致我们想要的vim的功能上的缺失. 所以是去是留看个人了.

针对插件, 直接删除掉即可. 如果不是必须的, 也可以重写插件.

针对vimrc文件的加载时间. 可以通过优化内容, 压缩空行, 压缩注释来实现. 我的vimrc 之前有1600行. 现在删除一些配置, 注释, 压缩到830行, 加载时间在3750微秒减少到了3254微秒.实际发现效果不明显.

055.359  054.415  003.254: sourcing $VIM/vimrc
055.391  000.073: sourcing vimrc file(s)


结论: 

    vim本身具有非常强大的功能, 非常高效的编辑效率, 通过一些插件, 我们可以更好的使用它, 熟悉它. 如果不是必要, 完全不需要把vim打造成一个全能的工具, 毕竟术业有专攻, 既然咖啡机可以煮咖啡, 为什么要emacs来煮. 精简删除掉一些不必要的插件, 提高vim的启动效率, 提高vim的稳定性, 进而提高编码效率, 这也不失为一个好的途径.  微笑