Setting environment variables with system()
and export
will not work. Environment variables set this way will only exist in the shell (or process) where they're defined and their descendents, so setting them in a shell spawned by system()
will not make them available in Vim itself.
You can set environment variables directly in Vim, by using the :let
command and using the $
prefix to refer to the variables (that indicates to Vim that you're accessing or setting environment variables.)
So this should work:
let $TR_SOURCE = 'dyu'
Or even:
let $TR_SOURCE = g:tr_source
That should allow you to refer to them from the :source
command in your autocmd's. Note that these variables will also be accessible from any commands or shells you spawn from Vim, be it from the system()
function or the :!
command or some other command that takes an expression involving !
to run an external command.
Another possibility is to use Vim global variables directly, but in that case you'll need to use :execute
, since the :source
command doesn't interpret Vim variables directly (only environment variables.) You'll actually also need it for the whole autocmd
since you need it in its pattern too.
You can refer to the Vim variables directly in your autocmd's with:
execute 'autocmd BufNewFile ' . g:tr_source . '/*/*.adoc source ~/.config/nvim/' . g:tr_source . '.vim'
execute 'autocmd BufNewFile ' . g:tr_target . '/*/*.adoc source ~/.config/nvim/' . g:tr_target . '.vim'
If you don't mind the small environment pollution from setting them as environment variables (which might even come useful as you can refer to them in interactive usage), it looks like the option of using environment variables, setting them with :let
, is a much more convenient approach.