493

Visual Studio Code on Windows uses PowerShell by default as the integrated terminal. If you want to use Bash from Visual Studio Code, what steps should be followed?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mahade Walid
  • 9,404
  • 5
  • 17
  • 25
  • Please read https://stackoverflow.com/questions/43427631/how-to-add-multiple-terminal-in-vs-code/50890703#50890703 for answer your question. – AKS Jun 16 '18 at 18:53

31 Answers31

648
  1. Install Git from https://git-scm.com/download/win

  2. Open Visual Studio Code and press and hold Ctrl + ` to open the terminal.

    Enter image description here

  3. Open the command palette using Ctrl + Shift + P.

  4. Type - Select Default Profile

  5. Select Git Bash from the options

  6. Click on the + icon in the terminal window

  7. The new terminal now will be a Git Bash terminal. Give it a few seconds to load Git Bash

    Enter image description here

  8. You can now toggle between the different terminals as well from the dropdown in terminal.

    Enter image description here

jDelforge
  • 129
  • 4
  • 15
Himanshu
  • 6,983
  • 1
  • 15
  • 9
  • 8
    This worked for me. I tried all of the other guides by manually adding in the default bash but it just opened up a new terminal window every time. Many thanks – Glen Aug 21 '18 at 10:30
  • if ctrl+` doesn't work (as in, nothing happens! even after having installed git and set user settings json), then try this: ctrl+shift+P > Focus Terminal. – olisteadman Feb 27 '19 at 11:28
  • If you use `.bash_profile` instead of `.bashrc`, `bash.exe` will not check the profile script. You need to go to the JSON settings and set manually the arguments list as follows `"terminal.integrated.shellArgs.windows": ["--rcfile", "C:\\Users\\User\\.bash_profile"]` – Geo Angelopoulos Apr 08 '19 at 20:56
  • Glad you added this answer. I was confused why I wasn't getting the bash shell and I just needed to push the "+" symbol to open a new terminal. – Ragnar Lothbrok Sep 27 '19 at 17:43
  • I had to do ctrl+' (single quote, not backtick) – Max Nov 03 '19 at 11:09
  • 5
    Note that this now shows as "Select Default Profile" – Matt Vukas Apr 24 '21 at 04:34
  • much easier way to code in bash than using nano in terminal. is there a way in vsc to run the script by pressing run – Golden Lion Aug 27 '21 at 21:12
  • 2
    No one of this solutions work with the new VSCode Version: 1.60.0 (user setup): I still have powershell, when VSCode Startup.. – Ruslan Jackson Sep 06 '21 at 18:59
  • 4
    Doesn't work for me. Git Bash doesn't show up in the options. I found the answer [here](https://stackoverflow.com/a/70407051/283851). (other answers had a space character in the `Git Bash` name, which doesn't work - it has to be `GitBash` without a space.) – mindplay.dk Jun 14 '22 at 08:17
  • @mindplay.dk I second this. You have to put "GitBash" as the path of bash.exe in your directory (most of the times in Program Files, but not always) without the space. – cst1992 Sep 02 '22 at 10:33
420

You no longer need to type in bash.exe path manually. This answer is deprecated. Now you can switch to bash directly, if you have git installed in the default path. If you installed git to a different path you need to use the below solution.


Install Git from https://git-scm.com/download/win.

Then open Visual Studio Code and open the command palette using Ctrl + Shift + P. Then type "open user setting", and then select "Open User Settings" from the drop down menu.

Visual Studio Code command palate

Then this tab will open up with default settings on left and your settings on the right:

enter image description here

Now copy this line of code to your own settings page (the pane on the right hand side) and save - "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe"

Note: "C:\\Program Files\Git\bin\bash.exe" is the path where the bash.exe file is located from the Git installation. If you are using the Windows Subsystem for Linux (WSL) Bash shell, the path would be "C:\Windows\System32\bash.exe"

Now press Ctrl + ` to open up the terminal from Visual Studio Code. And you will have Bash -

Enter image description here

Mahade Walid
  • 9,404
  • 5
  • 17
  • 25
  • 1
    Those are default colors from git bash. – Mahade Walid Jun 02 '17 at 22:37
  • 6
    Two infos, that might be helpful: make sure, that you escape backslash characters in your json, when writing the path. Also make sure, that you include the 64 bit version of git bash to your vscode, since the 32 bit version might not have the colors by default. (Just remove the " (x86)" part from the path) – Lajos Mészáros Jun 07 '17 at 08:48
  • 2
    Adding to Lajos, if you still cannot see the terminal open up, try restarting vscode. – adityah Sep 01 '17 at 22:30
  • 5
    I was facing issue with `"terminal.integrated.shell.windows": "C:\\Program Files\\Git\bin\bash.exe"`, terminal was not opening. Tried `"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe"` . Worked perfectly for me. – Dharmjeet Kumar Dec 16 '17 at 13:11
  • @WalidOmonos How to add powershell and bash both at a same time. I can select which one to use? – dev Jan 11 '18 at 21:28
  • @dev I dont think you can use both at the same time. – Mahade Walid Jan 12 '18 at 07:45
  • 42
    FYI, make sure you link to `bash.exe`, and not `git-bash.exe`. The latter will open a terminal outside VS code as a separate window, whilst `bash.exe` will run inside VS code. – Darius Jan 16 '18 at 19:16
  • Do this: `"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",` – JREAM Oct 26 '18 at 13:35
  • 1
    this shouldn't be the accepted answer. Mr. Himanshu's was on point quicker. – dawn Dec 09 '18 at 20:09
  • How do you "switch to bash directly"? EDIT: Also, i already had theese setting, but the option to use git bash still does not show up – Matej J Jun 03 '19 at 15:51
  • 2
    How to find settings.json in User settings, is not mentioned here and I had to play little bit with VSCode to find it. We need to type 'terminal' in the 'search setting' text box, and you will see a link "Edit in settings.json", you need to click here. – Ashu Dec 01 '19 at 06:08
  • 1
    Thank you for this ..my life is now easier. Obviously you will need to investigate where the bash.exe file is located. There can be differing Program File folders so check them to find your git folder then locate the bin folder, which will have the bash.exe file – Jones Jan 07 '20 at 23:28
  • In latest version of vscode `"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe"` updated to `"terminal.integrated.defaultProfile.windows": "Git Bash"` – Satya Pendem Dec 19 '22 at 14:26
111

Updated 2023-03-09 : new screenshot. The pull down menu to select the terminal shell or configure the default one is slightly changed. Click Ctrl-' to select:

new pull down menu

Updated: Newer versions of Visual Studio Code have the Select Default Shell command in the terminal pull down menu:

Select Default Shell option

Remember that it just lists the shells that are in your %PATH% environment variable. For shells that aren't in your path, see other answers.

Extra tip: when you start bash it will just execute .bashrc, if you have initialization commands in .bash_profile you must copy it to .bashrc. It's essential for using Conda enviroments in Git Bash.

Before version 1.36 (June 2019)

The easiest way now (at least from Visual Studio Code 1.22 on) is to type Shift + Ctrl + P to open the Command Palette and type:

Select Default Shell

Now you can easily select your preferred shell between the ones found in your path:

Shell selection list

For shells that aren't in your %PATH%, see the other answers.

See the complete Visual Studio Code shell reference. There's lot of meaty stuff.

neves
  • 33,186
  • 27
  • 159
  • 192
  • 1
    This was the answer I was looking for. I wanted to use WSL Bash but the other answers was trying to using the bash that comes with git – ahmadali shafiee Jan 29 '19 at 05:51
  • 3
    Is there a way to open a particular shell...without making it the "default?" I think it makes sense to have a "open 'bash' just this one time." – Brent Arias Sep 08 '19 at 23:09
  • That must be specific to Linux, none of those are available in my Windows installation. – RedGlyph Feb 19 '23 at 14:01
  • @BrentArias there is down arrow beside the shell – neves Mar 01 '23 at 16:06
  • @RedGlyph maybe you should install git in your windows. See if the bash script is in your path. – neves Mar 01 '23 at 16:07
  • @neves Git is in my PATH. What I mean is that there is no such dropdown, so I suppose they've put the settings elsewhere or it's only for Linux. Probably hidden away in a JSON script as VSC does for everything. PowerShell is the only available terminal. I'll have a look into the doc again, for now I found a better tool. – RedGlyph Mar 08 '23 at 09:09
  • 1
    @RedGlyph now the pull down menu is beside the Plus sign. I'll edit the screenshot above. – neves Mar 09 '23 at 21:47
  • @neves I have tried it, but this one only shows me "powershell", which is the default when the terminal opens. So not even the command prompt. I'll have to check the settings somewhere. I find VSC very confusing to setup and use. – RedGlyph Mar 14 '23 at 19:45
  • @RedGlyph bash isn't probably in your path. Open the power shell and type `where bash` to see if it is located. There is a configuration in vscode to set the complete path to the bash shell. See other answers – neves Mar 15 '23 at 21:52
49

Press and hold Ctrl + ` to open the terminal. Inside the terminal, type bash to use Git Bash in the terminal. Note: Make sure you have Git Bash installed on your machine.

If you wish to use PowerShell again, just type powershell in the terminal. To use the Windows command line, type cmd in the terminal.

The setting you choose will be used as your default.

Isaac Sekamatte
  • 5,500
  • 1
  • 34
  • 40
  • 4
    this adds more value to the selected answer – pjdupreez Oct 19 '17 at 17:12
  • 2
    To "switch back to powershell" probably you need to `exit` from your bash shell running inside the default shell, then type `powershell` since bash doesn't know what powershell is. – Lucas Apr 08 '18 at 15:43
19

For me this is the only combination worked!

"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\git-cmd.exe",
"terminal.integrated.shellArgs.windows": [
  "--command=usr/bin/bash.exe",
  "-l",
  "-i"
]

With git-bash.exe as the ...shell.windows, every time the bash was opening outside VS!!

Thank God it worked finally!! Else, I was planning to wipe out VS completely and reinstall it (making me to reinstall all my extensions and redo my customizations!)

  • 1
    this really helped me. I got bash working per the previous answers above, but I lost all of my bash and git aliases. This bought them back. Thanks. – David Martin Jun 22 '18 at 19:14
  • 1
    This is what worked with me: "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe", "terminal.integrated.shellArgs.windows": [ "--login", "-i" ] – Belal Mohammed Oct 07 '18 at 08:57
  • 1
    This solution worked for me as well. A caveat to add is that in my case, to reduce lag with gitbash, followed this post https://stackoverflow.com/questions/32232978/change-the-location-of-the-directory-in-a-windows-install-of-git-bash and set a new HOME environment variable in windows. To get it working properly in VSCode, the above answer worked perfectly. – Rob B Jul 08 '19 at 20:42
18

Visual Studio Code can detect and list installed Git Bash in the menu of config Terminal: Select Default Profile, as many other answers have already described, but this has never happened to me. For those also not-so-lucky as me, you can add custom profile into Visual Studio Code's settings.json manually:

{
    // Tested in Visual Studio Code version 1.58.2, 1.59.1
    // Notice: my git install path is `D:\Git\bin\bash.exe`

    //"terminal.integrated.shell.windows": "D:\\Git\\bin\\bash.exe",
    // This works fine for me for a long time,
    // but in latest versions this is reported as deprecated,
    // you can keep this and sometimes Visual Studio Code will prompt to help
    // `migrate` it into new setting.

    // This part can be generated by Visual Studio Code
    "terminal.integrated.profiles.windows": {
        // This seems to be a reserved profile name, and also does not work for
        // me
        "Git Bash": {
            "path": "D:\\Git\\bin\\bash.exe",
            "icon": "terminal-bash"
        },
        "PowerShell": {
            "source": "PowerShell",
            "icon": "terminal-powershell"
        },
        "Command Prompt": {
            "path": [
                "${env:windir}\\Sysnative\\cmd.exe",
                "${env:windir}\\System32\\cmd.exe"
            ],
            "args": [],
            "icon": "terminal-cmd"
        },

        // Add your custom one with a different profile name from "Git Bash"
        "gitbash": {
            "path": "D:\\Git\\bin\\bash.exe",
            "icon": "terminal-bash"
        }
    },
    // Set the custom profile as default
    "terminal.integrated.defaultProfile.windows": "gitbash",

    // ...
}

rustyhu
  • 1,912
  • 19
  • 28
  • 3
    For `"terminal.integrated.defaultProfile.windows": "gitbash"`- Value is not accepted. Valid values: "PowerShell", "Command Prompt", "Git Bash", "JavaScript Debug Terminal". – Raul Sep 10 '21 at 11:58
  • @Raul It is only a warning, you can try reloading the VS Code window to check the effect. If the profile name you set for `"terminal.integrated.defaultProfile.windows"` corresponds to the one added in `"terminal.integrated.profiles.windows"` it should work. – rustyhu Sep 10 '21 at 17:14
  • Mine has the following line, and it works on my system: `"terminal.integrated.defaultProfile.windows": "Git Bash"` – SherylHohman Jan 30 '22 at 15:00
  • @SherylHohman Yeah if the "Git Bash" profile already exists and works it's fine, but if it does not you can also add a custom profile as what my answer introduces. – rustyhu Feb 01 '22 at 15:18
  • 1
    The take-away is that the name you supply must match an existing (self-defined) profile name. It can be named anything you wish, but the name must be the same in both locations...and that is possibly the source of @Raul error. Edit the settings.json file accordingly. – SherylHohman Feb 01 '22 at 16:20
  • No longer works if you have spaces in the profile names - see [this answer](https://stackoverflow.com/a/72307809/283851) – mindplay.dk Jun 14 '22 at 08:42
  • @mindplay.dk Thanks for your advise, but I rechecked the setting in my local environment, it worked well as before, and I had tried editing my default profile name as "git bash", and I noticed the generated profile name of `cmd.exe` "Command Prompt" also had a space inside, these two also worked well as default profile in my environment. I use VSCode 1.67.2 (system setup) and Windows_NT x64 10.0.18363 now. – rustyhu Jun 14 '22 at 11:44
  • @rustyhu latest VS Code here, too. I was unable to add a new profile with a space in the name - it doesn't even show up as a choice, it gets completely ignored. Nothing to do with setting the default, as others noted in the comments - I pick the default using the UI, but it doesn't show up there at all if there's a space in it. Go figure. ‍♂️ – mindplay.dk Jun 15 '22 at 14:05
  • @mindplay.dk Quite strange. Is there any relevant warning prompt for the profiles in your `settings.json`? I usually edit `settings.json` directly but I have also checked UI and it's fine. Maybe you can use a custom profile with no space in the name as workaround first, which is also recommended in my answer. – rustyhu Jun 16 '22 at 01:35
  • This works for me, vscode 1.69.2, git-bash 2.39.0.2 – AG6HQ Feb 13 '23 at 21:57
17

Things has been a little bit changed due to the latest updates on Visual Studio Code. The following steps work for me.

  1. Press Ctrl + Shift + P to open the Visual Studio Code command palate.

  2. Type >preferences: Open Settings (JSON) in the text area.

  3. Add the following lines at the end of the JSON file which is displayed in your right hand pane.

    "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe"
    
  4. Close and reopen your Visual Studio Code instance.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aravinda Meewalaarachchi
  • 2,551
  • 1
  • 27
  • 24
10

I followed this tutorial from Paul DeCarlo to use the Bash from the Windows Subsystem for Linux (WSL) instead of what comes with Git Bash for Windows. They are the same steps as above in the answer, but use the below in your User Settings instead.

"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\bash.exe",

This worked for me the first time... which is rare for this stuff.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jay Killeen
  • 2,832
  • 6
  • 39
  • 66
7

This, at least for me, will make Visual Studio Code open a new Bash window as an external terminal.

If you want the integrated environment you need to point to the sh.exe file inside the bin folder of your Git installation.

So the configuration should say C:\\<my-git-install>\\bin\\sh.exe.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Christer
  • 1,651
  • 1
  • 17
  • 34
7

(2021, VSC v.1.55.1)

How to add Git Bash as default Terminal, for those who installed their Git Bash not in the default path:

  1. In Visual Studio Code open Settings using Ctrl + ,
  2. a) In Search settings (red box on screenshot) field type integrated automation enter image description here b) Or just click through Features -> Terminal (blue boxes on ss)
  3. Click any Edit in settings.json enter image description here
  4. Type your bash.exe location into the "terminal.integrated.shell.windows": " " field enter image description here

Note1: As it's a JSON file, remember to use double \\ instead of \ in your pathway.

Note2: Don't confuse the bash.exe(it's in bin folder) with git-bash.exe, in first case the bash terminal will stay inside VSC, in second it will be opened externally.

enter image description here

Jakub
  • 218
  • 3
  • 6
6

It depends on whether you have installed Git Bash in the current user only or all users:

If it is installed on all users then put "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe" in your User Settings (Ctrl + Comma).

If it is installed on only the current user then put "terminal.integrated.shell.windows": "C:\\Users\\<name of your user>\\AppData\\Local\\Programs\\Git\\bin\\bash.exe" in your User Settings (Ctrl + Comma).

If the methods listed above do not work then you should try Christer's solution which says -

If you want the integrated environment you need to point to the sh.exe file inside the bin folder of your Git installation.

So the configuration should say C:\\<my-git-install>\\bin\\sh.exe.

Note: The sh.exe and bash.exe appear completely same to me. There should be no difference between them.

Community
  • 1
  • 1
Shaurya
  • 375
  • 4
  • 7
  • This worked for me since I (for some reason) installed git for only the current user but I accessed the json settings via: CTRL-SHFT-P then selected 'Preferences: Open Settings (JSON)' Thanks for the code snippet! – AWP Jan 05 '21 at 21:02
6

This answer is similar to the top voted answer, but with an important distinction: a lot of the previous answers on this question focus on running Git Bash while my answer focuses on running WSL Bash.

  1. Enable Windows Subsystem for Linux on your Windows 10 machine.

  2. Open Visual Studio Code and press and hold Ctrl + ` to open the terminal.

  3. Open the command palette using Ctrl + Shift + P.

  4. Type - Select Default Shell.

  5. Select WSL Bash (NOT Git Bash) from the options.

enter image description here

  1. Click on the + icon in the terminal window. The new terminal now will be a WSL Bash terminal!
therobinkim
  • 2,500
  • 14
  • 20
6

My VS Code version: 1.56.1 (code --version)

User settings for configuring integrated terminals:

  • Ctrl + Shift + P
  • type: user
  • select: Preferences: Open User Settings
  • click: Open Settings (JSON) button (near to upper right corner)

settings.json:

{
    "terminal.integrated.tabs.enabled": true,
    "terminal.integrated.shell.windows": "<your installation path>\\Git\\bin\\bash.exe",
    "terminal.integrated.defaultProfile.windows": "Git Bash",
    "terminal.integrated.profiles.windows": {
        "Git Bash": {
            "path": "<your installation path>\\Git\\bin\\bash.exe",
            "icon": "terminal-bash"
        },
        "Command Prompt": {
            "path": "${env:windir}\\System32\\cmd.exe",
            "icon": "terminal-cmd"
        },
        "Windows PowerShell": {
            "path": "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
            "icon": "terminal-powershell"
        }
    }
}

terminal.integrated.defaultProfile.windows

This property will only set GitBash to be default when you hit the "add new terminal" ("+") button. It is not enought to make it default on startup.

terminal.integrated.shell.windows

Depricated warning will be shown. But this configuration is needed to make the selected shell (GitBash here) default on startup.

Problem filtering

On PROBLEMS tab, next to the input field, clicking on the filter icon I checked "Show Active File Only" option to make this deprecated error disappear when dealing with anything else.

S.Dave
  • 151
  • 2
  • 4
5

Latest VS code :

  • if you can't see the settings.json, go to menu File -> Preferences -> Settings (or press on Ctrl+,)
  • Settings appear, see two tabs User (selected by default) and Workspace. Go to User -> Features -> Terminal
  • Terminal section appear, see link edit in settings.json. Click and add "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",
  • Save and Restart VS code.

Bash terminal will reflect on the terminal.

Teuk1
  • 3
  • 3
Rijo
  • 2,963
  • 5
  • 35
  • 62
5

For scoop users:

"terminal.integrated.shell.windows": "C:\\Users\\[YOUR-NAME]\\scoop\\apps\\git\\current\\usr\\bin\\bash.exe",
"terminal.integrated.shellArgs.windows": [
  "-l",
  "-i"
],
Zoe
  • 27,060
  • 21
  • 118
  • 148
sfy
  • 2,810
  • 1
  • 20
  • 22
4

Add the Git\bin directory to the Path environment variable. The directory is %ProgramFiles%\Git\bin by default. By this way you can access Git Bash with simply typing bash in every terminal including the integrated terminal of Visual Studio Code.

How to set the path and environment variables in Windows

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
4

I had already set up lots of conda environments on WSL (Bash on Ubuntu on Windows), so I wanted to use the same Bash installation on Visual Studio Code.

To do that, I just had to specify the path of this particular Bash executable (instead of the Git-Bash) on Visual Studio Code's settings:

"terminal.integrated.shell.windows": "C:\\Windows\\System32\\bash.exe"


PS: to make sure where your Ubuntu on Bash executable is installed on your Windows machine, open the Command prompt (search: cmd) and run:

where bash.exe

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dvitsios
  • 458
  • 2
  • 7
4

simply go to settings.json in visual studio code and add this line:

  "terminal.integrated.defaultProfile.windows": "Git Bash",
naimur978
  • 144
  • 8
3

VS CODE 1.60.0

As I was facing the preceding problem that Git Bash has not been recognized at all, although being previously installed. To let any of the above-mentioned solutions work, you need to follow the instructions discussed in this thread and listed below.

  1. Go to your settings.json
  2. enable git bash by adding "git.enabled": true
  3. and define it's path by adding "git.path": "<YOUR PATH TO GIT>\\Git\\bin\\bash.exe"

Afterward, the instructions, for instance as defined by @rustyhu, will work.

PS: Hopefully I'm gonna save you a couple hours of time while debugging the originally stated problem

Ndrslmpk
  • 125
  • 9
3

Update: (June 2022 , VsCode 1.67)

Many answers here (as this one, highly upvoted) rely on the setting "terminal.integrated.shell.windows" which is now deprecated.

And many others (as the accepted one) assume Git for Windows to be installed with bash.exe in the PATH - which is not the currently recommended/default installation option.

My current recipe:

Add the following to your settings (Ctrl-Shift-P -> Preferences: Open Settings (JSON) ), replacing with your own bash path:

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

Close your current terminals and restart VsCode.

leonbloy
  • 73,180
  • 20
  • 142
  • 190
3

In July 2022, I fixed this by adding the following inside the curly braces.

"terminal.integrated.defaultProfile.windows": "Git Bash"

Notice the space between Git and Bash.

So, my settings.json looks like this:

{
    "python.defaultInterpreterPath": "C:\\Users\\Prakh\\AppData\\Local\\Programs\\Python\\Python39\\python.exe",
    "python.diagnostics.sourceMapsEnabled": true,
    "python.linting.pylintEnabled": true,
    "python.linting.enabled": true,
    "code-runner.runInTerminal": true,
    "terminal.integrated.defaultProfile.windows": "Git Bash"
}

Now restart VS code and close all existing terminals and open a new one.

Prakhar Sharma
  • 543
  • 9
  • 19
2

What about detached or unrelated shells and code [args] support?

While other answers talk about how to configure and use the VScode integrated WSL bash terminal support, they don't solve the problem of "detached shells": shells which were not launched from within VScode, or which somehow get "disconnected" from the VScode server instance associated with the IDE.

Such shells can give errors like:

Command is only available in WSL or inside a Visual Studio Code terminal.

or...

Unable to connect to VS Code server. Error in request

Here's a script which makes it easy to solve this problem.

I use this daily to connect shells in a tmux session with a specific VScode server instance, or to fix an integrated shell that's become detached from its hosting IDE.

#!/bin/bash
# codesrv-connect
#
#  Purpose:
#     Copies the vscode connection environment from one shell to another, so that you can use the
#     vscode integrated terminal's "code [args]" command to communicate with that instance of vscode
#     from an unrelated shell.
#
#  Usage:
#    1.  Open an integrated terminal in vscode, and run codesrv-connect
#    2.  In the target shell, cd to the same directory and run
#       ". .codesrv-connect", or follow the instruction printed by codesrv-connect.
#
#  Setup:
#    Put "codesrv-connect somewhere on your PATH (e.g. ~/bin)"
#
#  Cleanup:
#    - Delete abandoned .codesrv-connect files when their vscode sessions die.
#    - Do not add .codesrv-connect files to git repositories.
#
#  Notes:
#     The VSCODE_IPC_HOOK_CLI environment variable points to a socket which is rather volatile, while the long path for the 'code' alias is more stable: vscode doesn't change the latter even across a "code -r ." reload.  But the former is easily detached and so you need a fresh value if that happens.  This is what codesrv-connect does: it captures the value of these two and writes them to .codesrv-connect in the current dir.
#
#   Verinfo: v1.0.0 - les.matheson@gmail.com - 2020-03-31
#

function errExit {
    echo "ERROR: $@" >&2
    exit 1
}

[[ -S $VSCODE_IPC_HOOK_CLI ]] || errExit "VSCODE_IPC_HOOK_CLI not defined or not a pipe [$VSCODE_IPC_HOOK_CLI]"
if [[ $(which code) != *vscode-server* ]]; then
    errExit "The 'code' command doesn't refer to something under .vscode-server: $(type -a code)"
fi
cat <<EOF >.codesrv-connect
# Temp file created by $(which codesrv-connect): source this into your working shell like '. .codesrv-connect'
# ( git hint: add ".codesrv-connect" to .gitignore )
#
cd "$PWD"
if ! test -S "$VSCODE_IPC_HOOK_CLI"; then
    echo "ERROR: $VSCODE_IPC_HOOK_CLI not a socket. Dead session."
else
    export VSCODE_IPC_HOOK_CLI="$VSCODE_IPC_HOOK_CLI"
    alias code=$(which code)
    echo "Done: the 'code' command will talk to socket \"$VSCODE_IPC_HOOK_CLI\" now."
    echo "You can delete .codesrv-connect when the vscode server context dies, or reuse it in other shells until then."
fi
EOF

echo "# OK: run this to connect to vscode server in a destination shell:"
echo ". $PWD/.codesrv-connect"

Stabledog
  • 3,110
  • 2
  • 32
  • 43
  • 1
    For any fish users out there, I compiled something based on this into a small fish plugin https://github.com/chvolkmann/code-connect-fish-plugin – chvolkmann Feb 13 '21 at 18:59
2

VSCODE 1.63.2

Even combining the answers here, I couldn't get it to work. Maybe, I have a somewhat exotic setup. I'd get this error, Setting "terminal.integrated.defaultProfile.windows": "Git Bash" and it would still only open Powershell and Git Bash is nowhere to be seen:

Value is not accepted

My workaround was to modify the entry for Command Prompt by changing its path pointing to bash and then set that as default, like so:

"terminal.integrated.profiles.windows": {
    "PowerShell": {
        "source": "PowerShell",
        "icon": "terminal-powershell"
    },
    "Command Prompt": {
        "path": [
            "C:\\Path\\To\\Git\\bin\\bash.exe",
            // "${env:windir}\\Sysnative\\cmd.exe",
            // "${env:windir}\\System32\\cmd.exe"
        ],
        "args": [],
        "icon": "terminal-cmd"
    
},
"terminal.integrated.defaultProfile.windows": "Command Prompt",
mc51
  • 1,883
  • 14
  • 28
2

Ctrl+Shift+P > Open User Settings > Paste the following inside { }

"terminal.integrated.profiles.windows": {
    "Git Bash": {
    "path": "D:\\Git\\bin\\bash.exe", 
    "icon": "terminal-bash"
    }
},
"terminal.integrated.defaultProfile.windows": "Git Bash",
"git.enabled": true,
"git.path": "D:\\Git\\bin\\bash.exe",
"terminal.integrated.shell.windows": "D:\\Git\\bin\\bash.exe"

If you are in a Workspace!

Ctrl+Shift+P > Open Workspace Settings > Paste the following inside "settings": { }:

"terminal.integrated.profiles.windows": {
    "Git Bash": {
    "path": "D:\\Git\\bin\\bash.exe",  
    "icon": "terminal-bash"
    }
},
"terminal.integrated.defaultProfile.windows": "Git Bash",
"git.enabled": true,
"git.path": "D:\\Git\\bin\\bash.exe",
"terminal.integrated.shell.windows": "D:\\Git\\bin\\bash.exe"

Finally close all of your Vscode windows/instances, reopen and you're good to go.

With regard to leonbloy and soulshined solutions!

William Le
  • 825
  • 1
  • 9
  • 16
1

I happen to be consulting for a Fortune 500 company and it's sadly Windows 7 and no administrator privileges. Thus Node.js, Npm, Visual Studio Code, etc.. were pushed to my machine - I cannot change a lot, etc...

For this computer running Windows 7:

Below are my new settings. The one not working is commented out.

{
    "update.channel": "none",
    "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe"
    //"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\bash.exe"
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tom Stickel
  • 19,633
  • 6
  • 111
  • 113
1

I tried the above answers as of vscode 1.42.1 and they worked inasmuch as to get me a git bash terminal. So, bottom line this setting works just for opening a bash shell from terminal:

"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe"

However it has the unwanted side effect of also being the shell used to build things and that breaks the MS C++ chain because the \ character used for path separator is understood by bash as an escape character. The complete fix for me then required me to add this extra variable, setting it to powershell:

"terminal.integrated.automationShell.windows": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"

Now, I can have my bash terminal and Ctrl-Shift-B or F5 work without problems.

Oh, and as other posters mentioned, the source for this information is VSCode's documentation.

luv2learn
  • 606
  • 8
  • 12
1

To open a terminal in Visual Studio Code you do not need to use Ctrl + ` as recommended everywhere, since it's highly inconvenient finger operation. A better solution would be this:

Ctrl + j
Maksym Dudyk
  • 1,082
  • 14
  • 16
0

If you already have "bash", "powershell" and "cmd" CLI's and have correct path settings then switching from one CLI to another can done by the following ways.

Ctrl + ' : Opens the terminal window with default CLI.

bash + enter : Switch from your default/current CLI to bash CLI.

powershell + enter : Switch from your default/current CLI to powershell CLI.

cmd + enter : Switch from your default/current CLI to cmd CLI.

VS Code Version I'm using is 1.45.0

Community
  • 1
  • 1
SridharKritha
  • 8,481
  • 2
  • 52
  • 43
0

In the latest stable build, i.e. 1.54, Disabling ConPTY solved my problem. I am writting this incase it does solve yours too.

{

 "terminal.integrated.shell.windows": "C:\\path\\to\\bin\\bash.exe",
 "terminal.integrated.windowsEnableConpty": false
}
0

Add this to settings.json

{
    "terminal.integrated.profiles.windows": {
        "PowerShell": {
            "source": "Git Bash",
            "icon": "terminal-bash"
        },
Joe Shakely
  • 623
  • 7
  • 9
0

Update VSCode 1.77 March 2023

leonbloy's answer uses terminal.integrated.profiles.windows with GitBash.path explicitly filled out.

With VSCode 1.77, based on vscode-docs issue 6108, the issue 176062 will help to set up git-bash (see PR 176063).

Detect git bash installs by searching PATH

This is available in VSCode Insider today.


Note that, depending on the PATH where Git is installed, it risks triggering:

Unsafe profile detection

Certain shells are installed in unsafe paths by default, like a path that could be written to by another user on a Windows environment.
VS Code will still detect these, but not expose them as a proper profile until they have been explicitly configured via the Terminal: Select Default Profile command.
When configuring an unsafe profile, there will be a warning before it's added:

https://code.visualstudio.com/assets/docs/terminal/profiles/unsafe-profile-warning.png

This terminal uses a potential unsafe path that can be modified by another user.
Are you sure you want to use it?

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250