如何在Visual Studio Code中更改集成终端

77
我想把集成终端改成Cmder。我在Windows 8.1上使用Visual Studio Code。
我查看了文档和首选项文件,但是我对要更改的哪一行感到困惑。

外部终端

// Customizes which terminal to run on Windows.
"terminal.external.windowsExec": "%COMSPEC%",

// Customizes which terminal application to run on OS X.
"terminal.external.osxExec": "Terminal.app",

// Customizes which terminal to run on Linux.
"terminal.external.linuxExec": "xterm",

综合终端

// The path of the shell that the terminal uses on Linux.
"terminal.integrated.shell.linux": "sh",

// The command line arguments to use when on the Linux terminal.
"terminal.integrated.shellArgs.linux": [],

// The path of the shell that the terminal uses on OS X.
"terminal.integrated.shell.osx": "sh",

// The command line arguments to use when on the OS X terminal.
"terminal.integrated.shellArgs.osx": [],

// The path of the shell that the terminal uses on Windows. When using shells shipped with Windows (cmd, PowerShell or Bash on Ubuntu), prefer C:\Windows\sysnative over C:\Windows\System32 to use the 64-bit versions.
"terminal.integrated.shell.windows": "C:\\Windows\\system32\\cmd.exe",

// The command line arguments to use when on the Windows terminal.
"terminal.integrated.shellArgs.windows": [],

// Controls the font family of the terminal, this defaults to editor.fontFamily's value.
"terminal.integrated.fontFamily": "",

// Controls whether font ligatures are enabled in the terminal.
"terminal.integrated.fontLigatures": false,

// Controls the font size in pixels of the terminal, this defaults to editor.fontSize's value.
"terminal.integrated.fontSize": 0,

// Controls the line height of the terminal, this number is multipled by the terminal font size to get the actual line-height in pixels.
"terminal.integrated.lineHeight": 1.2,

// Controls whether the terminal cursor blinks.
"terminal.integrated.cursorBlinking": false,

// Controls whether locale variables are set at startup of the terminal, this defaults to true on OS X, false on other platforms.
"terminal.integrated.setLocaleVariables": false,

// A set of command IDs whose keybindings will not be sent to the shell and instead always be handled by Code. This allows the use of keybindings that would normally be consumed by the shell to act the same as when the terminal is not focused, for example ctrl+p to launch Quick Open.
"terminal.integrated.commandsToSkipShell": [
    "editor.action.toggleTabFocusMode",
    "workbench.action.debug.continue",
    "workbench.action.debug.restart",
    "workbench.action.debug.run",
    "workbench.action.debug.start",
    "workbench.action.debug.stop",
    "workbench.action.quickOpen",
    "workbench.action.showCommands",
    "workbench.action.terminal.clear",
    "workbench.action.terminal.copySelection",
    "workbench.action.terminal.focus",
    "workbench.action.terminal.focusNext",
    "workbench.action.terminal.focusPrevious",
    "workbench.action.terminal.kill",
    "workbench.action.terminal.new",
    "workbench.action.terminal.paste",
    "workbench.action.terminal.runSelectedText",
    "workbench.action.terminal.scrollDown",
    "workbench.action.terminal.scrollDownPage",
    "workbench.action.terminal.scrollToBottom",
    "workbench.action.terminal.scrollToTop",
    "workbench.action.terminal.scrollUp",
    "workbench.action.terminal.scrollUpPage",
    "workbench.action.terminal.toggleTerminal"
],

11个回答

51

要更改 Windows 上的集成终端,只需更改 terminal.integrated.shell.windows 行:

  1. 打开 VS 用户设置(首选项 > 用户设置)。这将打开两个并排的文档。
  2. 如果右侧的用户设置文档中没有该设置,请添加一个新的 "terminal.integrated.shell.windows": "C:\\Bin\\Cmder\\Cmder.exe" 设置。这样你就不会直接编辑默认设置,而是添加到其中。
  3. 保存用户设置文件。

然后,默认情况下可以使用键 Ctrl+backtick 访问它。


10
仍然会弹出Cmder窗口,而不是集成到其他窗口中。 - xyhhx
4
如此问题所述,https://github.com/Microsoft/vscode/issues/12006,由于 Cmder 是终端仿真器,因此无法集成。您可以使用 Clink 替代。 - mbernardeau
4
我知道。这是为那些发现这个答案有困惑的人澄清一下。 - xyhhx
2
不要错过@khernand的答案,他使得Cmder能够按照我希望的方式集成到这里。 - consideRatio
2
只需将设置 terminal.integrated.shell.linux 设置为您终端可执行文件的路径即可。 - mbernardeau
显示剩余4条评论

43

在Visual Studio Code中,可以实现将Cmder终端集成(而不是弹出窗口)。

操作步骤如下:

  1. 创建一个名为"CMDER_ROOT"的环境变量,指向你的Cmder目录。

  2. 在Visual Studio Code的(首选项用户设置)中,添加以下设置:

    "terminal.integrated.shell.windows": "cmd.exe"

    "terminal.integrated.shellArgs.windows": ["/k", "%CMDER_ROOT%\\vendor\\init.bat"]


10
这适用于Windows 10,但我需要更改参数路径中的反斜杠:["/k", "%CMDER_ROOT%\\vendor\\init.bat"] - Lodybo
3
使用 @Lodybo 的建议非常好!谢谢! - Ben Force
2
请注意,我实际上必须创建一个CMDER_ROOT环境变量。仅仅用init.bat的路径替换配置文件中的%CMDER_ROOT%是行不通的。 - yoyoyoyosef
3
我唯一的问题是,在集成终端中命令历史记录没有被保存。我无法重新选择该命令:a) 在外部的cmder.exe shell中;b) 在重新启动 VS Code之后。您有任何解决方法吗? - ford04

33

你可以通过快捷键 Ctrl + Shift + P 并输入 "default" 来快速完成。它将显示一个选项,上面写着:

终端:选择默认 Shell

然后它会显示所有可用的终端。


2
很好的建议。使用这个,我轻松地从cmd.exe切换到了bash。简单易行。 - Barry McDermid
3
有两种终端 - 控制台和集成控制台。后者用于调试代码。这个回答并没有涉及到这点,因此它实际上没有回答问题。 - mark

9

官方文档中:

在Windows上正确配置您的shell只是找到正确的可执行文件并更新设置的问题。下面是常见shell可执行文件及其默认位置的列表。

还有一个方便的命令选择默认shell,可以通过命令面板访问,它可以检测并为您设置这个。

所以您可以使用Ctrl + Shift + P打开命令面板,使用命令选择默认shell,然后它会显示所有可用的命令行界面。选择您想要的任何一个,Visual Studio Code会自动将其设置为默认的集成终端。

如果您想手动设置,请找到您的CLI的可执行文件位置,并打开Visual Studio Code的用户设置(Ctrl + `),然后设置:

"terminal.integrated.shell.windows":"path/to/executable.exe"

在Windows 7上使用Git Bash的示例:

"terminal.integrated.shell.windows":"C:\\Users\\stldev03\\AppData\\Local\\Programs\\Git\\bin\\bash.exe",

4

对于OP的终端Cmder,有一个集成指南,还有Visual Studio Code文档中的提示链接

如果你想在切换到Cmder后使用Visual Studio Code任务并且遇到问题,可以参考khernand's答案。将以下内容复制到你的settings.json文件中:

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

"terminal.integrated.env.windows": {
  "CMDER_ROOT": "[cmder_root]" // replace [cmder_root] with your cmder path
},
"terminal.integrated.shellArgs.windows": [
  "/k",
  "%CMDER_ROOT%\\vendor\\bin\\vscode_init.cmd" // <-- this is the relevant change
  // OLD: "%CMDER_ROOT%\\vendor\\init.bat"
],

调用的文件将打开Cmder作为集成终端,并切换到cmd进行任务。在这里查看源代码here。因此,您可以省略在tasks.json中配置单独的终端来使任务正常工作。

从VS Code 1.38开始,还有一个"terminal.integrated.automationShell.windows"设置,它允许您全局设置任务的终端并避免与Cmder相关的问题。

"terminal.integrated.automationShell.windows": "cmd.exe"

4

在被接受的答案中所解释的方法已经过时,现在配置默认shell的新推荐方式是通过在#terminal.integrated.profiles.windows#中创建一个终端配置文件,并将其配置文件名称设置为#terminal.integrated.defaultProfile.windows#的默认值。

目前旧的方法优先于新的配置文件设置,但这将来会改变。

参考文档中关于PowerShell的示例:

{
  "terminal.integrated.profiles.windows": {
    "My PowerShell": {
      "path": "pwsh.exe",
      "args": ["-noexit", "-file", "${env:APPDATA}PowerShellmy-init-script.ps1"]
    }
  },
  "terminal.integrated.defaultProfile.windows": "My PowerShell"
}

2
我通过“设置”→“终端”→“集成”→“Shell: Linux”成功了。
从那里,我将shell的路径从默认的“/bin/bash”编辑为“/bin/zsh”。
此外,还有适用于OS X和Windows的选项。

Screen capture of Visual Studio Code settings view

@charlieParker - 这是我在命令面板中看到的可用命令。

Enter image description here


2
@CharlieParker,我没有看到编辑 shell 选项的选项,大多数可用命令与集成终端相关的操作都是在已定义的终端中执行的。我在我的原始答案中添加了一个屏幕截图。你使用我使用的 preferences>settings 路线有问题吗? - MCGRAW

2

如果你想将外部终端更改为新的Windows终端,可以参考这篇文章


2
更改外部终端

Enter image description here

Windows Terminal,其他人也提到过它,是Alacritty的一个替代品,它是一个终端(仿真器)。

正如Visual Studio Code中所述,Cmder是一个类似于PowerShell或Bash的shell。

Enter image description here

"terminal.integrated.profiles.windows": {
    "cmder": {
        //  "path": "F:\\cmder\\Cmder.exe",  // 这样会开external terminal
      "path": "C:\\WINDOWS\\System32\\cmd.exe",
      "args": ["/K", "F:\\cmder\\vendor\\bin\\vscode_init.cmd"]
    }
  },
"terminal.integrated.profiles.linux": { "zsh": { "path": "zsh" }, },
"terminal.integrated.defaultProfile.windows": "PowerShell",

enter image description here

Cmder的存储库中的陈述是误导性的。

Enter image description here

终端下的不同Shell:

Enter image description here

如果你想将外部终端更改为新的Windows终端,以下是操作步骤。

1
以下方法对我有效:
  1. 打开 设置 → 这将打开 settings.json
  2. 输入 terminal.integrated.windows.shell
  3. 点击右上角的 {}。这将打开一个编辑器,可以在其中覆盖此设置。
  4. 将值设置为 terminal.integrated.windows.shell: C:\\Users\\<user_name>\\Softwares\\Git\\bin\\bash.exe
  5. 按下 Ctrl + S

尝试打开一个新的终端。它应该以集成模式中的 Bash 编辑器打开。


1
Bash编辑器是什么? - Peter Mortensen

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