Vim tip: autocommands

Whenever you find yourself thinking “I wish Vim could do this automagically for me”, you probably are thinking about autocommands. With autocommands, autocmd for short or au for lazy people, will let you tell Vim, “Hey, use this callback when an event occurs”.

The basic structure is pretty simple: “autocmd Event FileType Action”. So, for example, “autocmd BufEnter *.txt call Rot13()” would tell vim to set a callback on BufEnter, that is whenever you change buffers, for all *.txt files, which will rot13 your text. Feel free to use this for actually useful things, like spell checking or auto indenting.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s