Use variables/lists as an autocmd pattern

403 views Asked by At

Is it possible to use variables or lists to pass patterns to vim's autocmd?

Here is an example:

" Automatically source .vimrc if there have been any changes
let candidates = [ ".vimrc", "_vimrc", "vimrc", ".gvimrc", "_gvimrc", "gvimrc" ]

augroup AutoSourceVimrc
    autocmd!
    autocmd BufWritePost candidates so $MYVIMRC | if has('gui_running') | so $MYGVIMRC | endif
augroup END

The example above does not work - unfortunately.

1

There are 1 answers

2
romainl On BEST ANSWER

Here is a simpler version of your command:

augroup AutoSourceVimrc
    autocmd!
    autocmd BufWritePost *vimrc execute "source " . expand("<afile>")
augroup END