vimtricks.wiki Concise Vim tricks, one at a time.

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 file
  • profile func * — profile all function calls (you can also specify a pattern like profile func MyPlugin*)
  • After reproducing the slow behavior, run :profile stop or quit Vim
  • Open /tmp/profile.log to 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 pause and :profile continue to isolate specific operations
  • For Neovim, you can also use vim.profiler or the --cmd 'profile start' flag
  • Focus on the "self time" column to find the actual bottleneck, not just callers

Next

How do I return to normal mode from absolutely any mode in Vim?