More to write about after spending a weekend hacking on vim plugins.

Autocmd is probably one of the most powerful features of vim, but also one of the easiest to mis-use. If you ever end up copy-and-pasting someone else’s autocmd examples, beware!

Here’s one that I had in my .vimrc forever

autocmd BufRead,BufNewFile,BufAdd *.php setlocal ft=php

Looks sane enough. Turns out BufAdd is a bad idea here. From the docs:

NOTE: When this autocommand is executed, the current buffer “%” may be different from the buffer being created “<afile>”

Oops, this means that this command will run in the context of the current buffer, not the new buffer being created. In practice, this meant that sometimes when I opened php files, the file I was currently looking at would get it’s filetype set to php, which was super confusing.

Leave a comment

Your email address will not be published. Required fields are marked *