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

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 file
  • FileType is the event that triggers filetype plugins, indent rules, and any autocmd FileType entries 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 fromstart if syntax highlighting looks wrong after switching: :doautocmd FileType | syntax sync fromstart
  • :doautocmd BufEnter and :doautocmd BufRead are similarly useful for re-triggering other event-based configurations
  • See :help :doautocmd for the full syntax, including pattern matching

Next

How do I copy files to a target directory using netrw's marking system?