4

Is it possible to use a variable as the subject of an autocmd?

I want to allow plugin users to define a list of FileTypes the plugin will operate on.

This requires that I set an autocommand for each of the given FileTypes.

The below code doesn't work:

let g:enabledFileTypes = ['javascript', 'vim']

autocmd FileType g:enabledFileTypes * call myFunction()

This does:

autocmd FileType javascript,vim * call myFunction()

Is it possible to declare an autocommand that uses a variable as the file type list?

Michael Robinson
  • 29,278
  • 12
  • 104
  • 130

1 Answers1

9

The * glob is useless in a FileType autocommand.

You can join() that list into a comma-separated string:

execute "autocmd FileType " . join(g:enabledFileTypes, ",") . " call myFunction()"
romainl
  • 186,200
  • 21
  • 280
  • 313