Skip to content

Instantly share code, notes, and snippets.

@juntalis
Created October 17, 2011 00:07
Show Gist options
  • Save juntalis/1291621 to your computer and use it in GitHub Desktop.
Save juntalis/1291621 to your computer and use it in GitHub Desktop.
A (Windows) batch script to open the MSYS shell in the user's current window (cmd.exe or whatever wrapper you're using) at the user's current folder. It requires getcp.exe be in your msys bin folder. (getcp.exe is included with msysgit) It also generates
@rem Do not use "echo off" to not affect any child calls.
@SETLOCAL
@SETLOCAL ENABLEEXTENSIONS
:: Figure out where msys's root folder. If you want, you could just add the folder in the line
:: below.
@set MSYSROOT=
@if "x%MSYSROOT%"=="x" @if exist "%~dp0msys.bat" @set MSYSROOT=%~dp0
@if "x%MSYSROOT%"=="x" @if exist "%~dp0.msys-root" @set /P MSYSROOT=<%~dp0.msys-root
@if "x%MSYSROOT%"=="x" (
@echo Could not locate your mysys root folder.
@set /P MSYSROOT=Location:
)
:: Read as MSYSROOT.trim()
@if not "x%MSYSROOT%"=="x" (
@for /f "tokens=* delims= " %%a in ("%MSYSROOT%") do @set MSYSROOT=%%a
@for /f "useback tokens=*" %%a in ('%MSYSROOT%') do @set MSYSROOT=%%~a
@if not "%MSYSROOT:~-1%"=="\" @set MSYSROOT=%MSYSROOT%\
)
:: Verify that root folder exists
@if not exist "%MSYSROOT%" (
@echo "%MSYSROOT%" is not a valid folder. Please check for .msys-root in %~dp0, or if you entered the path above, please rerun this script and select a valid folder.
@exit /B 1
) else (
@if not "%MSYSROOT%"=="%~dp0" @echo %MSYSROOT%>%~dp0.msys-root
)
:: Home Folder
:: If you'd prefer the home directory set to your C:\Users\Username folder, uncomment the two lines
:: below.
@rem @if not exist "%HOME%" @set HOME=%HOMEDRIVE%%HOMEPATH%
@rem @if not exist "%HOME%" @set HOME=%USERPROFILE%
@if not exist "%HOME%" @if not "%MSYSROOT%"=="" @set HOME=%MSYSROOT%home\%USERNAME%
@if not "x%WD%"=="x" @set WD=
@set PLINK_PROTOCOL=ssh
@if not exist "%WD%msys-1.0.dll" @set WD=%MSYSROOT%\bin\
@set MSYSCON=sh.exe
:: In case you want to add more shit, just add a goto location below :default
@if not "%1"=="" goto %1
@goto default
:default
:: Default action, open msys and go to the current folder.
@set OLDCD=%CD%
@if not "x%OLDCD%"=="x" @set CURRCD=%CD%
:: Get the current console ("OEM") codepage.
@for /f %%i in ('"%MSYSROOT%bin\getcp.exe" -oem') do @set cp_oem=%%i
:: Get the current GUI ("ANSI") codepage.
@for /f %%i in ('"%MSYSROOT%bin\getcp.exe" -ansi') do @set cp_ansi=%%i
:: Set the console codepage to match the GUI codepage.
@chcp %cp_ansi% > nul < nul
@if not "x%OLDCD%"=="x" (
@"%MSYSROOT%bin\bash.exe" -l -i -c "cd \"$CURRCD\"; exec /bin/bash -rcfile ~/.bash_profile"
) else (
@"%MSYSROOT%bin\bash.exe" -l
)
:: Store the error level returned by bash.
@set ErrorLevel=%ErrorLevel%
:: Restore the original console codepage.
@chcp %cp_oem% > nul < nul
:: If we had a current directory at the store of the script, go back to it.
@if not "x%OLDCD%"=="x" chdir /D "%OLDCD%"
@goto quit
:generate
:: Generate registry files for adding and removing "MSYS Here" to the context menu for folders.
@SETLOCAL DISABLEDELAYEDEXPANSION
@REM :: Escape any sepcial characters in our path.
@REM @set MSYSROOT=%3
@REM @set MSYSROOT=%MSYSROOT:^=^^%
@REM @echo %MSYSROOT%
@REM @set MSYSROOT=%MSYSROOT:&=^&%
@REM @echo %MSYSROOT%
@REM @set MSYSROOT=%MSYSROOT:!=^!%
@REM @echo %MSYSROOT%
@REM @set MSYSROOT=%MSYSROOT:|=^|%
@REM @echo %MSYSROOT%
:: Just realized the above isn't necessary.
@set workdir=
@if "x%workdir%"=="x" @set workdir=%MSYSROOT%
@echo Windows Registry Editor Version 5.00>"%workdir%install-context.reg"
@echo.>>"%workdir%install-context.reg"
@echo ^[HKEY_CURRENT_USER\Software\Classes\Folder^]>>"%workdir%install-context.reg"
@echo.>>"%workdir%install-context.reg"
@echo ^[HKEY_CURRENT_USER\Software\Classes\Folder\shell^]>>"%workdir%install-context.reg"
@echo.>>"%workdir%install-context.reg"
@echo ^[HKEY_CURRENT_USER\Software\Classes\Folder\shell\openinmsys^]>>"%workdir%install-context.reg"
@echo ^@^="&MSYS Shell Here">>"%workdir%install-context.reg"
@echo.>>"%workdir%install-context.reg"
@echo ^[HKEY_CURRENT_USER\Software\Classes\Folder\shell\openinmsys\command^]>>"%workdir%install-context.reg"
@echo ^@^="\"%%COMSPEC%%\" /C \"%~dpfx0\"">>"%workdir%install-context.reg"
@echo Windows Registry Editor Version 5.00>"%workdir%uninstall-context.reg"
@echo.>>"%workdir%uninstall-context.reg"
@echo ^[-HKEY_CURRENT_USER\Software\Classes\Folder\shell\openinmsys\command^]>>"%workdir%uninstall-context.reg"
@echo ^[-HKEY_CURRENT_USER\Software\Classes\Folder\shell\openinmsys^]>>"%workdir%uninstall-context.reg"
@echo Context Menu integration generated as install-context.reg and uninstall-context.reg in:
@echo %MSYSROOT%
@goto quit
:install-context
@call :generate
@if "x%workdir%"=="x" @set workdir=%MSYSROOT%
@regedit "%workdir%uninstall-context.reg" && echo Context menu integration set up successfully.
@goto quit
:uninstall-context
@call :generate
@if "x%workdir%"=="x" @set workdir=%MSYSROOT%
@regedit "%workdir%install-context.reg" && echo Context menu integration set up successfully.
@goto quit
:quit
:: quit script with the current error level.
@exit /b %ErrorLevel%
@jfthuong
Copy link

jfthuong commented Jun 28, 2018

I used this script a lot (and deployed in many of my colleagues computer). I also posted a shorter version to answer a question in StackOverflow (https://stackoverflow.com/a/51078800/1603480).

Thanks a lot, man!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment