简体   繁体   中英

gVim access environment variable with special characters

PortableApps automatically add environment variables when using their apps. However, while using gVimPortable to access a variable with a special character in the name, I get errors.

For example: While executing

:echo $PortableApps\.comDocuments

or

:echo $PortableApps.comDocuments

I'm getting the following errors:

E15: Invalid expression: \.comDocuments

and

E121: Undefined variable: comDocuments
E15: Invalid expression: $PortableApps.comDocuments

I get similar problems when I do a

:echo $PAL:Drive

The PortableApps environment variables all seems to be there. I did a:

:echo $<C-D>

And they are listed.

How do I access these variables?

For Vim, environment variables can only contain alphabets, digits and underscore. From :h expand-env :

…  Any non-id character (not a letter, digit or '_') may
follow the environment variable name. That character and what follows is
appended to the value of the environment variable.  Examples: 
   :set term=$TERM.new
   :set path=/usr/$INCLUDE,$HOME/include,.

So, with an expression like set something=$PortableApps.comDocuments , Vim sees only $PortableApps as the environment variable - .comDocuments is a string to appended to it. With echo $PortableApps.comDocuments , . is the string concatenation operator , so Vim tries to take the value of $PortableApps and the variable comDocuments and join them. With echo $PortableApps\\.comDocuments , it sees the environment varible $PortableApps , followed by something it can't make sense of: \\.comDocuments .

If you have access to Vim's Python or Perl interfaces, you can use them instead. For example, after starting Vim with:

env foo.bar=blah vim

Both of the following gave the output blah :

:perl VIM::Msg($ENV{"foo.bar"})
:python import os; print os.environ['foo.bar']

Within Python, for example, you could assign the environment variable to a Vim variable:

:python vim.vars['foobar'] = os.environ['foo.bar']

Then, you could use foobar like any other Vim variable:

:echo foobar
blah

Of course, being on Windows, you would have to install Python or Perl to use this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM