You need to consider the timing of your plugin. When your plugin script is sourced during the startup of Vim, no buffer has yet been loaded, so &filetype
is empty. Therefore, something like this
let s:formatprgvarname = "g:formatprg_".&filetype
does not work! (For a filetype plugin (in ~/.vim/ftplugin/
), this is different; those are sourced only when the filetype has been detected. But as I understand you want a general-purpose plugin that considers the current filetype.)
Instead, do away with the script-local variable s:formatprgvarname
and resolve &filetype
at the point of action; i.e. when your autoformat functionality is triggered (by mapping or custom command). If you have no such trigger, you can hook into the FileType
event and set a (preferably buffer-local) variable then:
autocmd FileType * let b:formatprgvarname = "g:formatprg_".&filetype