13

I have MinGW/MSYS on Windows, and can't figure how to start MSYS shell in folder I'm working in.

For example, in Windows console I'm working in folder c:\temp and if I call MSYS (msys.bat) it opens new console window in some fixed location, representing my home folder.

How to change this msys.bat file, so that MSYS shell opens in current working folder (or changes to it, after start)?

zetah
  • 704

7 Answers7

6

I'm not sure what version of msysgit you are using, but for me calling msys.bat does not change the current directory. If you see the directory being changed, check the etc/profile file in the msysgit directory for cd commands. As this file gets executed when a login shell is started it might be the cause for you to always land in your home directory.

5

As others have pointed out, msys.bat will issue a cd "$HOME" from etc/profile. Setting the HOME environment variable to . gives me a mingw shell with the correct working directory.

set HOME=.
C:\MinGW\msys\1.0\msys.bat
3

This is not exactly what you were asking for but has the same effect.

What you could do is adding a MSYS2 here context menu entry. To do so add the following to your registry:

Windows Registry Editor Version 5.00

; Conext menu entry right clicking on directories [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\msys_shell] @="MSYS2 Here" "Icon"="C:\msys64\msys2.exe"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\msys_shell\command] @=""C:\msys64\msys2_shell.cmd" "-where" "%V""

; Conext menu entry right clicking when in the library folder [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\LibraryFolder\background\shell\msys_shell] @="MSYS2 Here" "Icon"="C:\msys64\msys2.exe"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\LibraryFolder\background\shell\msys_shell\command] @=""C:\msys64\msys2_shell.cmd" "-where" "%V.""

; Conext menu entry right clicking not on directories but the background [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\background\shell\msys_shell] @="MSYS2 Here" "Icon"="C:\msys64\msys2.exe"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\background\shell\msys_shell\command] @=""C:\msys64\msys2_shell.cmd" "-where" "%V""

; Conext menu entry right clicking on directories [HKEY_CLASSES_ROOT\Directory\shell\msys_shell] @="MSYS2 Here" "Icon"="C:\msys64\msys2.exe"

[HKEY_CLASSES_ROOT\Directory\shell\msys_shell\command] @=""C:\msys64\msys2_shell.cmd" "-where" "%V""

  • Make sure you adapt the MSYS2 installation paths accordingly
  • You can find more information regarding the use of %V here
  • Adding "-use-full-path" to lines where msys2_shell.cmd is called allows you to use everything in PATH in MSYS2
2

You can create a bash file and pass arguments to the msys2_shell.cmd to start anywhere you want it to be.

msys2_shell.cmd -where "home/name/esp"

Brandan
  • 21
0

If you want to run it in the folder you are working in, and you are working in that folder regularly, you can add a line to the end of .bash_profile in your home directory.

cd /c/temp

This command will be ran each time you log into the terminal. You can get elaborate and source a file if you have multiple commands you want to run, e.g.

. ~/etc/start_script . ~/etc/start_script2

This is especially useful if you already have your .bashrc configured and or don't really want to use an alternate HOME path.

0

In case the modifying of etc/profile (commenting out of cd "$HOME" like in first answer) is undesirable for some reason, this could do the job:

# in mingw:
%SystemDrive%\mingw\msys\1.0\bin\sh.exe --login -i -c "cd '%TEMP%'; $SHELL"
# msys in root:
%SystemDrive%\msys\bin\sh.exe --login -i -c "cd '%TEMP%'; $SHELL"

This example would start msys shell in /tmp directory.

sebres
  • 386
0

After reading this inspiring answer and asking the launcher for help,

$> c:\msys64\msys2_shell.cmd -help
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 solved this issue like this for an up-to-date MSYS2 installation on Windows10/11 (in 2024):

Create a file msys.cmd in my tools folder (this folder is in %PATH%) with following contents.

set MSYSTEM=UCRT64
c:\msys64\msys2_shell.cmd -here %*

This will default to the nowadays encouraged UCRT64 but will as well pass arguments like -mingw64 (which are higher priority) to the launcher, so not much to remember.

Wolf
  • 416