2

What are some of the ways to construct autocommand groups in the .vimrc? What is the benefit of grouping autocommands? Can you provide some materials on proper use of autocommand groups? Any help here is greatly appreciated. Thanks!

1

One of the most common uses of autocommands is to allow you to execute commands only on specific filetypes, or to customize a keymapping to execute differently in different filetypes.

For example, I could do

augroup filetype_ruby
    autocmd!
    autocmd FileType ruby nnoremap <buffer> <localleader>t :!rake test
augroup END

augroup filetype_erlang
    autocmd!
    autocmd FileType erlang nnoremap <buffer> <localleader>t :!rebar eunit
augroup END

What this example does is it maps the <localleader>t hotkey to running tests through the appropriate build tool - using rake in a ruby project, and rebar in an erlang project.

This is especially useful for developers making plugins specific to a certain programming language, like vim-ruby or any of tpope's ruby plugins.

There are other events you can use besides FileType - there's:

  • BufWrite (when a buffer is written)
  • FileAppendCmd ( when a file is appended to)
  • FileAppendPost

and many others. Basically, if you want vim to execute specific commands when these events are fired, augroups are the way to go.

Some good references for reading and learning:

Not the answer you're looking for? Browse other questions tagged or ask your own question.