3

[EDITED to add:] It turns out that the actual problem here was a brainless typo I had made. The answer to my actual question is "yes, you just do the obvious thing and it works, if you aren't a moron". I'm accepting the answer that basically says that, but maybe in fact the question should be deleted.

My employer's standard setup for software development on Windows involves using MSYS. (Either traditional MSYS or MSYS2.) Is there a way to run the shell for either of these inside the new Windows Terminal?

Traditional MSYS has a Windows batch script that arranges to execute sh.exe inside whatever terminal it's being run from. You might therefore think that making a new profile in WT's profiles.json, with "commandline": "path\to\sh.exe", would do the trick. Alas, no: if I do this and ask WT for a terminal of that type, I get a plain old cmd prompt instead. (Starting in C:\WINDOWS\System32.) [EDITED to add:] Duh, not-alas, yes: if I do this then it basically does work, but this gets wrong a couple of details that the accepted answer gets right.

The thing you run to get an MSYS2 prompt is a Windows executable instead of a batch script as in traditional MSYS, but it too launches its own sh.exe. Same thing happens with that.

I guess what's going on here is that the MSYS sh.exe is just expecting to interact via stdin and stdout, whereas an executable suitable for running directly from WT needs to understand the rather complicated Microsoft console APIs, or something like that.

I've also tried, with little optimism and no success (they all produce the same result as above), the following things:

"commandline": "cmd path/to/sh.exe"
"commandline": "cmd /c path/to/sh.exe"
"commandline": "cmd path/to/msys2_shell.cmd"
"commandline": "cmd /c path/to/msys2_shell.cmd"

Is there any convenient way to get an MSYS shell running inside a Windows Terminal?

  • Some confusion. Which environment do you want to run in Windows Temrinal, msys or msys2? msys is kind of old. – Biswapriyo Nov 27 '19 at 16:13
  • I would be happy with either. As you say, traditional MSYS is pretty long in the tooth these days, but for my purposes it would be OK. – Gareth McCaughan Nov 27 '19 at 16:15

2 Answers2

4

Assuming MSYS2 is installed in C:\msys2 folder, the commandline entry for Windows Terminal will be C:\msys2\usr\bin\bash.exe -i -l. Hence the entry in profile.json will be:

{
    // Make changes here to the MSYS2 profile
    "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
    "name": "msys2",
    "commandline": "C:\\msys2\\usr\\bin\\bash.exe -i -l",
    "hidden": false
},

The command launches bash.exe from MSYS2 environment with --interactive and --login options. Those options are necessary to set up environment specific to MSYS2 only. For more information about how to add entry in Windows Terminal profile.json file, see this documentation from Microsoft Temrminal GitHub repository.

Biswapriyo
  • 10,831
  • 10
  • 47
  • 78
  • For me, this produces the exact same result as the other very similar things I tried before (using `sh.exe` rather than `bash.exe`, and without `-i -l`). I didn't use the exact same GUID as yours but I assume that doesn't need anything other than to be unique. Have you tried this and found that it works? – Gareth McCaughan Nov 27 '19 at 18:35
  • @GarethMcCaughan I can see the prompt. What command did not work, any simple example? – Biswapriyo Nov 27 '19 at 18:40
  • I'm not sure what you mean by "what command did not work". When I put an entry just like yours into my `profiles.json` and select the "MSYS" entry from the dropdown menu, it gives me not an MSYS shell session but a `cmd` prompt. – Gareth McCaughan Nov 27 '19 at 18:44
  • ... but, hmmmmm, when I copy-and-paste yours in and make the necessary edits, I _do_ get an MSYS shell session, so clearly I have done something amusingly wrong somewhere. Investigating further now. – Gareth McCaughan Nov 27 '19 at 18:46
  • ... Ugh. So, the answer is that I am a moron. I had mistyped "commandline" as "commandLine" and so of course nothing worked. – Gareth McCaughan Nov 27 '19 at 18:49
  • I'm accepting your answer because (duh) indeed if you do what it says then it works. If I'd actually done what _I_ said then it would have kinda-worked too, but I hadn't included the `-i -l` flags so you've actually answered a question I didn't get far enough to know I needed to ask :-). – Gareth McCaughan Nov 27 '19 at 18:57
  • The linked documentation has been moved to another link as given in the github link. Would be great if it is edited. https://docs.microsoft.com/en-us/windows/terminal/customize-settings/startup – devesh Feb 20 '21 at 17:19
4

MSYS2 has their own guide to add MSYS2 to Windows Terminal. It uses msys2_shell.cmd instead of run the shell manually.

Usage:
    msys2_shell.cmd [options] [login shell parameters]

Options:
    -mingw32 | -mingw64 | -ucrt64 | -clang64 | -msys[2]   Set shell type
    -defterm | -mintty | -conemu                            Set terminal type
    -here                            Use current directory as working
                                     directory
    -where DIRECTORY                 Use specified DIRECTORY as working
                                     directory
    -[use-]full-path                 Use full current PATH variable
                                     instead of trimming to minimal
    -no-start                        Do not use "start" command and
                                     return login shell resulting
                                     errorcode as this batch file
                                     resulting errorcode
    -shell SHELL                     Set login shell
    -help | --help | -? | /?         Display this help and exit

Any parameter that cannot be treated as valid option and all
following parameters are passed as login shell command parameters.

I think the manual itself is pretty self-explanatory. The important flags here are -no-start and -defterm. But here, I wants to add more flags. The one that I added is -here that recommended by the guide, and -shell, because I want to choose another shell. You can customize it as you want.

C:\\msys64\\msys2_shell.cmd -defterm -here -no-start -mingw64 -shell zsh

So, the JSON profile would be

{
    "guid": "{05351409-756a-416f-81c1-a97a1a2cdfe2}",
    "name": "MINGW64",
    "commandline": "C:\\msys64\\msys2_shell.cmd -defterm -here -no-start -mingw64 -shell zsh",
    "hidden": false
},

For the GUID field, you can generate your own with [guid]::NewGuid().Guid in PowerShell, or use an online service like this one. It just identifier that this profile is different with others that might have same name.

For the latest version of Windows Terminal, you don't need edit the JSON file manually, and it also generate the GUID for you. Simply click "Add a new profile" and specify the "Command line" field.

terminal

nouvist
  • 141
  • 5