Skip to content

Instantly share code, notes, and snippets.

@dhkatz
Last active June 23, 2024 22:09
Show Gist options
  • Save dhkatz/106891324c9624074a84d11e2691144b to your computer and use it in GitHub Desktop.
Save dhkatz/106891324c9624074a84d11e2691144b to your computer and use it in GitHub Desktop.
Using MSYS2 with Visual Studio Code

Using MSYS2 with Visual Studio Code is extremely easy now thanks to the Shell Launcher extension by Tyriar.

First, install the extension and reload Visual Studio Code.

Then, open the settings.json to edit your settings.

Add the field shellLauncher.shells.windows. I recommend using autocompletion here so that all the default shells are added.

You should having something like this now:

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

Now you have to add the MSYS2 bash to the list of shells.

Add this shell into the list. The path to your installing of MSYS2 may be different. Just find the path to bash.exe in /usr/bin/.

{
    "shell": "C:\\msys64\\usr\\bin\\bash.exe",
    "label": "MSYS2",
    "args": ["--login", "-i"],
    "env": {
        "MSYSTEM": "MINGW64",
        "CHERE_INVOKING": "1",
        "MSYS2_PATH_TYPE": "inherit"
    }
}

You can choose to remove MSYS2_PATH_TYPE if you don't want your shell to inherit all the existing PATH settings from Windows.

@nikitalita
Copy link

in the new vscode, shellLauncher.shells.windows no longer exists; it's terminal.integrated.profiles.windows now:
here's the default values:

    "terminal.integrated.profiles.windows": {
        "PowerShell": {
            "source": "PowerShell"
        },
        "Command Prompt": {
            "path": [
                "${env:windir}\\Sysnative\\cmd.exe",
                "${env:windir}\\System32\\cmd.exe"
            ],
            "args": []
        },
        "Git Bash": {
            "source": "Git Bash"
        },
        
    },

"PowerShell" and "Git Bash" are aliases to internal profile sources (apparently the only ones); you can overide them by just removing "source" and setting "path" and "arguments" in here, and you can

@brendan8c
Copy link

brendan8c commented May 6, 2021

in the new vscode, shellLauncher.shells.windows no longer exists; it's terminal.integrated.profiles.windows now:

If you delete the line shellLauncher.shells.windows then at opening VSCode will use terminal "PowerShell"!!!
Command terminal.integrated.defaultProfile.windows does not work like shellLauncher.shells.windows They are different!
photo

You say that the team terminal.integrated.profiles.windows now replaces the command terminal.integrated.shell.windows but this is not so!

@slvdrvlc
Copy link

currently it served me changing "shell" by "path" and giving it a name, I also installed fish and I prefer to use that, I share what I achieved because I take all afternoon hehehe

"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"icon": "terminal-powershell"
},
"Command Prompt": {
"path": [
"${env:windir}\Sysnative\cmd.exe",
"${env:windir}\System32\cmd.exe"
],
"args": [],
"icon": "terminal-cmd"
},
"Bash": {
"path": ["C:\laragon\bin\git\bin\bash.exe"]
},
"MSYS2": {
"path": "C:\msys64\usr\bin\bash.exe",
"label": "MSYS2",
"args": ["--login", "-i"],
"env": {
"MSYSTEM": "MINGW64",
"CHERE_INVOKING": "1",
"MSYS2_PATH_TYPE": "inherit"
}
},
"FISH": {
"path": "C:\msys64\usr\bin\fish.exe",
"label": "MSYS2",
"args": ["--login", "-i"],
"env": {
"MSYSTEM": "MINGW64",
"CHERE_INVOKING": "1",
"MSYS2_PATH_TYPE": "inherit"
}
},

@Neonit
Copy link

Neonit commented Feb 18, 2022

No need for an extension anymore due to the native interated terminal profiles. But this is still useful to get the configuration right. @slvdrvlc posted it already, but it's not so well readable.

Actually, all you have to do now is to add an entry to the "terminal.integrated.profiles.windows" setting in settings.json (if it is not there, add it and let it autocomplete):

"MSYS2 Bash": {
  "path": "C:\\msys64\\usr\\bin\\bash.exe",
  "args": ["--login", "-i"],
  "env": {
      "MSYSTEM": "MINGW64",
      "CHERE_INVOKING": "1",
      "MSYS2_PATH_TYPE": "inherit"
  }
}

@brendan8c
Copy link

I don't use this extension.
The default terminal in my VSCode is Git Bash
Here's what it looks like.

  "terminal.integrated.defaultProfile.windows": "Git Bash",
  "terminal.integrated.profiles.windows": {
    "Git Bash": {
      "path": "C:\\Program Files\\Git\\bin\\bash.exe",
      "args": ["--login"]
    }
  }

@tiagofrancafernandes
Copy link

I don't use this extension. The default terminal in my VSCode is Git Bash Here's what it looks like.

  "terminal.integrated.defaultProfile.windows": "Git Bash",
  "terminal.integrated.profiles.windows": {
    "Git Bash": {
      "path": "C:\\Program Files\\Git\\bin\\bash.exe",
      "args": ["--login"]
    }
  }

Good

@tiagofrancafernandes
Copy link

For msys64 you can use

    "terminal.integrated.profiles.windows": {
        "msys64 - bash": {
            "path": "C:\\msys64\\usr\\bin\\bash.exe"
            // ,"args": [
            //     "-arg1",
            //     "-ar2"
            // ]
        }
    },
    "terminal.integrated.defaultProfile.windows": "msys64 - bash"

@PanicNOR
Copy link

PanicNOR commented Jan 22, 2024

the "args": ["--login", "-i"], method didn't work for me because of the "-i" args..
here I use cmd.exe to call "msys2_shell.cmd"..

"terminal.integrated.profiles.windows": {
    "MSYS64 UCRT (cmd + zsh)": { 
      "path": "cmd.exe",
      "args": [
        "/c",
        "C:\\msys64\\msys2_shell.cmd -defterm -here -no-start -ucrt64 -shell zsh"
      ],
      "env": {
        "MSYSTEM": "UCRT64",
        // "CHERE_INVOKING": "1", //- Default way
        "PATH": "/ucrt64/bin:/usr/bin:/usr/local/bin:/bin:${env:PATH}" //- custom path, force-search "/ucrt64/bin" path first.
      }
    }
},
"terminal.integrated.env.windows": { 
    "MSYSTEM": "UCRT64",
    // "CHERE_INVOKING": "1", //- Default way
    "PATH": "/ucrt64/bin:/usr/bin:/usr/local/bin:/bin:${env:PATH}"
},
"terminal.integrated.defaultProfile.windows": "MSYS64 UCRT (cmd + zsh)",

@prashant-kiit
Copy link

Thanks @tiagofrancafernandes. It worked.

@ThisNoName
Copy link

@PanicNOR

the "args": ["--login", "-i"], method didn't work for me because of the "-i" args.. here I use cmd.exe to call "msys2_shell.cmd"..

Trying to setup fish shell on a new machine, had msys2_shell.cmd worked but looking for a cleaner solution. Always had the -i and never thought about it, until stumbled upon your comment. In the end, this worked for me. Thanks.

  // fish shell integration
  "terminal.integrated.defaultProfile.windows": "fish",
  "terminal.integrated.profiles.windows": {
    "fish": {
      "path": "C:\\msys64\\usr\\bin\\fish.exe",
      "args": ["--login"],
      "env": {
        "MSYSTEM": "MINGW64",
        "CHERE_INVOKING": "1",
        "MSYS2_PATH_TYPE": "inherit"
      }
    },
    // alternative integration via msys2_shell.cmd
    // "MSYS2 Fish": {
    //   "path": "cmd.exe",
    //   "overrideName": true,
    //   "icon": "terminal-cmd",
    //   "args": [
    //     "/c",
    //     "C:\\msys64\\msys2_shell.cmd -defterm -here -no-start -ucrt64"
    //   ]
    // },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment