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

How do I manually fire an autocommand event to re-trigger filetype settings or test autocmds?

Answer

:doautocmd

Explanation

:doautocmd fires any autocommand event manually, exactly as if that event had occurred naturally. This is invaluable when you need to force-reload filetype settings after switching a buffer's type, or when debugging your own autocmds during config development.

How it works

  • :doautocmd {Event} — triggers the named event for the current buffer
  • :doautocmd {Event} {pattern} — triggers for a file matching the given pattern
  • :doautoall {Event} — fires the event in every open buffer

Common events to trigger manually:

:doautocmd FileType python     " re-run all FileType python autocmds
:doautocmd BufEnter            " simulate entering the current buffer
:doautocmd ColorScheme         " reapply colorscheme-dependent highlights

Example

You open a file that was incorrectly detected as text and change its type:

:setfiletype python
:doautocmd FileType python

This triggers ftplugin settings, indent rules, syntax highlighting, and LSP attachment — everything that would have fired had Vim detected Python from the start.

Tips

  • Combine with :set filetype= or :setfiletype to switch types and reload settings in one workflow
  • Use :doautoall FileType to propagate a setting change across every buffer at once
  • In Neovim, the Lua equivalent is vim.api.nvim_exec_autocmds('FileType', { pattern = 'python' })
  • Prefix with :silent doautocmd to suppress any "No matching autocommands" messages

Next

How do I read from and write to files directly in Vimscript without shelling out to external commands?