How do I profile Vim functions at runtime to find what's making it slow?
Answer
:profile start /tmp/profile.log | profile func *
Explanation
When Vim feels sluggish during editing (not just at startup), the :profile command lets you measure the execution time of every function call. This is different from --startuptime, which only measures startup. Runtime profiling catches slow autocommands, mappings, and plugin functions that fire during editing.
How it works
:profile start /tmp/profile.log— begin profiling, writing results to the specified fileprofile func *— profile all function calls (you can also specify a pattern likeprofile func MyPlugin*)- After reproducing the slow behavior, run
:profile stopor quit Vim - Open
/tmp/profile.logto see timing data sorted by total/self time
Example
To find why saving a file feels slow:
:profile start /tmp/profile.log
:profile func *
:profile file *
Now save the file with :w, then quit Vim. Examine the profile log:
FUNCTIONS SORTED ON TOTAL TIME
count total (s) self (s) function
1 0.523000 0.002000 MyPlugin#OnSave()
1 0.521000 0.521000 MyPlugin#LintBuffer()
This reveals that MyPlugin#LintBuffer() is consuming 521ms on every save.
Tips
- Add
profile file *to also profile sourced scripts, not just functions - Use
:profile pauseand:profile continueto isolate specific operations - For Neovim, you can also use
vim.profileror the--cmd 'profile start'flag - Focus on the "self time" column to find the actual bottleneck, not just callers