Profiling Vim
I like Vim because it's very fast. Unfortunately the other day I found myself opening a diff file that took forever to load. The file had 58187 (this number will be important later on) lines in it but I never thought Vim would choke with something that was less than 2M size.
This post was originally published on medium
Finding out which plugin is making Vim slow
If you find yourself in a similar situation this is what you can do in order to find out what is causing Vim to slow down.
-
Open Vim and start profiling
:profile start /tmp/profile.log
:profile func *
:profile file *This is telling Vim to save the results of the profile into
/tmp/profile.logand to run the profile for every file and function.Note: The
profile.logfile only gets generated until you close Vim. -
Do the action that is taking a long time to run (in my case opening the diff file)
:edit /tmp/file.diff
:profile pause
:q! -
Analyze the data
There is a lot of information in
/tmp/profile.logbut you can start by focusing on theTotal time. In my case there was a clear offender with a total time of more than 14 seconds! And it looked like this:FUNCTION <SNR>24_PreviewColorInLine()
Called 58187 times
Total time: 14.430544
Self time: 2.961442Remember the number of lines in the file I mentioned before? For me it was interesting to see that the function gets called just as many times.
-
Pinpoint the offender
Finding out where a function is defined is very easy thanks to the
<SNR>tag and the number right after it. You simply need to run:scriptnamesand scroll until you find the index number you are looking for, in my case 24.24: ~/.vim/bundle/colorizer/autoload/colorizer.vim
I opened up a GitHub issue to make the developers of the plugin aware but it seems as if the project has been left unmaintained so I decided to remove it from my vimrc file.