60

I would like to use Cygwin as the integrated terminal on Visual Studio Code on my Windows laptop (as this would give me use of the Linux terminal commands git and G++, etc.) but when I set the value for "terminal.integrated.shell.windows": to the address of the Cygwin application (.exe) then it opens a new Cygwin terminal rather than remaining in VS Code.

So my question is: can I use Cygwin integrated into the VS Code terminal and use that to use commands on it (mkdir, rm, etc.) but also use git commands and use it as an integrated compiler and debugger (for generically but for C++ at least)? And how would I go about this?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user2766296
  • 599
  • 1
  • 5
  • 9
  • 2
    Do you have Windows 10 on your computer? If so, you can just use WSL instead of Cygwin. – ifconfig Sep 05 '17 at 22:58
  • Besides, @user2766296 I don't believe VS Code works with Cygwin as a terminal anyway. – ifconfig Sep 06 '17 at 15:55
  • 1
    @ifconfig, it does. I just tried it (see answer below), and it works great. Even shell colors (.dircolors) work. – Steven Volckaert Nov 22 '17 at 07:17
  • Cygwin is not a **terminal**, so it's meaningless to ask for _using Cygwin as terminal_. You can use Cygwin programs **inside** a terminal of course, and the Cygwin distribution indeed provides some terminals for this purpose (I'm using for instance `mintty`), but you can equally well using some other terminal program for your purpose (`ConEmu` works well). – user1934428 Apr 04 '22 at 10:11

9 Answers9

75

These config settings work for me:

{
  // start bash, not the mintty, or you'll get a new window
  "terminal.integrated.shell.windows": "C:\\cygwin\\bin\\bash.exe",
  // Use this to keep bash from doing a 'cd ${HOME}'
  "terminal.integrated.env.windows": {
    "CHERE_INVOKING": "1"
  },
  // Make it a login shell
  "terminal.integrated.shellArgs.windows": [
    "-l"
  ],
}
Davis Herring
  • 36,443
  • 4
  • 48
  • 76
Rick Renshaw
  • 799
  • 4
  • 3
23

Since VS Code 1.55 (March 2021), you can use terminal profiles.

  1. Select File > Preferences > Settings
  2. Select the Open Settings (JSON) icon (top right, same level as tabs)
  3. Copy and paste the code below inside the top-level curly braces {}
  "terminal.integrated.profiles.windows": {
    "Cygwin": {
      "path": "C:\\cygwin\\bin\\bash.exe",
      "args": ["--login"],
      "env": {"CHERE_INVOKING": "1"}
    }
  }

If you have 64-bit, your Cygwin path may need to be:

        "path": "C:\\cygwin64\\bin\\bash.exe",

To open the Cygwin terminal

  1. Press F1
  2. Type: Terminal: Create New Terminal (with Profile)
  3. Select Cygwin
DBolton
  • 506
  • 4
  • 9
tony
  • 431
  • 4
  • 12
16

You could just call the Cygwin.bat without ENV issue:

{
    // Replace with your Cygwin.bat file path 
    "terminal.integrated.shell.windows": "C:\\cygwin64\\Cygwin.bat",
}

Make sure the BAT scripts fit to your Cygwin.

Nick Tsai
  • 3,799
  • 33
  • 36
14

Combining above answers, this is my working configuration.

{
    "terminal.integrated.shell.windows": "C:\\cygwin\\bin\\bash.exe",
    "terminal.integrated.env.windows": {
        "CHERE_INVOKING": "1"
    },
    "terminal.integrated.shellArgs.windows": [
        "--login",
        "-i"
    ],
}

{tested at ubuntu 18.04lts, running Windows 7 ultimate 32bt in Virtualbox 5.2.12}

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • 3
    Bash options: `-i Force the shell to run interactively`. `--login Make this shell act as if it had been directly invoked by login. When the shell is interactive, this is equivalent to starting a login shell with ‘exec -l bash’. When the shell is not interactive, the login shell startup files will be executed. ‘exec bash -l’ or ‘exec bash --login’ will replace the current shell with a Bash login shell.` – Danijel Dec 10 '18 at 13:08
  • 2
    For 64-bit Cygwin, remember to place Cygwin with cygwin64, but otherwise, this seems to work so far. – Hashim Aziz Aug 27 '19 at 16:50
  • a correct configuration in official website of visual studio code: https://code.visualstudio.com/docs/editor/integrated-terminal – Chi Cuong Le Jan 11 '21 at 12:22
  • This warns that you are using a deprecated setting, but I couldn't find a work around. – Daniel Kaplan Jul 13 '21 at 23:14
  • 1
    @DanielKaplan, the "work around" is to use the newer VSCode settings of `terminal.integrated.profiles.windows` and `terminal.integrated.defaultProfile.windows` as noted in @tony's answer (which of course came after your comment). – Matthew Oct 31 '22 at 16:03
8

VS Code only allows you to set one default terminal configuration at a time and as its likely that users would want multiple shells to be available at any time like CMD, Powershell and Cygwin Bash, it would be best to use an Visual Studio Code Extension called Shell Launcher.

This tool will allow you to launch any number of shells at any time. First you need to reassign the CTRL-SHIFT-T hotkey to shellLauncher or use a different unused hotkey.

Then, go into your settings.json for VS Code and add the following block:

"shellLauncher.shells.windows": [
  {
    "shell": "C:\\Windows\\System32\\cmd.exe",
    "label": "cmd"
  },
  {
    "shell": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
    "label": "PowerShell"
  },
  {
    "shell": "C:\\cygwin\\bin\\bash.exe",
    "args": ["-l"],
    "env": {"CHERE_INVOKING": "1"},
    "label": "Cygwin Bash"
  }
],

Note: alter paths above as required

Now when you hit the hotkey you assigned, you will get a dropdown of the available terminals configured.

Timothy C. Quinn
  • 3,739
  • 1
  • 35
  • 47
5

For VS Code v1.60 works the following approach:"

    "terminal.integrated.profiles.windows": {
      "Cygwin": {
        "source": "PowerShell",
        "args": ["C:\\cygwin\\cygwin.bat -i /Cygwin-Terminal.ico -"]
       }
     }

As disadvantage: opened directory will be the root folder.

AlexK
  • 59
  • 1
  • 6
1

If you take out the following part the terminal starts in the project you have open.

// Make it a login shell
/*"terminal.integrated.shellArgs.windows": [
    "--login"
  "-l"
]*/,
Spilvergo
  • 31
  • 4
1

Add these lines to your settings.json file

"terminal.integrated.defaultProfile.windows": "Cygwin",
     "terminal.integrated.profiles.windows": {
          "Cygwin": {
               "path": "C:\\cygwin64\\bin\\bash.exe",
               "args": [
                    "--login"
               ],
               "icon": "code"
          }
     }

Now when you create a new terminal window it should open Cygwin.

Tejas
  • 19
  • 2
0

Note that with VSCode 1.75 (Jan. 2023), you will have a confirmation step, when using a Cygwin terminal.

See:

  • issue 167721 ("Bring back unsafe terminal profile paths with a confirmation step")
  • PR 170193 ("Support detecting terminal profile with potentially unsafe paths and add an opt-in")

Some background on this: there was a CVE recently about picking up shells with paths that could be written to by other users on Windows in a shared user environment.
The cygwin profile was removed and the git bash profile lost a single path it used to pick up:

C:\Cygwin64\bin\bash.exe
C:\Cygwin\bin\bash.exe
C:\ProgramData\scoop\apps\git-with-openssh\current\bin\bash.exe

This change adds the ability to pick unsafe paths up as a "detected profile", similar to Windows PowerShell when PowerShell is not installed, it will not show up in new terminal with profile list by default:

https://user-images.githubusercontent.com/2193314/209881424-eb51a6d5-50f4-4d52-b168-b49c1a15e67c.png

But we do make it more convenient to set up:

https://user-images.githubusercontent.com/2193314/209881440-34c64b8a-b78f-4141-bf29-806d929c3a63.png

https://user-images.githubusercontent.com/2193314/209881451-4b779ddd-e3b2-4ace-bfa7-5d4d4e4dcc5e.png

From there the user can select the item to make it the default or click the gear to just add it to settings.

This is what that looks like now if you have Cygwin or MSYS2 (new) installed:

https://user-images.githubusercontent.com/2193314/209881528-6ab3bad6-b3c4-45e5-b65d-dcebee912600.png

https://user-images.githubusercontent.com/2193314/209881547-c67381d9-4123-437a-906f-9814c69e5041.png

Notice the warning icon next to the exe as an extra indicator that this one is special. When you click on either the profile or the gear, it will present this notification:

https://user-images.githubusercontent.com/2193314/209882438-9a664bd2-19e2-4070-926b-3ace8fa06ade.png

When accepting:

https://user-images.githubusercontent.com/2193314/209882468-a16d9dfa-7f36-4801-9860-1b535394c151.png

This is available in VSCode insiders today.

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