Skip to Content
Modern Vim
book

Modern Vim

by Drew Neil
May 2018
Intermediate to advanced
168 pages
4h 2m
English
Pragmatic Bookshelf
Content preview from Modern Vim
Tip 26Using Autocommands to Respond to Events

This tip comes in two parts. In the first part, you’ll try out some examples of autocommands to understand how they work. In the second part, you’ll look at some real examples of how autocommands can be useful, collected from other tips in this book.

Understanding Autocommands

Here’s a short sample Vim script that defines two autocommands:

 augroup demo
  autocmd!
  autocmd BufReadPost * echo ​'Reading: '​ . expand(​'<afile>'​)
  autocmd BufWritePost * echo ​'Writing: '​ . expand(​'<afile>'​)
 augroup END

Before we look at the syntax of these autocommands, let’s see how they behave. Open that script in a fresh instance of Vim:

=> $ cd code/autocmd-demo/
=> $ vim ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Vim Masterclass

Vim Masterclass

Jason Cannon
Mastering Vim

Mastering Vim

Ruslan Osipov

Publisher Resources

ISBN: 9781680506006Errata Page