[英]How to specify $HOME folder in sublime-build file?
I am using a custom build system in Sublime Text 3 for a C++ project. 我在Sublime Text 3中使用自定义构建系统来进行C ++项目。 One of the lines looks like
其中一条线看起来像
"-I${HOME}/my_file/include"
The problem is that ${HOME}
does not expand to the actual /home/user_x
, but expands to an empty string ''
. 问题是
${HOME}
不会扩展到实际的/home/user_x
,而是扩展为空字符串''
。 I cannot find any way of specifying the HOME
folder elegantly, without the need to write the actual path. 我找不到任何方式来优雅地指定
HOME
文件夹,而无需编写实际路径。 Is there a way of doing this? 有办法做到这一点吗? Ie, to expand the UNIX
$HOME
variable to the actual location of the home folder? 即,将UNIX
$HOME
变量扩展到主文件夹的实际位置?
What you're running afoul of here is that the cmd
, shell_cmd
and working_dir
arguments are all subject to variable expansion in build systems. 你在这里遇到的是
cmd
, shell_cmd
和working_dir
参数都在构建系统中受到变量扩展的影响 。
What's not outlined there is that anything of this format is treated as a variable when the expansion happens, and unknown variables are expanded to an empty string (ie no value). 没有概述的是,当扩展发生时,这种格式的任何内容都被视为变量,未知变量被扩展为空字符串(即没有值)。
As such, Sublime sees your ${HOME}
and tries to expand it, which unhelpfully removes it's value from the string. 因此,Sublime看到你的
${HOME}
并尝试扩展它,这无助于从字符串中删除它的值。
The easiest and least invasive way around this is to quote the $
character in the variable expansion so that Sublime will ignore it and pass it through, where the shell can expand it as appropriate: 最容易和最少侵入性的方法是引用变量扩展中的
$
字符,以便Sublime忽略它并传递它,shell可以根据需要扩展它:
{
"shell_cmd": "echo Home is \\${HOME}"
}
or 要么
{
"cmd": "echo Home is \\${HOME}",
"shell": true
}
Note that as seen in these examples you need to use either shell_cmd
with a complete command string or cmd
with a command string in combination with shell
being set to true
in order for this to work. 请注意,如这些示例中所示,您需要使用带有完整命令字符串的
shell_cmd
或带有命令字符串的cmd
以及将shell
设置为true
以使其起作用的组合。
Note also that the quoting appears to be doubled because the build file is JSON. 另请注意,引用似乎加倍,因为构建文件是JSON。 So when the build is loaded,
\\\\${HOME}
is internally seen by Sublime as \\${HOME}
(ie it "eats" the first level of quoting), and the final quoting causes Sublime to treat the $
as a literal and not special so that it will be passed to the shell, which expands it itself. 因此,当构建加载时,Sublime内部将
\\\\${HOME}
视为\\${HOME}
(即它“吃掉”第一级引用),最终引用会导致Sublime将$
视为文字而不是特别的,所以它将被传递给shell,它自己扩展它。
You can try writing your commands to a shell script and running the script from Sublime. 您可以尝试将命令写入shell脚本并从Sublime运行脚本。 I'm not sure if Sublime supports this feature, but shell script should find and expand $HOME and any other variables.
我不确定Sublime是否支持此功能,但shell脚本应该找到并扩展$ HOME和任何其他变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.