Automatic commands

Autocommands are a way to automatically execute code when certain events happen.

:au :autocmd :autocmd! events pat cmd

Execute commands automatically on events.

When cmd is not given, list all commands defined for the given events and pat. When ! is given, delete the matching commands rather than listing them.

When cmd is given, add it to the list of commands to be executed when events occur for pages matching the regular expression pat. If pat is preceded by an unquoted !, then the autocommand is executed only for pages not matching the following regular expression. If the -javascript (short name -js) option is given, cmd is interpreted as JavaScript code. Otherwise, it is interpreted as an Ex command.

This behavior differs from Vim's implementation in that pat is a regular expression rather than a glob.

Available events:

For Ex cmds, the following keywords are replaced with the appropriate value before the commands are executed. For JavaScript commands, they may be accessed as ordinary variables, sans angle brackets.

:doautoa :doautoall :doautoall event url

Apply all event autocommands matching the specified url to all buffers. If no url is specified use the current URL.

:do :doautocmd :doautocmd event url

Apply all autocommands matching the specified url to the current buffer. If no url is specified use the current URL.

Examples

Enable passthrough mode on all Google sites:

:autocmd LocationChange google\.com -js modes.set(modes.PASS_THROUGH)

Enable passthrough mode on some Google sites:

:autocmd LocationChange (www|mail)\.google\.com -js modes.set(modes.PASS_THROUGH)

Set the filetype to mail when editing email at Gmail:

:autocmd LocationChange !mail\.google\.com gvim -f :autocmd LocationChange mail\.google\.com gvim -f -c 'set ft=mail'