在VSCode中配置默认shell和参数命令的新方法是什么?

31

几天前,我猜测微软发布了VSCode的新更新。当我尝试构建我的esp idf项目时,它没有工作,因为在执行“特殊”的项目构建命令之前,它依赖于终端中运行的一个命令。 我得出结论,允许自动执行此操作的以下设置位于“设置”中的main.code.workspace文件中:

    "terminal.integrated.shell.windows": "cmd.exe",
    "terminal.integrated.shellArgs.windows": [

        "/k", 
        "C:/Coding/ESP32/esp-idf/export.bat"
    ],

错误信息如下:

此方法已弃用,配置默认终端的新推荐方法是在 #terminal.integrated.profiles.osx# 中创建终端配置文件,并将其配置文件名称设置为 #terminal.integrated.defaultProfile.osx# 中的默认值。这目前将优先于新配置文件设置,但这将来会发生改变。

如何以新方式配置启动时的默认终端并运行此命令?

5个回答

49
在 settings.json 文件中,我们需要拥有以下内容。
"terminal.integrated.profiles.windows": {
    "PowerShell": {
        "source": "PowerShell",
        "icon": "terminal-powershell"
    },
    "Command Prompt": {
        "path": [
            "${env:windir}\\Sysnative\\cmd.exe",
            "${env:windir}\\System32\\cmd.exe"
        ],
        "args": [],
        "icon": "terminal-cmd"
    },
    "Git Bash": {
        "source": "Git Bash"
    }
},

通过添加来设置默认终端

"terminal.integrated.defaultProfile.windows": "Command Prompt",

我们可以通过按下Ctrl + Shift + P并搜索“Terminal: Select Default Profile”,然后选择所需的shell来实现此目标。

但是,虽然不推荐使用,您当前的设置应该可以正常工作。


3
谢谢,我已将此代码块添加到我的 main.code-workspace 文件中,这样每次加载工作区时都会执行。但如何使其在启动时自动输入以下两个命令:"/k","C:/Coding/ESP32/esp-idf/export.bat"? - MikeLemo
2
我相信你可以像这个评论中所示,将它添加到args字段中,以便每次执行。 - sunsun
1
这是正确的答案,但更简单的方法是因为你只需要 "terminal.integrated.defaultProfile.windows": "Command Prompt"。这是因为第一个代码块中写的都是默认设置(除非你想从默认设置中更改某些内容...)。 - YoniChechik
"Git Bash" 带空格无法工作。请参见:https://dev.to/andrewriveradev/how-to-set-git-bash-as-integrated-terminal-in-vscode-2k31 - JoePC

8

我已经解决了这个问题:

1)将Git/bin路径添加到环境变量中

2)重新启动 VSC 并在 settings.json 中添加以下内容:

"terminal.integrated.profiles.windows": {
    "PowerShell": { "source": "PowerShell", "icon": "terminal-powershell" },
    "Command Prompt": {
      "path": [
        "${env:windir}\\Sysnative\\cmd.exe",
        "${env:windir}\\System32\\cmd.exe"
      ],
      "icon": "terminal-cmd"
    },
    "GitBash": {
      "path": ["F:\\Program files\\Git\\bin\\bash.exe"],
      "icon": "terminal-bash"
    },
},
"terminal.integrated.defaultProfile.windows": "GitBash",

只需在路径中替换“GitBash”为您的git bash路径。

3)移除旧设置

对于我来说,它是

"terminal.integrated.shell.windows": "F:\\Program files\\Git\\bin\\bash.exe"

4)保存设置,通过按删除键关闭VSC终端,重新启动VSC。

希望这个方法有效!


非常感谢,伙计。这是最简单的方法。真的很感激。不知道为什么它不是批准的答案。 - Faraz Zaidi
人们必须注意程序的路径。就像我这里,我正在使用C驱动器,我所做的只是: ----> 修改上面的路径以适应我的系统路径,即 "path": ["C:\\Program files\\Git\\bin\\bash.exe"] - Comsavvy

1

在 Windows 11 下使用 VSCode 1.76.2,

我在用户设置.json 中添加了以下内容,以便在终端列表中使用 Git Bash:

"terminal.integrated.profiles.windows": {
    "GitBash": {
      "path": ["C:\\Git\\Git\\bin\\bash.exe"],
      "icon": "terminal-bash"
    },
},
"terminal.integrated.defaultProfile.windows": "GitBash",

如果您不想将此 bash 终端设置为默认值,请删除最后一行。

注意:在“GitBash”名称中不要添加任何空格!


1
我最近在Windows上升级到VSCode 1.60,并遇到了类似的问题。我已经将上述提到的“GitBash”选项添加到配置文件中,但当我尝试像这样使用它时:
   "terminal.integrated.defaultProfile.windows": "GitBash",

VSCode 抱怨 "GitBash" 不是一个有效的值:

Value is not accepted. Valid values: "PowerShell", "Command Prompt", "JavaScript Debug Terminal".
所以我更新了“命令提示符”配置文件,将其指向我的git bash,这样就可以工作了。
    "terminal.integrated.profiles.windows": {
    "PowerShell": {
        "source": "PowerShell",
        "icon": "terminal-powershell"
      },
      "Command Prompt": {
        "path": [
          "C:\\<PATH TO MY bash.exe>",
        ],
        "args": [],
        "icon": "terminal-bash"
      }
  },
"terminal.integrated.defaultProfile.windows": "Command Prompt",

0

2
你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心找到有关如何编写良好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接