How do I profile Vimscript functions to find what is slowing down Vim at runtime?
Answer
:profile start profile.log | profile func *
Explanation
When Vim feels sluggish during editing—not just at startup—you need runtime profiling to pinpoint which functions are consuming the most time. The :profile command instruments Vimscript function calls, measuring both self time and total time for each invocation, then writes a detailed log you can analyze.
How it works
:profile start profile.logbegins profiling and sets the output file where results will be written:profile func *tells Vim to profile every function call (you can replace*with a specific pattern likeMyPlugin*to narrow the scope)- After reproducing the slow behavior, run
:profile stopor quit Vim to flush the results - Open
profile.logto see a sorted breakdown of function call counts, total time, and self time
Example
" Start profiling all functions
:profile start /tmp/profile.log
:profile func *
" Optionally, also profile scripts (sourced files)
:profile file *
" Now do the slow operation...
" When done, stop profiling
:profile stop
The output log looks like:
FUNCTIONS SORTED ON SELF TIME
count total (s) self (s) function
142 1.235678 0.987654 MySlowFunction()
38 0.456789 0.345678 StatusLineUpdate()
5 0.123456 0.098765 SyntaxHighlightCheck()
Tips
- Use
:profile func MyPlugin*to narrow profiling to specific functions, reducing overhead - The
self timecolumn is usually most useful—it shows time spent in the function itself, excluding calls to other functions - Combine with
:profile file *to also see which script files are slow during sourcing - This is the runtime complement to
vim --startuptimewhich only measures startup