ConEmu: How to call msys2 as tab?

I also had problems getting MSYS2, MinGW32 and MinGW64 shells (v2016-12-07) to work in ConEmu v161206.

The only (half-portable) working solution I've found is the following.

  1. Create a new task with the name {Shells::mingw-64} and the following commands in ConEmu for the MinGW64 shell:

    set MSYSTEM=MINGW64 & set MSYSCON=conemu64.exe & "%ConEmuDir%\..\..\..\msys64\usr\bin\bash.exe" --login -i -new_console:C:"%ConEmuDir%\..\..\..\msys64\mingw64.exe"
    
  2. Create a new task with the name {Shells::mingw-32} and the following commands in ConEmu for the MinGW32 shell:

    set MSYSTEM=MINGW32 & set MSYSCON=conemu64.exe & "%ConEmuDir%\..\..\..\msys64\usr\bin\bash.exe" --login -i -new_console:C:"%ConEmuDir%\..\..\..\msys64\mingw32.exe"
    
  3. Create a new task with the name {Shells::msys2-64} and the following commands in ConEmu for the MSYS2 shell:

    set MSYSTEM=MSYS & set MSYSCON=conemu64.exe & "%ConEmuDir%\..\..\..\msys64\usr\bin\bash.exe" --login -i -new_console:C:"%ConEmuDir%\..\..\..\msys64\msys2.exe"
    

Attention: Modify the relative paths above to match the path to MSYS2 on your system.

The solution is half-portable, since it explicitly states the ConEmu executable (change it to ConEmu.exe for x86).

I am not sure if the value of the MSYSCON environment variable matters. I've only set it for safety, because the MSYS2 batch file msys2_shell.cmd also sets it.

An alternative is to use the msys2_shell.cmd batch script. This approach has the huge disadvantage that the msys2_shell.cmd spawns a new process via the Windows START command, therefore two tabs are added in ConEmu.

  1. Create a new task with the name {Shells::mingw-64} and the following commands in ConEmu for the MinGW64 shell:

    "%ConEmuDir%\..\..\..\msys64\msys2_shell.cmd" -mingw64 -conemu -new_console:C:"%ConEmuDir%\..\..\..\msys64\mingw64.exe"
    
  2. Create a new task with the name {Shells::mingw-32} and the following commands in ConEmu for the MinGW32 shell:

    "%ConEmuDir%\..\..\..\msys64\msys2_shell.cmd" -mingw32 -conemu -new_console:C:"%ConEmuDir%\..\..\..\msys64\mingw32.exe"
    
  3. Create a new task with the name {Shells::msys2-64} and the following commands in ConEmu for the MSYS2 shell:

    "%ConEmuDir%\..\..\..\msys64\msys2_shell.cmd" -msys -conemu -new_console:C:"%ConEmuDir%\..\..\..\msys64\msys2.exe"
    

The msys2_shell.cmd is intended to be used directly from a CMD.EXE, with ConEmu64.exe / ConEmu.exe in the PATH:

msys2_shell -mingw64 -conemu

That command opens a new MinGW64 tab in ConEmu.

tl;dr: I recommend the first approach. Here is a screenshot as a proof in ConEmu:

Working MSYS2 MinGW64 shell in ConEmu


What to do is actually pretty simple, once you peel all the layers away. This works well as an invocation (make sure to modify it to your specific MSYS2 install location):

set CHERE_INVOKING=1 & set MSYSTEM=MINGW64 & "c:\msys64\usr\bin\bash.exe" --login -i

I would personally recommend setting the shell to inherit your PATH:

set CHERE_INVOKING=1 & set MSYSTEM=MINGW64 & set MSYS2_PATH_TYPE=inherit & "c:\msys64\usr\bin\bash.exe" --login -i

My ConEmu Tasks settings page ends up looking like this: ConEmu Startup::Tasks Page

Breaking it All Down

MSYS2 likes to configure the shell according to environment variables rather than command line parameters. Passing options into a program that way might seem unnatural at first, but basically it just follows this simple series: set VAR=value & set VAR2=value & "c:\path\to\command" [args]

  • CHERE_INVOKING - if set to 1, bash starts in the current working directory, otherwise it starts in the user's home dir. This needs to be set to 1 to allow the shell to work as-expected from the Explorer Window context-menu.

  • MSYSTEM - which MSYS2 subsystem to use. There are three: MSYS, MINGW32, and MINGW64. The MSYS subsystem should only be used for pacman command operations and so I typically only invoke that via the old-skool start menu icon on the very rare occasion it's needed. MINGW32 is useless to all by a select few users and you know who you are already, probably.

  • MSYS2_PATH_TYPE - if unset MSYS uses a minimal PATH environment that contains only MSYS binaries and a couple select Windows system paths, and is a required mode for MSYSTEM=MSYS shell only. For MINGW shells it's recommended to use inherit -- I've been using MSYS2_PATH_TYPE=inherit without issues for years.

There's another variable MSYSCON mentioned by another answer and referenced in msys2_shell.cmd -- ignore it. It's only for use within the msys2_shell.cmd batch file and holds no bearing on the behavior of bash.exe or MSYS shell initialization.

MSYS2 installation Location

The MSYS2 path has been intentionally hard-coded as c:\msys64\ since that's the default install location for the MSYS2 Windows installer. It doesn't make sense in this case to use %ConEmuDir% since that's relative to %PROGRAMFILES%, and MSYS2 is more typically installed relative to the root directory of the hard drive. Note that it does make sense to use %ConEmuDir% when referencing the MSYS2 shell that comes with Git for Windows, since that is installed relative to %PROGRAMFILES%.

There is no easy way to know the install location of MSYS2. There's no environment variable set up by MSYS2 either, as much as I wish there was. Wouldn't it be swell if MSYS2 had an option to export something liek MSYSTEM_HOME into your machine's environment? But it doesn't, so it's entirely on you as a user to know where it's installed and point ConEmu there.

Regarding msys2_shell.cmd :

Don't use it. The batch file is incorrectly named. It should be called msys2_terminal.cmd or similar since what it's actually doing is a setting up an entire terminal for the shell. This is a common terminology mistake, especially on Windows operating systems where there's never really been a clear separation of terminal from shell. Here's how it works, explained from a Windows-user perspective:

  • Shell - this is the command processor and batch/script runner. It is essentially a command line application much like any other command line application. A shell doesn't even require any visible window or keyboard connections. It can operate entirely in the background on files, streams, or other sources of data input/output.

  • Terminal - this is a windowed application that provides a keyboard/mouse interface to the shell program. In windows it's often referred to as a Console Window. Keyboard commands are piped to the shell program, and output is displayed inside of the window with scrollbars, colored text, and other fun features.

In this scenario, I want my terminal to be ConEmu and my shell to be BASH (mingw64). Therefore I want ConEmu to start the shell and only the shell. I don't want ConEmu to start mintty. Those are both terminals and that would cause us to get secondary pop-up windows -- one for ConEmu and one for mintty. But that's exactly what trying to run msys2_shell.cmd will do. So don't use it.


Look inside msys2_shell.bat to get the answer. This batch runs mintty instead of bash shell. Useless. Change third line to full path to bash.exe -l -i.

how can I set this configuration as the default one when I start ConEmu

There is "Startup" settings page in ConEmu, isn't it?

Tags:

Conemu

Tasks