Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a variable as the subject of autocmd FileType

Tags:

vim

vim-plugin

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?

like image 633
Michael Robinson Avatar asked Jan 10 '23 05:01

Michael Robinson


1 Answers

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()"
like image 141
romainl Avatar answered Jan 11 '23 17:01

romainl