Wednesday, 4 December 2013

Cygwin Here

This short article explains how to open a specific folder in Cygwin.


I often use Cygwin in my work on Windows. Once I decided to simplify the issue of opening arbitrary folder in Cygwin. I fed up to constantly start Cygwin and perform the cd path command to proceed to the specific folder. Also I wouldn't want to create special aliases for this purpose. Googling of the question has helped me. Moreover. I have summarized, developed and implemented another solution based on the answers from Internet.

I have developed the simple batch script that allows me to start Cygwin in a few ways:
  • from Windows Explorer - by the right click (file managers like Total Commander, Double Commander, etc are available as well)
  • from command line - typing the special command
  • from Far Manager - creating the special menu item

Source code

@echo off

rem It is assumed that this script is located under the Cygwin binaries 
rem directory. Usually it is "c:\cygwin\bin". Put this file as the 
rem "cyghere.bat" under this directory and perform the command 
rem "cyghere.bat /install" to install the script as a part of Windowds 
rem Explorer. Once installed it will be available as "Cygwin Here" item in 
rem the context menu of Windows Explorer. 

if /i "%~1" == "/HELP" (
 echo:Cygwin Here
 echo:%~n0 [/HELP ^| /INSTALL ^| /UNINSTALL ^| /LIST ^| "folder"]
 goto :EOF

rem Add/Remove/List the registry keys
rem   HKEY_CLASSES_ROOT\Directory\shell\Cygwin Here
rem   HKEY_CLASSES_ROOT\Directory\Background\shell\Cygwin Here
rem   HKEY_CLASSES_ROOT\Drive\shell\Cygwin Here

if /i "%~1" == "/INSTALL" (
 for %%p in ( "Drive" "Directory" "Directory\Background" ) do (
  reg add "HKEY_CLASSES_ROOT\%%~p\shell\Cygwin Here\command" /ve /d "\"%~f0\" \"%%V\"" /f
 goto :EOF

if /i "%~1" == "/UNINSTALL" (
 for %%p in ( "Drive" "Directory" "Directory\Background" ) do (
  reg delete "HKEY_CLASSES_ROOT\%%~p\shell\Cygwin Here" /f
 goto :EOF

if /i "%~1" == "/LIST" (
 for %%p in ( "Drive" "Directory" "Directory\Background" ) do (
  reg query "HKEY_CLASSES_ROOT\%%~p\shell\Cygwin Here" /s
 goto :EOF

rem Proceed to the specified folder
if not "%~1" == "" pushd "%~1" || goto :EOF

rem Run Cygwin in the specified directory
start "Cygwin Here" "%~dp0mintty.exe" -i /Cygwin-Terminal.ico -h start "%~dp0bash.exe" -l -c "cd '%CD%' ; exec bash"



This script accepts three arguments:
  • /HELP - to display the help prompt
  • /INSTALL - to install the script to Windows Explorer
  • /UNINSTALL - to uninstall the script from the Windows Explorer
  • /LIST - to display the list of the modified registry keys

Other values are considered as a path where Cygwin should be opened. Empty list of arguments means to open Cygwin in the current directory.

Installation and Usage

To start using of the script do the following steps:
  1. Open the folder where Cygwin binaries are located on your PC (usually it is C:\cygwin\bin)
  2. Create the text file cyghere.bat
  3. Open the newly created file in your favorite editor and put the example above
  4. Save the file and close the editor
  5. Open the Command Line Prompt and perform the command C:\cygwin\bin\cyghere.bat /install

If you have Cygwin installed to another location then you should take this point into account and slighly modify accordingly your environment. For example, I have installed it under C;\PROGS\x\cygwin. And all works!

Open Windows Explorer and do the right click on a folder or drive. You'll see the command Cygwin Here
Open the Command Line Prompt. Type the command C:\cygwin\bin\cyghere.bat.
Repeat the command above appending a path to the specific folder on your system.

Installation and Usage in Far Manager

Far Manager users could improve their work in it if they apply these actions:
  1. Start FAR
  2. Press the keys F2, Ins, confirm Insert the command
  3. Type C as a hotkey, Cygwin Here as a label, C:\cygwin\bin\cyghere.bat as a command
  4. Press OK

Done! Cygwin Here is in your instance of FAR. You can use it pressing the key sequence F2, C.

No comments:

Post a Comment