Search code examples
listvariablesvimautocmd

Use variables/lists as an autocmd pattern


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.


Solution

  • Here is a simpler version of your command:

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