msys2 / MSYS2-packages

Package scripts for MSYS2.
https://packages.msys2.org
BSD 3-Clause "New" or "Revised" License
1.3k stars 489 forks source link

Using MSYS2 in Windows Terminal #1684

Open angelog0 opened 5 years ago

angelog0 commented 5 years ago

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

Thanks...

chuxubank commented 4 years ago

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 commented 4 years ago

You can get the icons here:

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

rkitover commented 4 years ago

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 commented 4 years ago

@rkitover Thank you, it works! sshd

DHowett commented 4 years ago

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 commented 2 years ago

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 commented 1 year ago

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 commented 1 year ago

That worked @CGQAQ !! Thanks a lot!

anoban commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 https://github.com/microsoft/terminal/discussions/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 commented 1 year ago

@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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

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 commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

I raised an issue with the Windows terminal team here: https://github.com/microsoft/terminal/issues/17856#issuecomment-2334943355

TBBle commented 2 months ago

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 commented 2 months ago

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