Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using MSYS2 in Windows Terminal #1684

Open
angelog0 opened this issue Jun 27, 2019 · 73 comments
Open

Using MSYS2 in Windows Terminal #1684

angelog0 opened this issue Jun 27, 2019 · 73 comments

Comments

@angelog0
Copy link

Has someone tried to use MSYS2 and friends (MinGW32/64) in the new Windows terminal? What would it be the MSYS2-profile?

Thanks...

@lazka
Copy link
Member

lazka commented Jul 1, 2019

Looks like you asked there too: microsoft/terminal#1669

Did you get it working?

@angelog0
Copy link
Author

angelog0 commented Jul 2, 2019

This seems to work:

{
            "acrylicOpacity" : 0.5,
            "closeOnExit" : true,
            "colorScheme" : "base16-default-mod-lighten",
            "commandline" : "C:/msys64/home/user/wt-msys2.cmd -msys2",
            "cursorColor" : "#FD9D4F",
            "cursorShape" : "filledBox",
            "fontFace" : "DejaVu Sans Mono",
            "fontSize" : 11,
            "guid" : "{00000000-0000-0000-0000-000000000000}",
            "historySize" : 9001,
            "icon": "ms-appdata:///roaming/msys2.ico",
            "name" : "MSYS2",
            "padding" : "0, 0, 0, 0",
            "snapOnInput" : true,
            "startingDirectory" : "C:/msys64/home/user",
            "useAcrylic" : false
        },

where

$ cat .ms-terminal/wt-msys2.cmd
@echo off
setlocal

rem To activate windows native symlinks uncomment next line
set MSYS=winsymlinks:nativestrict

rem Shell types
if "x%~1" == "x-msys2" set MSYSTEM=MSYS
if "x%~1" == "x-mingw32" set MSYSTEM=MINGW32
if "x%~1" == "x-mingw64" set MSYSTEM=MINGW64


rem Shell types
rem set MSYSTEM=MSYS

C:\msys64\usr\bin\bash.exe --login -i

exit /b 0


May be we need to adjust the guid with uuidgen

@patstew
Copy link

patstew commented Jul 5, 2019

Instead of a separate script you can just use:
"commandline" : "cmd.exe /c \"set MSYSTEM=MINGW64&& C:\\msys64\\usr\\bin\\bash.exe --login\"",
Note the lack of a space before &&.
One problem is that bash does not seem to catch the SIGBREAK signal (I think that's what windows sends in place of SIGHUP?), so it doesn't save history on exit.

@angelog0
Copy link
Author

angelog0 commented Jul 5, 2019

@patstew, thanks for the suggestions, but it is not a separate script but the same .cmd with different options.

Thanks.

@patstew
Copy link

patstew commented Jul 5, 2019

I was just pointing out you don't need any .cmd at all.

@sifivekevin
Copy link

Just noting here that one can define CHERE_INVOKING=1 and the profile will then honor the "startingDirectory" parameter. Like this:

"commandline" : "cmd.exe /c \"set CHERE_INVOKING=1&& set MSYSTEM=MSYS2&& C:\\msys64\\usr\\bin\\bash.exe --login\"",
"startingDirectory": "%USERPROFILE%\\git",

@angelog0
Copy link
Author

angelog0 commented Oct 1, 2019

OK, I tried this that seems to work:

"commandline": "cmd.exe /c \"set MSYSTEM=MSYS&& C:/msys64/usr/bin/bash.exe --login\""

and noticed that has:

$ echo $TERM                                                                                                            
cygwin

In MInTTY, instead it is xterm, so how can we set the same in Windows Terminal? May be other variable need to be imported..

See also this #2953.

@angelog0
Copy link
Author

angelog0 commented Oct 1, 2019

Let me know if we have to reclose this

@angelog0 angelog0 reopened this Oct 1, 2019
@rkitover
Copy link
Contributor

I just tried this too, and terminal things do not work, even if I manually set TERM=xterm-256color.

E.g. tmux says:

open terminal failed: not a terminal

colors in vim don't work, etc..

@pineman
Copy link

pineman commented Oct 19, 2019

Colors are all broken for me too.

@angelog0
Copy link
Author

angelog0 commented Nov 5, 2019

@restia666ashdoll, also this works

"commandline" : "C:\\msys64\\usr\\bin\\bash.exe\" -i -l",

but you cannot set environment variables. For example, what do you use to start a MSYS2/MinGW64 shell?

This works for me:

"commandline": "cmd.exe /c \"set MSYSTEM=MINGW64&& set MSYS=winsymlinks:nativestrict&& C:/msys64/usr/bin/bash.exe --login\"",
"hidden": false,
"startingDirectory": "C:/msys64/home/user",

and similar for MSYS2, MSYS2/MinGW32 shells.

@rkitover
Copy link
Contributor

rkitover commented Nov 6, 2019

@restia666ashdoll try forward slashes.

While these methods may work for people for git, we still need an actual fix for the terminal to work correctly as a terminal.

@Witchilich
Copy link

Witchilich commented Nov 7, 2019

If you install them with scoop install msys2, you can just use this

        {
            "acrylicOpacity" : 0.75,
            "closeOnExit" : true,
            "colorScheme" : "flat-ui-v1",
            "commandline" : "cmd /c msys2",
            "cursorColor" : "#FFFFFF",
            "cursorShape" : "bar",
            "fontFace" : "Consolas",
            "fontSize" : 10,
            "historySize" : 9001,
            "icon" : "%SCOOP%\\apps\\msys2\\current\\msys2.ico",
            "name" : "MSYS2",
            "padding" : "0, 0, 0, 0",
            "snapOnInput" : true,
            "startingDirectory" : ".",
            "useAcrylic" : true
        },
        {
            "acrylicOpacity" : 0.75,
            "closeOnExit" : true,
            "colorScheme" : "flat-ui-v1",
            "commandline" : "cmd /c mingw64",
            "cursorColor" : "#FFFFFF",
            "cursorShape" : "bar",
            "fontFace" : "Consolas",
            "fontSize" : 10,
            "historySize" : 9001,
            "icon" : "%SCOOP%\\apps\\msys2\\current\\mingw64.ico",
            "name" : "MinGW64",
            "padding" : "0, 0, 0, 0",
            "snapOnInput" : true,
            "startingDirectory" : ".",
            "useAcrylic" : true
        },

@lamuertepeluda
Copy link

My config (with windows PATH included --> vscode and other stuff can work), notice MSYS2_PATH_TYPE

    {
      "acrylicOpacity": 0.75,
      "closeOnExit": true,
      "colorScheme": "Campbell",
      "commandline": "cmd.exe /c \"set MSYSTEM=MINGW64&& set MSYS=winsymlinks:nativestrict&& set MSYS2_PATH_TYPE=inherit&& C:/msys64/usr/bin/bash.exe --login\"",
      "cursorColor": "#FFFFFF",
      "cursorShape": "bar",
      "fontFace": "Consolas",
      "fontSize": 10,
      "guid": "{1597a4d0-b666-4b1f-a8d3-79af62638679}",
      "historySize": 9001,
      "icon": "C:\\msys64\\msys2.ico",
      "name": "MSYS2",
      "padding": "0, 0, 0, 0",
      "snapOnInput": true,
      "startingDirectory": "%USERPROFILE%",
      "useAcrylic": true
    }

MSYS2 configuration thanks to this great article

@machsix
Copy link

machsix commented Nov 21, 2019

Sadly, this doesn't give me a terminal with 256 colors.
My configuration

        {
            "guid": "{75529e5d-bde6-4848-85f4-d73c3838a5f5}",
            "name": "MinGW 64",
            "commandline": "cmd.exe /c set MSYSTEM=MINGW64 && c:/msys64/usr/bin/bash --login -i",
            "cursorColor": "#FFFFFF",
            "cursorShape": "bar",
            "hidden": false,
            "icon": "C:\\msys64\\msys2.ico",
            "fontFace": "Cascadia Code",
            "startingDirectory": "%USERPROFILE%"
        },

I used the test script here
https://askubuntu.com/questions/821157/print-a-256-color-test-pattern-in-the-terminal

Capture

I think it is related to this one
microsoft/terminal#2837
#1771

@Witchilich
Copy link

Works here
image

@TBBle
Copy link
Contributor

TBBle commented Jan 4, 2020

Those're two different tests. The pipe-to-bash test from @machsix 's link is generating blocks use "Set Background Colour to x" (SGR 48) for x in the range 1-255 (i.e. 256-colour palette).

The one-liner used by @restia666ashdoll is iterating through the 9 terminal styles, for each of the eight foreground colours and eight background colours (i.e. 8/16 colour palette, since usually 'bold' in that palette means a second set of 8 colours, traditionally brighter versions of the first 8).

@restia666ashdoll's script for easy cut-and-paste.

for x in {0..8}; do for i in {30..37}; do for a in {40..47}; do echo -ne "\e[$x;$i;$a""m\\\e[$x;$i;$a""m\e[0;37;40m "; done; echo; done; done; echo ""

256-colour support is definitely an issue about MSYS2 or bash or cygwin interacting with ConPTY, since I can do:

curl -s https://gist.githubusercontent.com/HaleTom/89ffe32783f89f403bba96bd7bcd1263/raw/ | bash > 256col.ans
cat 256col.ans

and see the same result as @machsix, but outputting the same file (256col.ans) under PowerShell Core in MS Terminal or directly, (or MSYS in MinTTY) shows the intended result.

The same problem shows up if I run PowerShell Core directly (rather than inside MS Terminal) and then start MSYS in-place with

C:\msys64\msys2_shell.cmd -no-start -defterm

Given that the foreground text on 30, 31, 32, and 33 have come out differently, but all used the same command (\e[38;5;15m), I guess something is incorrectly mangling the ANSI sequences, as they're acting like they saw \e[30m, \e[31m,\e[32m,\e[33m. However, the entire rest of the test is acting like it saw the same foreground colour, and for some reason is using the supplied foreground colour as the background in every case.

@TBBle
Copy link
Contributor

TBBle commented Jan 4, 2020

Another good test to show it's something about cygwin and Windows ConHost is https://github.com/Maximus5/ConEmu/blob/master/Release/ConEmu/Addons/AnsiColors256.ans. In the faulty case, you see a bunch of \, which are actually part of an escape sequence.

And looking again at microsoft/terminal#2837, that's almost certainly the issue. https://conemu.github.io/en/CygwinAnsi.html is a good description of the underlying issue.

@TBBle
Copy link
Contributor

TBBle commented Jan 4, 2020

To bring it back to MSYS, fhandler_console::char_command in cygwin.dll is the code that tries to translate CSI sequences, e.g., SGR as we're discussing here, into ConHost commands. It does this through a table mapping the 256-colour palette down into the the 16-colour palette used by the Windows Console CHAR_INFO structure (see table256).

It's not obvious what goes wrong, since the first row of that table is the same as the values for 30-37, then again with bold added, so something else must be mangling the sequences.

It's also possible the parsing itself (fhandler_console::write, the code that calls char_command) is faulty. This seems likely because the output of the \ in the AnsiColors256.ans test points to a failure in gotrsquare, eatpalette, endpalette, which is part of the same parser.

@TBBle
Copy link
Contributor

TBBle commented Jan 4, 2020

Trying to track this down, it seems that there's a value wincap.has_con_24bit_colors() which controls whether TERM is set to xterm (true) or cygwin (false), see msys2/msys2-runtime@bd62786.

I'm definitely getting TERM as cygwin on my Windows 10 1909 desktop machine when run under Terminal or from PowerShell Core, but not from mintty.

Ah! The msys-runtime package is still on Cygwin 3.0.7, and the change above for 24-bit colour support in Windows landed in Cygwin 3.1, along with Pseudo Console support which (I think) removes the need for the winpty tool.

So for 24-bit colour support in Windows Terminal, we need to upgrade msys-runtime, which I think should be a separate bug report from this one.

As far as the original question, I'm using the simple solution from the other ticket

"commandline" : "C:/msys64/msys2_shell.cmd -defterm -no-start -msys2"
"commandline" : "C:/msys64/msys2_shell.cmd -defterm -no-start -mingw64"
"commandline" : "C:/msys64/msys2_shell.cmd -defterm -no-start -mingw32"

@stania
Copy link

stania commented Mar 23, 2020

Any changes there?

@rkitover
Copy link
Contributor

As @TBBle said, we need someone to update the msys2 runtime to the latest cygwin version.

Any volunteers?

@Biswa96
Copy link
Member

Biswa96 commented Mar 23, 2020

The new conpty interface in cygwin is not complete yet. New issues pop up everyday as well as new patches.

@rkitover
Copy link
Contributor

All the terminal stuff is still kind of buggy. I have been using mostly powershell 7, with the new terminal and over ssh, and I see all kinds of weird issues with things like the native vim in various configurations. Sometimes it works perfectly, other times it misaligns everything and puts stars around words, other times it takes up only half the terminal, etc. etc.. I just end up using whichever version of vim or neovim works in that configuration.

I'd love to be able to use the new terminal with msys2, and do things like start an msys2 shell from a native sshd powershell session etc..

@rkitover
Copy link
Contributor

Perhaps the cygwin terminal stuff may not be perfect, but better than nothing for a lot of people.

@Alexpux how do you feel about this? Would it helped if we made a PR (and tested it thoroughly of course) to merge the latest cygwin runtime?

@yihuajack
Copy link

What's the difference between bash.exe --login and bash.exe --login -i?

@Biswa96
Copy link
Member

Biswa96 commented Apr 1, 2020

From man bash:

-i        If the -i option is present, the shell is interactive.
-l        Make bash act as if it had been invoked as a login shell (see INVOCATION below).

@chuxubank
Copy link

Works here
image

How to get mingw64 mini icon?

@mapitman
Copy link
Contributor

mapitman commented Jun 1, 2020

How to get mingw64 mini icon?

See this line in my profiles.json file
https://gist.github.com/mapitman/9c2c0a0f8f6e65475d210817e2be6a82#file-profiles-json-L21

@chuxubank
Copy link

How to get mingw64 mini icon?

See this line in my profiles.json file
https://gist.github.com/mapitman/9c2c0a0f8f6e65475d210817e2be6a82#file-profiles-json-L21

Thanks, but it's msys2.icon, what I want is mingw64 icon, which has a blue background.

@rkitover
Copy link
Contributor

rkitover commented Jun 2, 2020

You can get the icons here:

https://github.com/msys2/msys2-launcher

@rkitover
Copy link
Contributor

rkitover commented Jun 2, 2020

I've updated my sshd setup script to support Cygwin, due to a request, as well and to be more automated, it sets up keys and host aliases automatically now. MSYS2 and Cygwin sshds can be used at the same time, along with the Windows native sshd.

This gives you full terminal support, including for tmux, and remote support (obviously) the only limitation is that running GUI apps is not possible.

https://github.com/rkitover/windows-alt-sshd-msys2

@rescenic
Copy link

rescenic commented Jun 3, 2020

@rkitover Thank you, it works!
sshd

@DHowett
Copy link

DHowett commented Jun 15, 2020

Just as a note: I think that no amount of "enabling the runtime to use the pseudoconsole API" is going to help tmux determine that its parent device is a tty. Pseudoconsole support changes what happens when a pty is allocated, which is not the thing that tmux is failing to do.

If we consider tmux as a set of steps,

  1. Start up
  2. Do something to the controlling terminal
    2.a. Check whether the controlling terminal is a tty
  3. Allocate a PTY

It's failing on 2.a.; enable_pcon would only impact 3.

@eromoe
Copy link

eromoe commented Oct 26, 2022

Does anyone know how to set a right click context menu to do:

  1. open a terminal default with msys2
  2. go to current folder

I have been using this reg file to do such thing, but I have no idea how to change to terminal ..

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\mingw]
@="Open MinGW32 here"
"Icon"="D:\\msys64\\msys2.ico"
[HKEY_CLASSES_ROOT\Directory\Background\shell\mingw\command]
@="D:\\msys64\\usr\\bin\\mintty -i /msys2.ico --dir \"%V/\" /bin/env MSYSTEM=MINGW32 CHERE_INVOKING=1 /usr/bin/bash -l"
[HKEY_CLASSES_ROOT\Directory\shell\mingw]
@="Open MinGW32 here"
"Icon"="D:\\msys64\\msys2.ico"
[HKEY_CLASSES_ROOT\Directory\shell\mingw\command]
@="D:\\msys64\\usr\\bin\\mintty -i /msys2.ico --dir \"%1/\" /bin/env MSYSTEM=MINGW32 CHERE_INVOKING=1 /usr/bin/bash -l"

@CGQAQ
Copy link

CGQAQ commented Apr 10, 2023

This works like charm with system path recognized

{
    "commandline": "C:\\tools\\msys64\\msys2_shell.cmd -defterm -here -no-start -mingw64 -shell bash -use-full-path",
    "experimental.retroTerminalEffect": false,
    "guid": "{a69dbdaf-3219-4965-a5a7-2210313f9581}",
    "hidden": false,
    "icon": "C:\\tools\\msys64\\msys2.ico",
    "name": "MSYS2",
    "opacity": 85,
    "startingDirectory": null,
    "tabTitle": "MSYS2"
}

@endo64
Copy link

endo64 commented Apr 18, 2023

That worked @CGQAQ !! Thanks a lot!

@anoban
Copy link

anoban commented Apr 21, 2023

Hi there!

Here are my configurations:

{
                "commandline": "C:\\msys64\\msys2_shell.cmd -defterm -here -no-start -ucrt64 -shell bash -use-full-path",
                "cursorShape": "underscore",
                "elevate": false,
                "experimental.retroTerminalEffect": false,
                "guid": "{fe36c14f-2b13-4109-8e9a-5414c547bb40}",
                "hidden": false,
                "icon": "C:\\msys64\\msys2.ico",
                "name": "MSYS2 UCRT64",
                "startingDirectory": "D:\\Dev",
                "tabTitle": "MSYS2 UCRT64"
}

But the bash's character ($) has changed to a (#) now. I would like the dollar sign back. Any ideas?

Here's the UCRT shell (native).
Screenshot (1)

Here's the Windows Terminal version of it.
Screenshot (2)

@Biswa96
Copy link
Member

Biswa96 commented Apr 21, 2023

But the bash's character ($) has changed to a (#) now. I would like the dollar sign back. Any ideas?

Probably the Windows Terminal is running as administrator.

@Biswa96
Copy link
Member

Biswa96 commented Apr 21, 2023

msys2 is working in Windows Terminal, right? Can this issue be closed now? There is a documentation about how to use msys2 in Windows Terminal here https://www.msys2.org/docs/terminals/#windows-terminal

@ipaqmaster
Copy link

ipaqmaster commented Jun 15, 2023

I would be a lot better if the official documentation didn't use a .cmd batch script and instead just invoked what it needs directly for each environment. Because of this bat file approach, closing MSYS2 terminals gives a terminate batch job (y/n) question instead of just closing the shell when you ctrl+d.

E: As commented here microsoft/terminal#14663 C:\Windows\System32\cmd.exe /C "set MSYSTEM=MINGW64&& start /B C:\msys64\usr\bin\bash.exe -l" seems to work well for replacing msys2_shell.cmd.

@angelog0
Copy link
Author

angelog0 commented Jun 19, 2023

@ipaqmaster, with WT 1.18 you need just this:

{
                // Removing 'enable_pcon' from MSYS because it is
                // enable by default since Sep. 24, 2022.
                // "environment" is in WT since 1.18
                "environment":
                {
                    "MSYS": "winsymlinks:nativestrict",
                    "CHERE_INVOKING": "1",
                    "MSYSTEM": "MSYS"
                },
                "colorScheme": "MSYS2-Theme",
                "commandline": "C:/msys64/usr/bin/bash --login",
                "guid": "{9c173dff-f524-43df-b40e-29953998c744}",
                "icon": "C:/msys64/msys2.ico",
                "name": "MSYS2 MSYS Shell",
                "startingDirectory": "C:/msys64/home/%USERNAME%"
            },

or this:

{
                // "environment" is in WT since 1.18
                "environment":
                {
                    "MSYS": "winsymlinks:nativestrict",
                    "CHERE_INVOKING": "1",
                    "MSYSTEM": "MINGW64"
                },
                "colorScheme": "MSYS2-Theme",
                "commandline": "C:/msys64/usr/bin/bash --login",
                "guid": "{b01223ec-657d-425c-96e1-20b6f3e4681c}",
                "icon": "C:/msys64/mingw64.ico",
                "name": "MSYS2 MINGW64 Shell",
                "startingDirectory": "C:/msys64/home/%USERNAME%"
            },

@ipaqmaster
Copy link

I was looking for somewhere to set variables right in WT. Felt strange that there was no facility to do so. Glad it's a feature now.

@comiluv
Copy link

comiluv commented Jul 18, 2023

Even with "environment" key set and "commandline" key with value of "bash --login", MSYS2 still shows "Press Ctrl-D to close or Enter to restart" on Ctrl-D or logout. Windows Terminal Preview 1.18.1462.0

            {
                "commandline": "C:/tools/msys64/usr/bin/bash --login",
                "environment": 
                {
                    "CHERE_INVOKING": "1",
                    "MSYS": "winsymlinks:nativestrict",
                    "MSYSTEM": "UCRT64"
                },
                "font": 
                {
                    "face": "Lucida Console"
                },
                "guid": "{71160544-14d8-4194-af25-d05feeac7233}",
                "icon": "C:\\tools\\msys64\\ucrt64.ico",
                "name": "UCRT64 / MSYS2",
                "startingDirectory": "C:/tools/msys64/home/%USERNAME%"
            },

@angelog0
Copy link
Author

Even with "environment" key set and "commandline" key with value of "bash --login", MSYS2 still shows "Press Ctrl-D to close or Enter to restart" on Ctrl-D or logout. Windows Terminal Preview 1.18.1462.0

NOT here

"profiles":
    {

        "defaults":
        {
            "closeOnExit": "graceful",
            "cursorShape": "filledBox",
            "font":
            {
                "face": "JetBrains Mono",
                "size": 11
            },
            "hidden": false,
            "historySize": 9001,
            "padding": "8, 8, 8, 8",
            "snapOnInput": true
        },
        "list":
        [
            {
                "environment":
                {
                    "MSYS": "winsymlinks:nativestrict",
                    "CHERE_INVOKING": "1",
                    "MSYSTEM": "MSYS"
                },
                "colorScheme": "MSYS2-Theme",
                "commandline": "C:/msys64/usr/bin/bash --login",
                "guid": "{9c173dff-f524-43df-b40e-29953998c744}",
                "icon": "C:/msys64/msys2.ico",
                "name": "MSYS2 MSYS Shell",
                "startingDirectory": "C:/msys64/home/%USERNAME%"
            },

Maybe you have something different elsewhere..

@sskras
Copy link

sskras commented Jul 18, 2023

@comiluv:

Even with "environment" key set and "commandline" key with value of "bash --login", MSYS2 still shows "Press Ctrl-D to close or Enter to restart" on Ctrl-D or logout. Windows Terminal Preview 1.18.1462.0

Windows Terminal version: 1.17.11461.0 probably does this too, but closes the tab instantly after that.

Looks like version 1.18.1462.0 changed the default (Automatic) behaviour.
I think this could be restored by adding a line with closeOnExit set to always to your profile:

             {
+                "closeOnExit": "always",
                 "commandline": "C:/tools/msys64/usr/bin/bash --login",
                 "environment": 
                 {
  ...

... or maybe use graceful:

+                "closeOnExit": "graceful",

The GUI way:

image

I see @angelog0 confirms my suggestion (Mid-air collision detected:)

@tranngocteam79
Copy link

This is my MSYS2 configuration if anyone is interested.

Windows Terminal

"profiles": 
    {
        "list": 
        [
             {
                "commandline": "C:/msys64/msys2_shell.cmd -defterm -here -no-start -mingw64",
                "guid": "{17da3cac-b318-431e-8a3e-7fcdefe6d114}",
                "icon": "C:/msys64/mingw64.ico",
                "name": "MINGW64 / MSYS2",
                "startingDirectory": "C:/msys64/home/%USERNAME%"
            },
            {
                "commandline": "C:/msys64/msys2_shell.cmd -defterm -here -no-start -msys",
                "guid": "{71160544-14d8-4194-af25-d05feeac7233}",
                "icon": "C:/msys64/msys2.ico",
                "name": "MSYS / MSYS2",
                "startingDirectory": "C:/msys64/home/%USERNAME%"
            },
        ]
    }

WindowsTerminal_00T9FRXT6v

VS Code

settings.json

{
    "terminal.integrated.profiles.windows": {
        "msys64": {
            // bash.exe (bash shell) or zsh.exe (zsh shell)
            "path": "C:\\msys64\\usr\\bin\\bash.exe", 
            "args": ["--login", "-i"],
            "env": {
                "MSYSTEM": "MINGW64",
                "CHERE_INVOKING": "1",
                "MSYS2_PATH_TYPE": "inherit"
            },
        }
    },
    "terminal.integrated.defaultProfile.windows": "msys64"
}

image

Jetbrains IDE

Go to Setting => Tools => Terminal
Type CHERE_INVOKING=1;MSYS2_PATH_TYPE=inherit;MSYSTEM=MINGW64 in Enviroment Variables field
Type C:\msys64\usr\bin\zsh.exe --login -i (zsh shell) or C:\msys64\usr\bin\bash.exe --login -i (bash shell) in Shell path field
image
idea64_WFEtFUfai9

Right click open MSYS2 MINGW64 (Context menu)

Use BluePointLilac ContextMenuManager add new item in Background category
https://github.com/BluePointLilac/ContextMenuManager
Use command

C:\Windows\System32\cmd.exe /C "set CHERE_INVOKING=1&& set MSYS2_PATH_TYPE=inherit&& set MSYSTEM=MINGW64&& start /B C:\msys64\usr\bin\zsh.exe --login -i"

ShareX_WZKEY6UvDj
KzRkMbM6ye

@alshdavid
Copy link

alshdavid commented Sep 3, 2024

Does anyone know how to persist the zsh command history?

{
  "guid": "{55355a23-abac-48c2-b092-70c8cfc95684}",
  "hidden": false,
  "name": "zsh",
  "icon": "C:\\Users\\alshdavid\\.local\\msys64\\msys2.ico",
  "closeOnExit": "always",
  "commandline": "C:\\Users\\alshdavid\\.local\\msys64\\msys2_shell.cmd -defterm -here -no-start -mingw64 -shell zsh",
  "startingDirectory": "C:/Users/%USERNAME%",
  "environment": {
    "MSYS": "winsymlinks:nativestrict",
    "CHERE_INVOKING": "1",
    "MSYSTEM": "MINGW64"
  }
},

For bash, adding PROMPT_COMMAND='history -a' works but this doesn't work for zsh.

If I quit the terminal with exit then it saves the command history for the session, but not when I close the tab

@TBBle
Copy link
Contributor

TBBle commented Sep 6, 2024

Does anyone know how to persist the zsh command history?

I guess that you want to enable the INC_APPEND_HISTORY option or similar, so that you get the history saved after each command. This is the equivalent of PROMPT_COMMAND='history -a' for bash, I believe.

@alshdavid
Copy link

I raised an issue with the Windows terminal team here: microsoft/terminal#17856 (comment)

@TBBle
Copy link
Contributor

TBBle commented Sep 9, 2024

And they punted it back here, stating that the difference between what zsh expects when its window is closed (probably a SIGHUP) and what it receives right now lives in the cygwin runtime (i.e. msys2-runtime for msys2 packages). I had a quick look and per msys2-runtime source it looks like it should be generating a SIGHUP as expected, but even 22 years ago, SIGHUP didn't seem to be reliable in cygwin.

I am also just blindly assuming that zsh would save its history on SIGHUP. I had a quick look at the zsh source, but didn't work through the signal handling code to confirm that. But I am fairly sure bash saves history on SIGHUP, and since we need a "write-history-after-every-command" workaround for it as well, I am assuming the problem is not on the zsh side.

@rkitover
Copy link
Contributor

rkitover commented Sep 9, 2024

@TBBle You may want to ask the Cygwin mailing list, they may know more about this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests