如何在 VS 代码中添加多个终端?

我们可以在 VS 代码中添加多个不同的终端吗?我计划添加以下三个终端,并与所有这些工作:

  1. 命令提示符
  2. PowerShell
  3. Git Bash

我知道我需要在 Preferences => Setting中添加以下命令

 // // 64-bit cmd if available, otherwise 32-bit
"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\cmd.exe",
// // 64-bit PowerShell if available, otherwise 32-bit
"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\WindowsPowerShell\\v1.0\\powershell.exe",
// // Git Bash
"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",

我想在 setting.json中添加上述三个命令

enter image description here

当我点击 +时,不同的终端应该打开,我想在不改变首选项的情况下使用这些终端。 是否可能在 VS 代码或不?

110624 次浏览

There is a way to make this happens with these steps by installing an extension:

  1. Find an extension called Shell launcher and install it or you can find it here. Reload VS Code if you want or after you finished all steps.

  2. Go to Files --> Preferences --> Settings and this will open settings.json file and you then insert this (you can edit this to your heart's content):

Code:

"shellLauncher.shells.windows": [
{
"shell": "C:\\Windows\\<sysnative>\\cmd.exe",
"label": "cmd"
},
{
"shell": "C:\\Windows\\<sysnative>\\WindowsPowerShell\\v1.0\\powershell.exe",
"label": "PowerShell"
},
{
"shell": "C:\\Program Files\\Git\\bin\\bash.exe",
"label": "Git bash"
},
{
"shell": "C:\\Windows\\<sysnative>\\bash.exe",
"label": "WSL Bash"
}
]

PS: You can use shellLauncher.shells.linux for Linux or shellLauncher.shells.osx for macOS.

  1. Go to Files --> Preferences --> Keyboard Shortcuts and then find on {} icon on the top right corner to open keybindings.json file. Insert this:

Code:

[
{ "key": "ctrl+alt+`", "command": "shellLauncher.launch" }
]

Update: Type shelllauncher into the search bar. You can then see Shell Launcher: Launch command. Highlight and use any keybinding you like. For example, I picked Ctrl + Alt + (backtick)` for myself.

You can reload your VS Code and click the key combination you have assigned and that will give you the option to choose which integrated terminal you want to use.

enter image description here

For more details, please check the official site: https://marketplace.visualstudio.com/items?itemName=Tyriar.shell-launcher

Enjoy!

This can be done by adding a different key at the end. By just changing your example to:

// // 64-bit cmd if available, otherwise 32-bit
"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\cmd.exe",
// // 64-bit PowerShell if available, otherwise 32-bit
"terminal.integrated.shell.windows2": "C:\\Windows\\sysnative\\WindowsPowerShell\\v1.0\\powershell.exe",
// // Git Bash
"terminal.integrated.shell.windows3": "C:\\Program Files\\Git\\bin\\bash.exe",

Note that the key ...shell.windows is changed to ...shell.windows2 and ...shell.windows3.

Follow-up finding: I've noticed that once restart of the IDE, only the first terminal gets displayed. I had to reopen the settings and save it once to get both the terminals again. Will post if any better solution available.

press ctrl + shift + ` shortcut, or press a cross sign to run new terminal, then type bash if your default mode is powershell or powershell if your default mode is bash. And here you are, your terminal is switched.

For now VS Code support defining only one of available terminals as default at a time and you can not add multiple shell terminals.

If you don't want to install "Shell Launcher" plugin as suggested by @ian0411 then here is a quick way to change/select default terminal.

Press "Ctrl + Shift + P" to open the Command Palette.

Type "Terminal: Select Default Shell" and hit enter. You will be prompted to select Git Bash, PowerShell or Cmd as shown below:

Option to change preferred terminal shell

Note: This will change the default shell terminal but there is no harm changing it whenever you need to use another.

BTW, if you need to switch only between Cmd & Powershell then you can type cmd or powershell (in already opened terminal) to switch to desired terminal. But it would not work for Git Bash.

Even the question is asked last year and the answer is accepted but still I feel to answer this question as I didn't found any simple, suitable and complete answer while as a development I need multiple terminal handy in a click like below:-

enter image description here

and I don't bother about their path, add another extension for what the VS Code is already capable of or reload VS Shell etc and go to insert and setup the settings files manually.

I found this question is asked many times and almost all landed up manually setup of write some settings etc. or sometimes only opted to get a single type of terminal. The answer of @Pawan is somewhat near but again that solution finally land up to a single terminal, going to command setup for switch terminal and this one will work for git or any other terminal.

If you have tools installed which worked on command line like power-shell and git along with default cmd prompt in windows then the follow the quick three steps to get all terminals at once and switch to anyone with a click.

  1. Open terminal, it should be visible (use ctrl+` or from menu View-> Integrated Terminal )

  2. Open commands search (use Ctrl+Shift+P or from menu View->Command Palette...)

  3. In command box Type "Terminal: Select Default Shell" to select this option from drop down. enter image description here
  4. As you select this option, then all the available commands which are in path will be listed below like below enter image description here

  5. Just click any one which you like to add for quick access from command list.

  6. Finally, in the terminal window, just click on + sign next to terminal list as shown below:- enter image description here

The terminal selected in step 5 will now added after performing step6 to the list without removal of earlier terminal.

  1. Repeat step 3-6 for adding any other terminal to command list.

To work with particular terminal just select the required one in the terminal list of the terminal window.

In the terminal tab, there's a Split Terminal button. Works like a charm

For WSL Ubuntu on Windows terminal:

File -> preferences -> settings -> click code icon in top right

Enter the following:

{
"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\wsl.exe",
"git.enableSmartCommit": true
}

To open the multiple terminal please check the screen shot for the same(on the right bottom of the visual studio code their will be a dropdown and just after it, their is +(plus) icon . On pressing it the new terminal will open.).

I don't see this in the above, but read all the answers. I think this is the best approach for what is supported right now. I believe, like myself, the OP simply wants to open VSCode, and down yonder on the terminal window, we just want a list of options to open from.

Yes, you can open the command, "Terminal: Select Default Shell" and loop through that to add various types of terminals...

Yes, you can open the command, "Terminal: Select Default Shell" and loop through that to add various types of terminals...

Followed by:

enter image description here

I don't know when this option was added to the dropdown, but look! No need to start by looking up the command. I think this is a lot smoother than any other answer, but it still results in the annoying overwrite of your default term to whatever your last choice was before leaving session.

Here, we can more easily open the Select Default Shell - it should support "Select New Shell". Simple, you'd think. Hopefully someone adds that soon or I have time to contribute.

Here, we can more easily open the "Select Default Shell" - it should support "Select New Shell"

As of https://code.visualstudio.com/updates/v1_35 you can now:

  1. select from the pulldown menu directly above the terminal "Select Default Shell"
  2. select the one you like
  3. click the +
  4. done

(the same goes for opening split screen: before this do 1 + 2 and then click the split screen button)

(although overriding the default but does no longer matter if this is your flow)

The recommended way to automatically open multiple terminal windows is to use the Tasks feature. See Automating launching of terminals in the VS Code documentation.

I'm fairly certain that these are old-style settings.json and won't work as discussed in VS Code 2021. The new style looks like:

...
"terminal.integrated.profiles.windows":
{   "Bash":
{    "path": ["C:\\Programs\\Git\\bin\\bash.exe"],
"icon": "terminal-bash",
},
"Command Prompt":
{   "path": ["${env:windir}\\System32\\cmd.exe"],
"args": [],
"icon": "terminal-cmd"
},
"PowerShell":
{   "path":  ["C:\\Programs\\PowerShell\\pwsh.exe"],
"source": "PowerShell",
"args": [],
"icon": "terminal-powershell"
},
},
"terminal.integrated.defaultProfile.windows": "Bash"
...

That will get you access to Command and PowerShells and set bash as the default shell (my preference). If you want to launch a standalone application like git-bash, you will need something else.