Ashwin Purohit

Essays & Articles

Speeding up vim

I recently found myself with an 8 second vim startup time. No doubt due to plugins, since vim itself is quick, but it was horrifying and gave me Emacs flashbacks. Since 7.3, Vim has a profiler; let’s write its startup times to a log file, and open it:

vim --startuptime s.log s.log

times in msec
clock   self+sourced   self:  sourced script
clock   elapsed:              other lines
000.012  000.012: --- VIM STARTING ---
000.339  000.327: Allocated generic buffers
000.584  000.245: locale set
000.612  000.028: window checked
002.515  001.903: inits 1
002.535  000.020: parsing arguments
002.541  000.006: expanding arguments
002.590  000.049: shell init
003.677  001.087: Termcap init
003.743  000.066: inits 2
004.095  000.352: init highlight
005.526  000.962  000.962: sourcing /usr/share/vim/vim73/debian.vim
006.890  000.563  000.563: sourcing /usr/share/vim/vim73/syntax/syncolor.vim
...

We should try to get rid of lines with the highest elapsed times, in column two. Starting from the first line with a time,

:.,$!sort -n -r -k 2

That sorts to the end of the file -n(umerically), in -r(everse) so the highest times are first, and -k(eyed) on column 2.

times in msec
clock   self+sourced   self:  sourced script
clock   elapsed:              other lines
7012.437  3846.193  229.938: sourcing .../.vim/sessions/default
3099.982  2910.901  2910.901: sourcing .../config/.vim/bundle/vim-powerline/Powerline_default_default_compatible.cache
5270.192  1910.721  001.463: sourcing .../bin/share/vim/vim73/indent/htmldjango.vim
5269.421  1909.196  1908.687: sourcing .../.vim/bundle/web-indent/indent/html.vim
8295.618  1234.508: VimEnter autocommands
5854.548  583.832  583.775: sourcing .../.vim/bundle/web-indent/indent/html.vim
6463.395  565.818  000.981: sourcing .../bin/share/vim/vim73/indent/htmldjango.vim
6462.878  564.803  564.338: sourcing .../.vim/bundle/web-indent/indent/html.vim
6991.140  527.261  527.206: sourcing .../.vim/bundle/web-indent/indent/html.vim
097.454  084.202  039.007: sourcing $HOME/.vimrc

That top line takes 3846 ms by itself. Any function that adds more than 100 milliseconds is unacceptable. How to get rid of these offenders depends on your vim configuration, but I got rid of the lines in my .vimrc that were trying to autoload sessions on startup (which I don’t use), and wrote the Powerline cache to a different file (so it wouldn’t snowball under version control). The result?

times in msec
clock   self+sourced   self:  sourced script
clock   elapsed:              other lines
068.807  066.914  024.455: sourcing $HOME/.vimrc
068.751  024.493  023.868: sourcing .../.vim/colors/jellybeans.vim
126.100  022.154: opening buffers
103.136  016.129: loading plugins
039.598  015.738  015.637: sourcing .../bin/share/vim/vim73/filetype.vim
086.820  006.983  006.983: sourcing .../.vim/bundle/nerdcommenter/plugin/NERD_commenter.vim
078.795  003.701  003.701: sourcing .../.vim/bundle/fugitive/plugin/fugitive.vim
130.008  003.294: BufEnter autocommands

The longest function is only 66ms. Now vim starts instantly.

purohit@gmail.com

Software Engineer building back-end systems for ML tasks.

Previously: Stanford E.E., senior software engineer at 23andMe, lead software engineer at Recursion Pharma.

Want updates when I write?