Profiling Vim

1 minute read

I’ve been using a pretty popular dotfile repo, YADR, for quite some time now. I’ve used it for so long that I’ve even made contributions to the project here and there.

But sometimes, it slows down when performing certain tasks for no apparent reason. In particular, while making Ruby code examples for a presentation a few days ago, I noticed that opening/saving a ruby file took 3-5 seconds! Wanting my itch to be scratched, I decided to look for ways to profile the slowness away!

It’s really easy, just pop your vim open and type this in normal mode:

# save to ~/record.log
:profile start ~/record.log 

# the profile for every function and file
:profile func *
:profile file *

# do whatever's slow action that you want to record, for example:
:open sample_ruby_file.rb

:profile pause

# required to quit to create ~/record.log

Next you can now check the bottom of ~/record.log, under “FUNCTIONS SORTED ON SELF TIME”:

count  total (s)   self (s)  function
    3   0.042048   0.015790  <SNR>4_SynSet()
    1   2.495415   0.014309  <SNR>31_LoadFTPlugin()
    6   0.009933   0.004577  <SNR>227_line()
    1   0.006000   0.003480  <SNR>32_LoadIndent()
   12   0.003720   0.002310  <SNR>227_expand()
... and more

In my case, since 31_LoadFTPlugin took 2 seconds, I went down the rabbit hole of why this is slow and ended up in vim-ruby/ftplugin issues.

Luckily, it’s fixed in the latest vim-ruby. So I just had to update mine via :PluginUpdate vim-ruby (for Vundler users).

So next time you see a large spike in your vim setup, profile the slowness away!

BONUS: Also, if your vim’s startup time is slow, you can still profile that with:

$ vim --startuptime time_cost.txt

There you have it! Happy fiddling around with vim!