autocommands.xml

autocommands
Automatic commands

Contents

  1. Examples

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

:au:autocmd
:au[tocmd][!] [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 autocmd 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]:

BookmarkAdd
Triggered after a page is bookmarked
BookmarkChange
Triggered after a page's bookmark is changed
BookmarkRemove
Triggered after a page's bookmark is removed
ColorScheme
Triggered after a color scheme has been loaded
DOMLoad
Triggered when a page's DOM content has fully loaded
DownloadPost
Triggered when a download has completed
Fullscreen
Triggered when the browser's fullscreen state changes
LocationChange
Triggered when changing tabs or when navigating to a new location
PageLoadPre
Triggered after a page load is initiated
PageLoad
Triggered when a page gets (re)loaded/opened
PrivateMode
Triggered when private browsing mode is activated or deactivated
Sanitize
Triggered when private data are sanitized
ShellCmdPost
Triggered after executing a shell command with :!{cmd}
Enter
Triggered after Firefox starts
LeavePre
Triggered before exiting Firefox, just before destroying each module
Leave
Triggered before exiting Firefox

For Ex [cmd]s, 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.

<bookmark>
The JavaScript bookmark object. Only for Bookmark*.
<changed>
The name of the property that has changed. Only for BookmarkChange.
<doc>
The document for which the event occurred. Only for DOMLoad, PageLoad and PageLoadPre.
<file>
The target destination of a download. Only for DownloadPost.
<icon>
The icon associated with <url>. Only for Bookmark*.
<keyword>
The keywords applied to the bookmark. Only for BookmarkChange, BookmarkRemove.
<name>
The name of the item. Only for ColorScheme and Sanitize.
<size>
The size of a downloaded file. Only for DownloadPost.
<state>
The new state. Only for Fullscreen and PrivateMode.
<tab>
The tab in which the event occurred. Only for DOMLoad, PageLoad and PageLoadPre.
<tags>
The tags applied to <url>. Only for Bookmark*.
<title>
The page, bookmark or download title.
<url>
The URL against which the event was selected.
<win>
The window for which the event occurred. Only for DOMLoad, PageLoad and PageLoadPre.

:doautoa:doautoall
:doautoa[ll] {event} [url]

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

:do:doautocmd
:do[autocmd] {event} [url]

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

autocmd-examples
Examples

Enable Pass Through mode on all Google sites:

:autocmd LocationChange google.com :normal! <C-z>

Enable Pass Through mode on some Google sites:

:autocmd LocationChange ^https?://(www|mail)\.google\.com/ :normal! <C-z>

or

:autocmd LocationChange www.google.com,mail.google.com :normal! <C-z>

Set the filetype to mail when editing email at Gmail:

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