【问题标题】:Use $dir with backslashes escaped in VSCode settings在 VSCode 设置中使用带有反斜杠的 $dir
【发布时间】:2017-09-08 08:54:01
【问题描述】:

我将Code Runner 用于[VSCode][2] (Visual Studio Code),我正在尝试更改C++ 的运行命令。

我的 settings.json 文件中有以下设置:

// Set the executor of each language.
"code-runner.executorMap": {
    // ...

    "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"

    // ...
}

然后当我按下CTRL SHIFT P 并在Run Code 上按下回车键来运行我当前的 C++ 文件时,它会生成以下要运行的命令:

cd "c:\C++\" && g++ main.cpp -o main && "c:\C++\"main

然后命令的输出是:

bash: cd: c:\C++" && g++ main.cpp -o main && c:C++"main: No such file or directory

这是因为正如您在正在运行的命令中看到的那样,它试图 CD 到 "c:\C++\",但 \ 字符没有被转义,这会导致命令失败。 如果该命令将所有\ 字符转义为"c:\\C++\\",它将正确运行。

我正在为我的集成终端使用 git bash 控制台。

如何解决此问题并转义从我的settings.json 文件中的$dir 变量中检索到的路径?

【问题讨论】:

  • 你是如何解决这个问题的?
  • @tusharRawat 抱歉,我在 2017 年问过这个问题,我不记得了。

标签: bash visual-studio-code escaping vscode-code-runner


【解决方案1】:

试试 $dirWithoutTrailingSlash 变量而不是 $dir。

如果这不起作用,您可以尝试在位于%VSCODE_INSTALL%\Data\code\extensions\formulahendry.coderunner-%VERSION%\out\src\codeManager.js 中创建自己的变量。

向位于第 246 行附近的 placeholders 数组添加条目。例如:

{ regex: /\$dirWithoutQuotesNorTrailingSlash/g, replaceValue: this.getCodeFileDirWithoutTrailingSlash() }

我用来代替的

{ regex: /\$dirWithoutTrailingSlash/g, replaceValue: this.quoteFileName(this.getCodeFileDirWithoutTrailingSlash()) }

因为我不能在类路径中使用带有显式引号的变量。 您还可以使用正则表达式定义您自己的函数返回值(如 getCodeFileDirWithoutTrailingSlash)(大约第 222 行)。

【讨论】:

    猜你喜欢
    • 2012-11-16
    • 2021-05-21
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多