If you put any character that is invalid in an environment variable name after the environment variable name, it will force the expansion. Example:
ec expand('$SHELL-$HOME')
Output:
/bin/zsh-/home/wilson
Then you can replace all instances of the invalid character with an underscore:
ec expand('$SHELL-$HOME')->substitute('-', '_', 'g')
Output:
/bin/zsh_/home/wilson
I recommend you define your own function in your vimrc for this
function! Exp(str)
return expand(a:str)->substitute('-', '_', 'g')
endf
Then run ec Exp('$SHELL-$HOME')
as needed.
Or you could even do:
function! Exp(str)
return expand(a:str->substitute('_', '-', 'g'))->substitute('-', '_', 'g')
endf
and run it as ec Exp('$SHELL_$HOME')
. The only concern is if the unexpanded string contains hyphens or if the values of the environment variables contain hyphens.
Of course, it doesn't have to be a hyphen, it could be anything other than an alphanumeric or underscore. You might come up with some weird character that will definitely never cause undesired results. You could even use a single closing curly brace and use expand('$SHELL}_$HOME')->substitute('}', '', 'g')
, as long as the result will definitely not contain any }
characters.
expand
if the variables are already known to vim (:help expr-env-expand
)::echo $ONE .. '_' .. $TWO