简体   繁体   English

如何在sublime-build文件中指定$ HOME文件夹?

[英]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. 你在这里遇到的是cmdshell_cmdworking_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.

相关问题 将文件夹指针添加到默认的.sublime-build文件(Sublime Text 3,C ++) - Adding folder pointers to default .sublime-build files (Sublime Text 3, c++) 确认Windows上.sublime-build正在查看的位置(Sublime Text 3,C ++) - Confirm location that .sublime-build is looking at (Sublime Text 3, C++) on Windows 使用sublime-build时无法运行C ++程序,从终端运行时可以正常工作 - Cannot run C++ program when using sublime-build, works fine when running from terminal 如何为 sublime 文本设置 cpp 构建文件? - How to setup cpp build file for sublime text? 如何在用户的主文件夹中打开文件 - how to open a file in user's home folder Sublime Text,当我在 Mac 上使用 fstream 编写文件时,我的文件存储在我的主文件夹中 - Sublime Text, my file is stored in my home folder when I write a file using fstream on Mac 如何为一个文件指定构建设置? - How to specify build settings for one file? 如何在主目录中创建文件夹? - How to create a folder in the home directory? 如何在 Sublime Text 的构建系统中使用 Mingw - How to use Mingw with a build system in Sublime Text 如何在 bazel BUILD 文件中指定 -std=c++11 选项? - How to specify -std=c++11 option in bazel BUILD file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM