How do I manually re-trigger filetype-based autocmds without reopening the file?
Answer
:doautocmd FileType
Explanation
When you change a buffer's filetype mid-session — say with :set filetype=python — Vim updates the filetype option but does not automatically re-run the FileType autocmds that configure indentation, syntax, and keymaps. :doautocmd FileType fires those autocmds immediately, applying all filetype-specific settings to the current buffer without needing to close and reopen the file.
How it works
:doautocmd {event}manually fires all autocmds registered for{event}for the current fileFileTypeis the event that triggers filetype plugins, indent rules, and anyautocmd FileTypeentries in your config- By default it uses the current filename as the match pattern — if you need to specify explicitly:
:doautocmd FileType python - The
<nomodeline>variant (:doautocmd <nomodeline> FileType) skips any modeline processing
Example
You open a .conf file and then realize it uses Python syntax:
:set filetype=python
:doautocmd FileType
Now Python indentation, syntax highlighting, and any FileType python mappings from your config are active — as if you had originally opened a .py file.
Tips
- Useful in scripts and plugins that change the filetype programmatically and need settings to take effect immediately
- Combine with
:syntax sync fromstartif syntax highlighting looks wrong after switching::doautocmd FileType | syntax sync fromstart :doautocmd BufEnterand:doautocmd BufReadare similarly useful for re-triggering other event-based configurations- See
:help :doautocmdfor the full syntax, including pattern matching