Using msys shell in Visual Studio Code
Asked Answered
C

7

12

I'm getting into Visual Studio Code as I like its lightweight design and flexibility. It has an integrated terminal that can run, I believe, any shell [see here]. I'd like to use it run the msys shell that I've been using lately. Setting the terminal to run another shell other than the default powershell.exe should be as simple as changing the settings as described [here]. However, I use the msys shell by running the msys.bat file, as recommended on mingw.org.

So my question is, where is the .exe file I need to set the terminal to the msys shell? Or is running this shell without the .bat in this fashion impossible? Thanks!

Colligan answered 9/4, 2017 at 3:55 Comment(1)
Possible duplicate of How do I integrate MSYS2 shell into Visual studio code on Window?Alec
D
28

According to the msys.bat script, the actual executable that is launched depends on your TTY settings. The default setting uses the native command prompt and launches the sh.exe file as you can see from the following snippet:

:startsh
if NOT EXIST %WD%sh.exe goto notfound
start %WD%sh --login -i
exit

To get this to work in Visual Studio Code, you will need to add the following user settings:

"terminal.integrated.shell.windows": "C:\\MinGW\\msys\\1.0\\bin\\sh.exe",
"terminal.integrated.shellArgs.windows": ["--login", "-i"]

The path to the sh.exe file may be different depending on your install location for MSYS.

EDIT: (2019-01-20)

While the above still works for MSYS v1.0, I have since switched over to MSYS2 (https://www.msys2.org/). You can use the following settings to setup your Visual Studio Code to work with MSYS2 just as for v1.0 (once again, your install location might be different than mine):

"terminal.integrated.shell.windows": "C:\\msys64\\usr\\bin\\bash.exe",
"terminal.integrated.shellArgs.windows": ["--login", "-i"]

For extra-credit, we are going to modify an environment variable so that we always open the terminal in the current directory. There are various ways to accomplish this via scripting etc. However, the simplest way to do it, in my opinion, is to use the CHERE_INVOKING environment variable. By setting this flag to 1 it will inform the shell to use the current directory as the default entry point. Here is a complete tutorial on how to enable this flag:

  1. Press the windows key and type run and hit enter
  2. In the run dialog, type the following:

    rundll32.exe sysdm.cpl,EditEnvironmentVariables
    

    Run Dialog to open Environment Variables

  3. In the Environment Variables dialog that is opened, add a new user variable called CHERE_INVOKING and set it's value to 1.

    enter image description here

With this flag enabled in the Windows system, it should automatically open the terminal from the location where you called the bash.exe executable. For Visual Studio Code, this will be your root project directory. Enjoy!

Damned answered 5/5, 2017 at 19:15 Comment(11)
That's awesome! Thanks @JonnyMorrill. Now, here's a hard one for ya: How can I make the MSYS shell automatically cd to the root directory of whatever VS Code project I'm in at the time I launch the terminal?Huambo
@Huambo Question was asked long time ago, but to achive what you are asking (i.e. cd in the directory of VS Code project), please see my anser below: https://mcmap.net/q/888167/-using-msys-shell-in-visual-studio-codeCeballos
@Huambo - My apologies, I did not notice this for quite some time. I updated the answer with another solution I am currently using to enable this feature.Damned
@Jean-FrancoisT. - Very nice solution! I added another way to accomplish this by setting an environment variable flag. Yours is quite nice for users who would rather stay away from editing environment variables.Damned
@JonnyMorrill Interesting approach. 2 remarks: #1. there is a typo (varaible instead of variable) in 3rd step # 2. why doing run > ENTER and not just pressing Windows Key and pasting the command?Ceballos
@Jean-FrancoisT. - I was not aware that you could simply paste a command immediately after pressing the windows key. Thanks for the tip and the proof reading.Damned
Belated thanks @JonnyMorrill & @Jean-FrancoisT. The CHERE_INVOKING env var solution turned out to be the easiest for me. Now, if there was only some way to configure multiple different shells — say MSYS2, Windows cmd and git bash for example — and be able to simply select one or the other at will without having to edit settings every time I want to use a different shell in vscode; that would be the ultimate. I might ask about that in a separate question one day. Also, I found that the Windows key + paste deal doesn't work for me in Windows 7. That must be a Windows 10 thing I'm guessing.Huambo
@Huambo - If you want to have multiple types of shells open and managed by a single interface, I suggest looking into "ConEmu" (conemu.github.io). I use it for having multiple shells open outside of VSCode as it manages them quite well. I know this doesn't solve your issue of having multiple shells in VSCode, but it does work well as an alternative.Damned
Cheers @JonnyMorrill. Very impressive tool. I will probably install that soon.Huambo
You can use "terminal.integrated.env.windows" instead of adding the environment variables directly to the system. There you can also set MSYSTEM to use MINGW64 for example instead of plain MSYS.Prim
I installed "Git for Windows" instead of pure MSYS, and it worked with this path: "C:\\Program Files\\Git\\bin\\bash.exe". No shellArgs needed.Breastbone
T
21

according to MSYS terminals, we can use this:

    "terminal.integrated.profiles.windows": {
        "msys mingw64": {
            "path": "C:/msys64/msys2_shell.cmd",
            "args": [
                "-defterm",
                "-here",
                "-no-start",
                "-mingw64"
            ]
        }
    },

this starts msys mingw64. to start other shell, just change -mingw64 to -mingw32 or -msys.

Tartrazine answered 8/1, 2022 at 3:30 Comment(5)
this one that actually works! Thank you! Just add the snippet above in .vscode/settings.json or open the command-palette (cmd-shift-p) & type 'open settings (JSON)' to add it to the system/user settings.Montemayor
I can confirm this was the only way I got it to work under version 1.79.2. However, how do I set the runtime library path for that shell? I'm unable to run anything I build as it can't find the dependent DLLs.Morn
I add the library path to $Env:Path . You can set it in PowerShell's $profile . Or you can write a simple python script which parse ldd's output and copy the required dll to build folder. @MornTartrazine
Why Powershell's profile, considering it's MSYS2 bash shell that's being called? The PATH variables look very different in both cases.Morn
I use PowerShell to run the built executables.Tartrazine
G
5

An extension to the previous answer:

To get it to open into your current working directory, for some reason it has to run it through a native shell first. We can use an incredibly intuitive PowerShell wrapper to set the cd and start bash or another shell.

Here's the config that got it working for me:

"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
"terminal.integrated.shellArgs.windows": [
    "C:\\msys64\\usr\\bin\\sh.exe",
    "--login",
    "-i",
    "-c",
    "(\"cd \\`\"``cygpath '{0}'``\\`\"; /usr/bin/bash --login -i\" -f (Resolve-Path .).Path )"
],

You can replace bash with a different shell if you like, for example zsh.

Bash and JSON escapes use the \ character, but PowerShell uses ` instead, which bash would then attempt to use to execute a command, so be careful with all the escape characters.

Also, you have to specify the PowerShell binary, as opposed to the built-in "PowerShell" shell. I honestly have no idea why.

Gestate answered 11/4, 2018 at 15:57 Comment(0)
C
3

To open MSys in the folder of your project, you just have to

A. Add the following setting in your personal settings of Visual Code (assuming your Msys folder is C:\msys\1.0):

"terminal.integrated.shell.windows": "C:\\msys\\1.0\\Run_MSYS.bat",

B. Copy getcp.exe in the bin folder of MSys (i.e. C:\msys\1.0\bin); you can find it here: https://github.com/msysgit/msysgit/blob/master/mingw/bin/getcp.exe

C. Create the file Run_MSYS.bat in your MSys folder as follows:

@rem Do not use "echo off" to not affect any child calls.
@SETLOCAL
@SETLOCAL ENABLEEXTENSIONS

:: Figure out where msys's root folder. If you want, you could just add the folder in the line
:: below.
@set MSYSROOT=
@if "x%MSYSROOT%"=="x" @if exist "%~dp0msys.bat" @set MSYSROOT=%~dp0
@if "x%MSYSROOT%"=="x" @if exist "%~dp0.msys-root" @set /P MSYSROOT=<%~dp0.msys-root
@if "x%MSYSROOT%"=="x" (
@echo Could not locate your mysys root folder.
@set /P MSYSROOT=Location:
)
:: Read as MSYSROOT.trim()
@if not "x%MSYSROOT%"=="x" (
@for /f "tokens=* delims= " %%a in ("%MSYSROOT%") do @set MSYSROOT=%%a
@for /f "useback tokens=*" %%a in ('%MSYSROOT%') do @set MSYSROOT=%%~a
@if not "%MSYSROOT:~-1%"=="\" @set MSYSROOT=%MSYSROOT%\
)
:: Verify that root folder exists
@if not exist "%MSYSROOT%" (
@echo "%MSYSROOT%" is not a valid folder. Please check for .msys-root in %~dp0, or if you entered the path above, please rerun this script and select a valid folder.
@exit /B 1
) else (
@if not "%MSYSROOT%"=="%~dp0" @echo %MSYSROOT%>%~dp0.msys-root
)

:: Home Folder
:: If you'd prefer the home directory set to your C:\Users\Username folder, uncomment the two lines
:: below.
@rem @if not exist "%HOME%" @set HOME=%HOMEDRIVE%%HOMEPATH%
@rem @if not exist "%HOME%" @set HOME=%USERPROFILE%
@if not exist "%HOME%" @if not "%MSYSROOT%"=="" @set HOME=%MSYSROOT%home\%USERNAME%
@if not "x%WD%"=="x" @set WD=
@set PLINK_PROTOCOL=ssh
@if not exist "%WD%msys-1.0.dll" @set WD=%MSYSROOT%\bin\
@set MSYSCON=sh.exe

:: Default action, open msys and go to the current folder.
@set OLDCD=%CD%
@if not "x%OLDCD%"=="x" @set CURRCD=%CD%
:: Get the current console ("OEM") codepage.
@for /f %%i in ('"%MSYSROOT%bin\getcp.exe" -oem') do @set cp_oem=%%i
:: Get the current GUI ("ANSI") codepage.
@for /f %%i in ('"%MSYSROOT%bin\getcp.exe" -ansi') do @set cp_ansi=%%i
:: Set the console codepage to match the GUI codepage.
@chcp %cp_ansi% > nul < nul
@if not "x%OLDCD%"=="x" (
@"%MSYSROOT%bin\bash.exe" -l -i -c "cd \"$CURRCD\"; exec /bin/bash -rcfile ~/.bash_profile"
) else (
@"%MSYSROOT%bin\bash.exe" -l
)
:: Store the error level returned by bash.
@set ErrorLevel=%ErrorLevel%
:: Restore the original console codepage.
@chcp %cp_oem% > nul < nul
:: If we had a current directory at the store of the script, go back to it.
@if not "x%OLDCD%"=="x" chdir /D "%OLDCD%"

:: quit script with the current error level.
@exit /b %ErrorLevel%

This is a shorter version of the script found (probably) here: https://gist.github.com/juntalis/1291621

Thank you Juntalis for the great script!!

Cheers

Ceballos answered 28/6, 2018 at 9:3 Comment(0)
A
1

The answer from Jonny Morril helped me. When setting up, it will automatic go to the home folder any time.

Here I found a working solution for the explorer menu. But you don't need to implement this. The import line is:

If you try it out, you will notice that it opens a new MSYS window, but it automatically goes to the home directry instead of the current directory you're in. To fix this you need to edit "C:\MinGW\msys\1.0\etc\profile". Find the cd "$HOME" line (probably the last line) and comment it out (by adding a # before it).

After removing/comment-out the line, everything works perfectly, wihtout any work-around, like Bryan K suggestion.

Ansilma answered 15/6, 2018 at 9:52 Comment(0)
T
1

To further refine Bryan K's answer:

We'll instead add an MSYS2 profile to the terminal profiles drop down. In settings.json, in "terminal.integrated.profiles.windows", we'll add a new entry for MSYS2, as such:

"MSYS2": {
  "source": "PowerShell",
  "args": [
    "C:\\msys64\\usr\\bin\\bash.exe",
    "--login",
    "-i",
    "-c",
    "(\"cd \\`\"``cygpath '{0}'``\\`\"; /usr/bin/bash --login -i\" -f (Resolve-Path .).Path )"
  ]
}

Change the path to match your MSYS2 install (the one above is the default).

Now there will be an MSYS2 option in the terminal drop down, and it will open in the current working directory.

Temporary answered 10/9, 2021 at 1:12 Comment(0)
I
0

Today I have encountered the same problem, and at the current date(24/10/2021) this is the working solution for me: In file settings.json you should have this lines:

"terminal.integrated.env.windows": {
    "CHERE_INVOKING": "1" // open mysys terminal at the current path
},
"terminal.external.windowsExec": "C:\\msys64\\mysys2.exe",
"terminal.integrated.defaultProfile.windows": "MYSYS",  // I am using default MYSYS(this line is optional)
"terminal.integrated.profiles.windows": {
    "MYSYS": {
        "path": "C:\\msys64\\usr\\bin\\bash.exe",
        "args": [
            "--login",
            "-i"
        ]
    }
}
Illiteracy answered 23/10, 2021 at 21:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.