How do I find which Vim scripts or functions are slow and causing performance issues?
Answer
:profile start profile.log
Explanation
When Vim feels sluggish, the built-in :profile command lets you measure exactly how much time each script and function consumes. It writes detailed timing data to a log file, showing you where time is being spent so you can optimize or remove the offending code.
How it works
:profile start profile.log— begins profiling and sets the output file:profile func *— tells Vim to profile all functions (you can also target specific ones):profile file *— profiles all sourced scripts- After reproducing the slow behavior, quit Vim and inspect
profile.log
A typical profiling session looks like:
:profile start profile.log
:profile func *
:profile file *
" ... do the slow operation ...
:qa
Example
The output file contains per-function and per-line timing:
FUNCTION MySlowPlugin#highlight()
Called 47 times
Total time: 2.341500
Self time: 1.892300
count total (s) self (s)
47 1.892300 1.892300 let matches = matchlist(line, pattern)
This reveals that MySlowPlugin#highlight() was called 47 times and consumed 2.3 seconds, with the regex matching line being the bottleneck.
Tips
- For startup profiling, launch Vim with
vim --startuptime startup.log— this is separate from:profileand measures plugin load times - Sort the profile log by
Total timeto find the biggest offenders - Use
:profile pauseand:profile continueto isolate specific operations - Profile individual functions with
:profile func MyFuncinstead of*for targeted analysis