简体   繁体   English

gVim访问环境变量,带有特殊字符

[英]gVim access environment variable with special characters

PortableApps automatically add environment variables when using their apps. PortableApps在使用其应用程序时会自动添加环境变量 However, while using gVimPortable to access a variable with a special character in the name, I get errors. 但是,在使用gVimPortable访问名称中带有特殊字符的变量时,出现错误。

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. PortableApps环境变量似乎都在那里。 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. 对于Vim,环境变量只能包含字母,数字和下划线。 From :h expand-env : 来自: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. 因此,使用诸如set something=$PortableApps.comDocuments类的表达式,Vim仅将$PortableApps视为环境变量.comDocuments是要附加到.comDocuments的字符串。 With echo $PortableApps.comDocuments , . 使用echo $PortableApps.comDocuments . is the string concatenation operator , so Vim tries to take the value of $PortableApps and the variable comDocuments and join them. 字符串连接运算符 ,因此Vim尝试获取$PortableApps和变量comDocuments的值并将其comDocuments With echo $PortableApps\\.comDocuments , it sees the environment varible $PortableApps , followed by something it can't make sense of: \\.comDocuments . 通过echo $PortableApps\\.comDocuments ,它可以看到环境变量$PortableApps ,其后是一些无法理解的内容: \\.comDocuments

If you have access to Vim's Python or Perl interfaces, you can use them instead. 如果可以访问Vim的Python或Perl接口,则可以改用它们。 For example, after starting Vim with: 例如,用以下命令启动Vim:

env foo.bar=blah vim

Both of the following gave the output blah : 以下两个都给输出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变量:

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

Then, you could use foobar like any other Vim variable: 然后,您可以像其他任何Vim变量一样使用foobar

:echo foobar
blah

Of course, being on Windows, you would have to install Python or Perl to use this. 当然,在Windows上,您必须安装Python或Perl才能使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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