Autocommands and Their Events

An autocommand is a command, or a series of commands, that Vim automatically executes when a particular condition occurs. The conditions are called events, and Vim 7.4 includes more than 80 events, which we can trigger by doing anything from creating a new buffer to changing the color scheme to losing the user’s interest (see :help UserGettingBored). We can use autocommands to have Vim automatically execute code when a user loads a certain kind of file, set a setting to a specific value, or trigger some other kind of event.

Let’s look at an example. This is a very basic autocommand:

 
autocmd​ ​VimLeave​ * echo ​'Bye!'​ | ​sleep​ 1000m

The keyword is autocmd. Following the keyword is the event name, VimLeave ...

Get The VimL Primer now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.