autocmd BufNewFile,BufRead *.foo set filetype=html
BufNewFile,BufRead
is the list of events that trigger this autocommand.*.foo
is the pattern we want to match against the data returned by the event.set filetype=html
is the command we want to execute when the pattern matches the data returned by the event.
autocmd[!] [groupname] BufNewFile,BufRead *.foo [nested] set filetype=html
!
removes every autocommand of the current group for the given events and pattern.groupname
puts the autocommand in groupgroupname
.nested
allows autocommands to trigger other events and thus other autocommands.
As the online documentation says:
`:autocmd` adds to the list of autocommands regardless of whether they are
already present. When your .vimrc file is sourced twice, the autocommands
will appear twice. To avoid this, define your autocommands in a group, so
that you can easily clear them:
augroup vimrc
autocmd! " Remove all vimrc autocommands
au BufNewFile,BufRead *.html so <sfile>:h/html.vim
augroup END
Piling-up autocommands is in fact a very common cause of performance issues. Fortunately, fixing our hare-brained autocommand is rather easy:
augroup mygroup
autocmd!
autocmd BufNewFile,BufRead *.foo set filetype=html
augroup END
But do we have to put an augroup
and an autocmd!
around every freaking autocmd
? Well, no. Of course we don't. The idea is to assign every autocommand to a group that's properly reset when you :source $MYVIMRC
. This can be done like this…
-
Place your group somewhere near the top of your
vimrc
:augroup mygroup ^^^^^^^ autocmd! augroup END
-
Use that group in your autocommands:
autocmd mygroup BufNewFile,BufRead *.foo set filetype=html ^^^^^^^
By trying your tip, I get
E471: Argument required
. It's pointing to the second line here:Tested with a clean configuration file (the only contents of $MYVIMRC are the lines above).
NVIM v0.3.1
. andVIM v8.1.542