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

How do I find which script defined my BufWritePre autocommands?

Answer

:verbose autocmd BufWritePre

Explanation

When a save hook starts behaving unexpectedly, the hard part is usually finding who defined it. :verbose autocmd BufWritePre lists every autocommand for that event and shows the script location where each one was last defined. This is especially useful in larger configs where plugins and ftplugins both register write-time logic.

How it works

  • :autocmd BufWritePre filters the autocommand list to the pre-write event
  • :verbose adds origin metadata (script path and line) to each matching entry
  • The output reflects the current effective definitions, so it also helps catch accidental overrides

Example

Suppose saving a Python file strips trailing spaces and also runs a formatter unexpectedly.

You save main.py and two different BufWritePre hooks fire.

Run:

:verbose autocmd BufWritePre

You will see each matching command plus where it came from, for example your init.vim vs a plugin file. That gives you a concrete target to edit, disable, or move into a dedicated augroup.

Tips

  • Use an augroup name in your own config so your hooks are easy to identify in this output
  • Pair this with :verbose set option? when behavior might come from option changes instead of autocommands

Next

How do I open another buffer in a split without changing the alternate-file register?