Skip to content

Instantly share code, notes, and snippets.

@commonquail
Last active September 7, 2023 13:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save commonquail/7df0a1f0700706a8e43e to your computer and use it in GitHub Desktop.
Save commonquail/7df0a1f0700706a8e43e to your computer and use it in GitHub Desktop.
Open MSYS2 Bash in target directory

You can add Windows shell extensions for launching MSYS2 Bash sessions in a target directory.

For right-clicking the background of an open folder, use the command

C:\msys64\usr\bin\mintty --icon /msys2.ico /usr/bin/env CHERE_INVOKING=1 /usr/bin/bash --login

For right-clicking a folder, use the command

C:\msys64\usr\bin\mintty --icon /msys2.ico /bin/sh -lc 'cd "%V"; /usr/bin/env CHERE_INVOKING=1 bash --login'

These will launch Bash as if you had manually cd'ed to the specified directory. Importantly, Bash completion will work.

They key is launching Bash with CHERE_INVOKING=1 set in the target directory. That makes the second command more involved, since it needs to descend into the selected directory (%V) first.

mintty will correctly interpret the UNIX style file paths, and in Windows fashion the executable extensions are optional.

The --icon argument and its value is optional.

The attached .reg file creates the extensions for path to mintty above. Adjust the path accordingly before executing.


An older solution I knew of,

C:\msys64\usr\bin\mintty.exe /bin/sh -lc 'cd "$(cygpath "%V")"; exec bash'

whose origin I have forgotten, created a functional shell but did not initialise Bash completion.

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\Background\shell\open_msys2]
@="Open MSYS2 here"
[HKEY_CLASSES_ROOT\Directory\Background\shell\open_msys2\command]
@="C:\\msys64\\usr\\bin\\mintty --icon /msys2.ico /usr/bin/env CHERE_INVOKING=1 /usr/bin/bash --login"
[HKEY_CLASSES_ROOT\Folder\shell\open_msys2]
@="Open MSYS2 here"
[HKEY_CLASSES_ROOT\Folder\shell\open_msys2\command]
@="C:\\msys64\\usr\\bin\\mintty --icon /msys2.ico /bin/sh -lc 'cd \"%V\"; /usr/bin/env CHERE_INVOKING=1 bash --login'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment