更改 Visual Studio Code 的默认终端

404

我正在我的 Windows 10 电脑上使用 Visual Studio Code。我想要将默认终端从 Windows 的 PowerShell 更改为 BashUbuntu 上(在 Windows 上)。

我该如何做到这一点?


恐怕是指WSL(Windows Subsystem for Linux)吧。 - Peter Mortensen
12个回答

694
您还可以通过在 Visual Studio Code 中按下 F1 键并输入/选择 Terminal: Select Default Profile(或旧版 Visual Studio Code 中的 Terminal: Select Default Shell)来选择默认终端。

Terminal Selection 3

老的:

Terminal Selection

Terminal Selection


10
截至今天,在MacOS上,终端扩展程序的菜单中没有“选择默认Shell”的选项!幸运的是,使用https://dev59.com/C1cP5IYBdhLWcg3wa5XQ#49916667可行。 - sorin
4
我现在正在使用MacOS Catalina,"终端:选择默认Shell"对我有用。 - cakidnyc
更新的Linux和MAC答案:https://askubuntu.com/a/1118140/810954 - Kevin Danikowski
2
这个问题是否已经改变了呢?因为我在 Windows 版本 1.65.2 上做过这个操作,每次重新启动后都会出现 PowerShell 而不是 Git Bash。 - heretoinfinity
1
2023年:>select terminal profile是命令。 - sutan
对于 macOS 用户,只需按下键盘上的 SHIFT + Command + P 键,然后输入 terminal profile。选择 Terminal: Select Default Profile 并选择您喜欢的终端。 - Promise Preston

92
通过运行终端命令“Terminal: 选择默认配置文件”,您可以配置默认的集成终端。此命令也可以通过终端下拉菜单访问。

Enter image description here

查看终端基础知识

2
这是正确且最新的做法,但是在我的 VS Code v1.56.2 中无法工作。这个可以工作:"terminal.integrated.shell.windows": "C:\WINDOWS\System32\bash.exe"。 - Meeting Attender
当我尝试使用 "terminal.integrated.shell.windows": "C:\WINDOWS\System32\bash.exe" 时,我收到了这个警告 - "此方法已弃用,配置默认 shell 的新建议方式是创建终端配置文件"(VS Code v1.56.2)。 - arielhad
我收到了相同的弃用警告,但至少它能工作。就像我说的,在v1.56.2上推荐的方法对我不起作用。 - Meeting Attender
2
在我的VSCode 1.56.2中也无法工作。使用已弃用的方法,它可以工作。 - Drovosek
有一个小下拉菜单,上面写着“选择默认配置文件...”,可以将当前的shell设置为默认配置文件。 - Ray Foss
显示剩余2条评论

54
我只是在打开的终端中输入了以下关键字:
  1. powershell
  2. bash
  3. cmd
  4. node
  5. python(或python3)
详见下方图片(Visual Studio Code 版本 1.19.1,在 Windows 10 操作系统上):

Enter image description here

它也适用于Visual Studio Code Mac。我尝试过使用Visual Studio Code(版本1.20.1)。


1
似乎只有在选择bash作为默认shell时才能正常工作。 - winterlight
1
一直在寻找不改变默认设置、创建新的并将默认设置切换回PowerShell 的情况下,在Windows上打开我的WSL的方法。仅需键入"wsl"即可轻松解决问题。 - Ollie

39
打开菜单 文件首选项设置(或按下 Ctrl + ,)。然后点击右上角最左边的图标,"打开设置(JSON)"。

Screenshot showing location of icon

在JSON设置窗口中,添加以下内容(在花括号{}内):
"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\bash.exe"`

(在这里您还可以放置任何其他自定义设置。)

检查该路径,确保您的bash.exe文件在那里。否则,请找到它所在的位置,并指向那个路径。

现在,如果您在Visual Studio Code中打开一个新的终端窗口,它应该以Bash而不是PowerShell打开。


1
看起来自从这个答案发布以来,VSCode已经有了一些变化,现在有一个带有{}的按钮,你必须按下它才能进入编辑器进行更改。这个答案中的命令确实可以正常工作。感谢您的回答。 - dmoore1181
你不应该使用System32中的可执行文件,因为(通常情况下)vs code无法访问该可执行文件。而是要使用程序文件中的一个,terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe" - Sergio Escudero
"terminal.integrated.shell.windows""terminal.integrated.shell.linux"已被弃用,在我的情况下(在Linux上的v1.62.0)似乎无法工作。请改用"terminal.integrated.defaultProfile.windows""terminal.integrated.shell.linux",并在"terminal.integrated.profiles.windows""terminal.integrated.profiles.linux"中定义可执行文件路径。另请参见@Stephanieraymos的答案。 - Olli
我有 terminal.integrated.defaultProfile.linuxterminal.integrated.defaultProfile.windows 以及 terminal.integrated.defaultProfile.osx。神奇的三个... - Timo

18
只需按下 Ctrl + Shift + P 搜索 "Terminal: Select Default Profile"。点击 您将看到终端选项,选择 Git Bash

6

要在Visual Studio Code中更改项目的默认终端:

  1. 创建名为.vscode的文件夹
  2. 在该文件夹中创建一个settings.json文件
  3. 编写所需的设置

例如,如果您是Windows用户并想将"命令提示符"设置为默认终端,则可以编写:

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

值。您可以传递:"Git Bash""PowerShell""Command Prompt"
对于Linux,您将使用terminal.integrated.defaultProfile.linux,而对于macOS,您将使用:terminal.integrated.defaultProfile.osx

Enter image description here


5
由于您使用WSL,Visual Studio Code具有专用的Remote - WSL扩展,因此您可以直接在Visual Studio Code中使用Linux环境。
当您在Linux中打开项目时,默认情况下会使用Linux的默认shell(默认为bash),因此无需进行任何配置。
如果您想切换到其他配置文件,可以在“终端”→“集成”→“默认配置文件:Linux”部分中选择您喜欢的配置文件。

Linux Terminal Profiles


这应该是正确的答案,因为 OP 想要在 Vscode 中从 Windows 切换到 Linux,这只有通过 Remote WSL 才能实现。 - Timo
我在wsl上将fish设置为默认Shell,但是vscode仍然使用bash。显式地设置了这个设置,现在它就像魔法般工作了,谢谢! - amordo

4
您可以通过按下Ctrl + Shift + P 打开命令面板来更改终端。
或者您可以在顶部点击“视图”,然后选择“打开命令面板”。
然后键入Terminal: Select Default Profile 接着您可以键入您想要的终端。

4

参考 @arielhad 的解决方案...

我的VSCode版本为1.57.1。

打开settings.xml文件:

  • Ctrl + Shift + p
  • 输入“Open Settings (JSON)”并选择。

添加以下内容:

"terminal.integrated.profiles.windows": {
    "PowerShell": {
        "path": [
            "${env:windir}\\Sysnative\\WindowsPowerShell\\v1.0\\powershell.exe",
            "${env:windir}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
        ],
        "source": "PowerShell",
        "icon": "terminal-powershell",
        "args": [
            "-NoLogo",
            "-ExecutionPolicy",
            "Bypass"
        ]
    },
    "Command Prompt": {
        "path": [
            "${env:windir}\\Sysnative\\cmd.exe",
            "${env:windir}\\System32\\cmd.exe"
        ],
        "icon": "terminal-cmd"
    },

//START: THIS DOES NOT WORK
    "Git Bash": {
        "path": [
            "C:\\Program Files\\Git\\bin\\bash.exe",
        ],
        "source": "Git Bash",
        "icon": "terminal-bash"
    }
// END: THIS DOES NOT WORK

//START: THIS WORKS
    "GitBash": {
        "path": [
            "C:\\Program Files\\Git\\bin\\bash.exe",
        ],
        "icon": "terminal-bash"
    }
// END: THIS WORKS
}

我不知道为什么第二种方法可行,但它确实可行。看起来“Git Bash”是一个保留名称,我猜你不能设置路径。

2
按下 Ctrl + Shift + P。然后输入 settings.json
在文件末尾,将 'PowerShell' 更改为 'Git Bash'。

哈哈,这个答案与@OneShadow的答案有何不同之处,请详细阐述一下。 - Abhishek Dutt

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