[visual-studio-code] How do I use Bash on Windows from the Visual Studio Code integrated terminal?

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?

This question is related to visual-studio-code git-bash vscode-settings

The answer is


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 - [email protected] - 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"


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


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.


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.


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.


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!

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"
],

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


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


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.


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.


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!)


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.


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.


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.


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"
}


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


Examples related to visual-studio-code

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined raised when starting react app Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error Cannot edit in read-only editor VS Code How to setup virtual environment for Python in VS Code? Pylint "unresolved import" error in Visual Studio Code Why do I keep getting Delete 'cr' [prettier/prettier]? How to set up devices for VS Code for a Flutter emulator VSCode single to double quote automatic replace js 'types' can only be used in a .ts file - Visual Studio Code using @ts-check How can I clear the terminal in Visual Studio Code?

Examples related to git-bash

"Permission Denied" trying to run Python on Windows 10 Adding Git-Bash to the new Windows Terminal How do I use Bash on Windows from the Visual Studio Code integrated terminal? How can I change the user on Git Bash? Change drive in git bash for windows Running .sh scripts in Git Bash Set an environment variable in git bash Python not working in the command line of git bash Change the location of the ~ directory in a Windows install of Git Bash Username and password in command for git push

Examples related to vscode-settings

VSCode single to double quote automatic replace How to add a new project to Github using VS Code How do I use Bash on Windows from the Visual Studio Code integrated terminal? Color theme for VS Code integrated terminal How to restart VScode after editing extension's config? Moving Panel in Visual Studio Code to right side How do you format code on save in VS Code Is there a quick change tabs function in Visual Studio Code? How to reset settings in Visual Studio Code? How to change environment's font size?