Automatic commands

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

:au :autocmd :autocmd! events filter cmd

Execute commands automatically on events.

When cmd is not given, list all commands defined for the given events and filter. 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 comma-separated list of site-filters, filter. If the -javascript (short name -js) option is given, cmd is interpreted as JavaScript code. Otherwise, it is interpreted as an Ex command.

If the -group=group flag is given, add this autocommand to the named group. Any filters for group apply in addition to filter. When listing commands this limits the output to the specified group.

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 Pass Through mode on all Google sites:

:autocmd LocationChange google.com :normal!

Enable Pass Through mode on some Google sites:

:autocmd LocationChange www.google.com,mail.google.com :normal!

or

:autocmd LocationChange ^https?://(www|mail)\.google\.com/ :normal!

Set the filetype to mail when editing email at Gmail:

:autocmd LocationChange !mail.google.com,* :autocmd LocationChange mail.google.com gvim -f -c 'set ft=mail' +<line>