O'Reilly logo

The VimL Primer by Benjamin Klein

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required