I'm trying to figure out how to use an environment variable name in a function, passed in as a function argument, i.e.:

function! myautoloadfunc#set_default_env_var(name, value)
  if empty($a:name)
     let $a:name = a:value

call myautoload#set_default_env_var('SOME_VAR', 'SOME_VALUE')

Obviously that doesn't work, I'm pretty sure I can do this with exec, but not sure if there's a more efficient way. Any suggestions?

Edit: It seems that the following works:

call myautoload#set_default_env_var('$SOME_VAR', 'SOME_VALUE')
  • Why do you think :execute is not efficient? – romainl Jun 21 '15 at 15:22
  • Ignorance, I suppose. I just don't know. – nfarrar Jun 21 '15 at 15:53
  • 1
    exec 'let $' . name . '=' . string(value) – lcd047 Jun 21 '15 at 15:54
  • Maybe the vim built-in function fnameescape() could help you? – Alex Stragies Aug 11 '15 at 14:02
  • @nfarrar If you found a working solution to your problem it would be better if you could post it as an answer rather than putting it in your question ;-) – statox Aug 12 '15 at 15:33

Browse other questions tagged or ask your own question.