How can I create a Cygwin shortcut that will open Cygwin at a specific folder? This would obviate having to type
cd /cygdrive/c/Users/Tom/Desktop/
each time I launch Cygwin.
When you install Cygwin (or if you’ve already installed it, download it again and start setup again to run an update), make sure that you select the chere
package under the "Shells" category.
After Cygwin is launched, open up a Cygwin terminal (as an administrator) and type the command: chere -i -t mintty -s bash
.
Now you should have "Bash Prompt Here" in the Windows right-click context menu.
(mintty is Cygwin's default terminal. If you don't choose it with the -t
option, your "Bash Prompt Here" will use the same terminal as the Windows Command Prompt, which prevents horizontal resizing.)
Here's what I use. It doesn't require chere
package or registry tinkering. Works on Windows 7.
Go to your "Send To" folder:
C:\Users\<your_user_name>\AppData\Roaming\Microsoft\Windows\SendTo
Create a shortcut named Bash Here
having this in the Target
field:
C:\cygwin\bin\mintty.exe -i /Cygwin-Terminal.ico C:\cygwin\bin\bash.exe -l -c "cd \"$0\" ; exec bash"
In the Windows Explorer, you right-click on a folder & select Send To > Bash Here
.
And here's the opposite trick, opening a Windows Explorer in your current bash dir. Create this alias:
alias winx='/cygdrive/c/Windows/explorer.exe /e,\`cygpath -w .\`'
Note: the cygpath -w .
part above is enclosed in back-ticks.
Now just type winx
at the bash prompt, and a Win Explorer pops up there.
Note: If winx directs you to your documents folder, try
alias winx='/cygdrive/c/Windows/explorer.exe /e,`cygpath -w $PWD`'
I made a .reg file that puts an "Open Cygwin Here" option in the right-click context menu. It depends on the Cygwin "chere" package, which you can install using apt-cyg if you didn't install it in the initial setup.
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\Background\shell\cygwin_bash]
@="Open Cygwin Here"
[HKEY_CLASSES_ROOT\Directory\Background\shell\cygwin_bash\command]
@="C:\\cygwin\\bin\\mintty.exe -e /bin/xhere /bin/bash.exe"
For cygwin64 or installations without chere you can use the following command in a registry entry (assuming windows due to your path, also assuming cygwin installation directory is c:\cygwin64)
C:\cygwin64\bin\mintty.exe /bin/sh -lc 'cd "`cygpath "%V"`"; bash'
Works on Windows 7 and 8 Registry file available for download here: http://tomkay.me/blog/Cygwin64---Open-Here-18
Windows Registry Editor Version 5.00
; Open cygwin to folder
; http://tomkay.me - Tom Kay
[HKEY_CLASSES_ROOT\Folder\shell\open_cygwin]
@="Open Cygwin Here"
[HKEY_CLASSES_ROOT\Folder\shell\open_cygwin\command]
@="C:\\cygwin64\\bin\\mintty.exe /bin/sh -lc 'cd \"`cygpath \"%V\"`\"; bash'"
[HKEY_CLASSES_ROOT\Directory\Background\shell\open_cygwin]
@="Open Cygwin Here"
[HKEY_CLASSES_ROOT\Directory\Background\shell\open_cygwin\command]
@="C:\\cygwin64\\bin\\mintty.exe /bin/sh -lc 'cd \"`cygpath \"%V\"`\"; bash'"
If you want to have that directory as your default, simply add a cd
statement to your ~/.profile
file.
What I tend to do is use that method to set my usual directory, plus define aliases for my common cases as well:
alias tom="cd /users/tom"
or your equivalent. This lets me change directories very fast.
As two7s_clash said you first need to install chere
package and setup mintty:
apt-cyg install chere
chere -i -t mintty
You are now able to open cygwin in specific directory with a Right mouse click in Windows Explorer (Context Menu) and select "Bash Prompt Here".
You can also open cygwin from a specific directory using windows command prompt:
cd
) to custom directoryExecute C:\cygwin64\bin\mintty.exe C:\cygwin64\bin\env.exe CHERE_INVOKING=1 C:\cygwin64\bin\bash.exe -l
This command will open cygwin with current directory taken from command prompt.
This command can also be used to open cygwin from custom file manager, like FreeCommander.
To open cygwin with current directory taken from FreeCommander, do the following:
Add a new toolbar (+ Icon), Shortcut: Insert
C:\cygwin64\bin\mintty.exe
%ActivDir%
C:\cygwin64\bin\env.exe CHERE_INVOKING=1 C:\cygwin64\bin\bash.exe -l
You can add custom shortcut to open cygwin from FreeCommander:
Great reference: MinTTY Wiki, article Tips: Starting in a particular directory
I use and Icon to launch my cygwin without the chere
package.
C:\cygwin64\bin\mintty.exe -i /Cygwin-Terminal.ico -c 'cd'
C:\some\dir\name\here
To create a Windows shortcut that launches a Cygwin terminal in a directory of your own choosing, try the following:
Right-click on the Windows desktop, select 'New', and then select 'Shortcut'.
For location of the item, enter the following text, changing the mintty
path as needed and substituting the name of the desired directory where indicated.
C:\cygwin64\bin\mintty.exe /bin/sh -lc 'cd DESIRED-DIRECTORY; exec bash'
For example, the OP would use the following text:
C:\cygwin64\bin\mintty.exe /bin/sh -lc 'cd /cygdrive/c/Users/Tom/Desktop/; exec bash'
Click 'Next'.
Enter the desired name for the shortcut and click 'Finish'.
Multiple shortcuts can be placed on the desktop to open Cygwin terminals in various often-accessed directories.
Inspired by solution posted on How to open a Cygwin shell at a specific directory from Netbeans? at superuser.com.
I used the following lines of code to start zsh in the current directory in Win7.
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\Background\shell\cygwin_bash]
@="Open Cygwin Here"
"NoWorkingDirectory"=""
[HKEY_CLASSES_ROOT\Directory\Background\shell\cygwin_bash\command]
@="\"C:\\Programmieren\\cygwin64\\bin\\mintty.exe\" -i /Cygwin-Terminal.ico /bin/zsh --login -c 'cd \"%V\";zsh'"
note that i used %V instead of %L or %1 (since both didn't work for me)
When a fresh install is needed, I create a Windows "user environment variable " named HOME
and assigns it the path of wherever "My Documents" reside.
The cygwin installer detects the HOME
variable, automatically translates this into a cygpath and selects this it to be my ~ directory.
This has worked fine for every workstation I have used professionally the last 5 years (about 3 or 4, Win7). I have always been the only user on these machines, cannot say what the effect is.
I have made a registry edit script to open Cygwin at any folder you right click. It's on my GitHub.
Sample RegEdit
code from Github for 64-bit machines:
REGEDIT4
[HKEY_CLASSES_ROOT\Directory\shell\CygwinHere]
@="&Cygwin Bash Here"
[HKEY_CLASSES_ROOT\Directory\shell\CygwinHere\command]
@="C:\\cygwin64\\bin\\mintty.exe -i /Cygwin-Terminal.ico C:\\cygwin64\\bin\\bash.exe --login -c \"cd \\\"%V\\\" ; exec bash -rcfile ~/.bashrc\""
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\Background\shell\CygwinHere]
@="&Cygwin Bash Here"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\Background\shell\CygwinHere\command]
@="C:\\cygwin64\\bin\\mintty.exe -i /Cygwin-Terminal.ico C:\\cygwin64\\bin\\bash.exe --login -c \"cd \\\"%V\\\" ; exec bash -rcfile ~/.bashrc\""
Save the following code as a file: cygwin_bash.reg
This will add opening Cygwin in the current directory and opening Cygwin in the specified folder at the same time.
Use ".bashrc
" instead of ".bash_profile
" configuration. Because of exec bash
. .bashrc
is executed for interactive non-login shells. see: https://apple.stackexchange.com/questions/51036/what-is-the-difference-between-bash-profile-and-bashrc
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\Background\shell\z_cygwin_bash]
@="Cygwin Here"
"Icon"="C:\\cygwin64\\Cygwin.ico"
[HKEY_CLASSES_ROOT\Directory\Background\shell\z_cygwin_bash\command]
@="C:\\cygwin64\\bin\\mintty.exe /bin/sh -lc 'cd \"`cygpath \"%V\"`\"; exec bash'"
[HKEY_CLASSES_ROOT\Directory\shell\z_cygwin_bash]
@="Cygwin Here"
"Icon"="C:\\cygwin64\\Cygwin.ico"
[HKEY_CLASSES_ROOT\Directory\shell\z_cygwin_bash\command]
@="C:\\cygwin64\\bin\\mintty.exe /bin/sh -lc 'cd \"`cygpath \"%V\"`\"; exec bash'"
You can add the icon to the shell by adding an Icon
field set to the path to your Cygwin.ico
file.
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\Background\shell\cygwin_bash]
@="Cygwin"
"Icon"="\"C:\\cygwin64\\Cygwin.ico\""
[HKEY_CLASSES_ROOT\Directory\Background\shell\cygwin_bash\command]
@="C:\\cygwin64\\bin\\mintty.exe -e /bin/xhere /bin/bash.exe"
In the answer that uses the registry, I found that on Windows 2008 R2 SP1, you need to drop the Background out of the path. Further, since the chere and xhere are not part of cygwin64, here is a solution that works for both, combining the registry and the Send To solutions. Replace 'E:\cygwin64' with your install location:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\shell\cygwin_bash]
@="Open Cygwin Here"
[HKEY_CLASSES_ROOT\Directory\shell\cygwin_bash\command]
@="E:\\cygwin64\\bin\\mintty.exe -i /Cygwin-Terminal.ico --exec /bin/bash.exe --login -c \"cd '%1' ; exec bash -rcfile ~/.bashrc\""
This is the only Cygwin64 registry solution that worked for me in Windows 8.1:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\shell\mintty]
@="Mintty from Here"
"NoWorkingDirectory"=""
[HKEY_CLASSES_ROOT\Directory\shell\mintty\command]
@="C:\\cygwin64\\bin\\mintty.exe -h always -e /usr/bin/ash -c 'cd \"$(/usr/bin/cygpath \"%L\")\"; exec /usr/bin/bash '"
Make sure you modify your 'C:\cygwin64' path as necessary.
I have created the batch file and put it to the Cygwin's /bin directory. This script was developed so it allows to install/uninstall the registry entries for opening selected folders and drives in Cygwin. For details see the link http://with-love-from-siberia.blogspot.com/2013/12/cygwin-here.html.
update: This solution does the same as early suggestions but all manipulations with Windows Registry are hidden within the script.
Perform the command to install
cyghere.bat /install
Perform the command to uninstall
cyghere.bat /uninstall
I had also problem with git, that used to "steal" my cygwin commands - so i needed to run C:\cygwin\bin\mintty.exe -
but after installing chere
and executing chere -i -t mintty -f
as admin (see above), you can simply use : C:\cygwin\bin\mintty.exe -e /bin/xhere /bin/bash.exe "."
I added this in Total Commander as custom button (right click on panel > add ...) and it works very well.
Create a bash file say move.sh
which has the following code
C:\cygwin64\bin\run.exe -p /bin bash runFile.sh
This starts the Cygwin and executes the runFile.sh
present in the bin directory of Cygwin
Suppose you want to navigate to a specific directory say E:\code
then runFile.sh has the following code cd E:
cd code
Finally an answer which is independent of Cygwin itself.
This uses the fact that, if I am on the directory C:\\Cool
and I call the command C:\\Cygwin\\bin\\mintty.exe
, mintty
will automatically open on the current directory, i.e., C:\\Cool
.
First, you will need to create the file C:\\Cygwin\\silent_run.vbs
with the following contents:
Function EnquoteString(argument)
EnquoteString = Chr(34) & argument & Chr(34)
End Function
arglist = ""
With WScript.Arguments
For Each arg In .Unnamed
' Wscript.Echo "Unnamed: " & arg
If InStr(arg, " ") > 0 Then
' arg contains a space
arglist = arglist & " " & EnquoteString(arg)
Else
arglist = arglist & " " & arg
End If
Next
End With
CreateObject("Wscript.Shell").Run Trim( arglist ), 0, False
Next, to install this answer, you will use a Windows Registry file. For that, just put the contents of the following file into a file named C:\\Cygwin\\AddMinttyToContextMenu.reg
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\Background\shell\Terminal Here]
"Icon"="\"C:\\Cygwin\\bin\\mintty.exe\""
"Position"="Middle"
@="Terminal Here"
"CommandFlags"=dword:00000020
[HKEY_CLASSES_ROOT\Directory\Background\shell\Terminal Here\Command]
@="\"C:\\Cygwin\\bin\\mintty.exe\" -w max"
[HKEY_CLASSES_ROOT\Directory\shell\Terminal Here]
"Icon"="\"C:\\Cygwin\\bin\\mintty.exe\""
"Position"="Middle"
@="Terminal Here"
"CommandFlags"=dword:00000020
[HKEY_CLASSES_ROOT\Directory\shell\Terminal Here\Command]
@="cmd.exe /c cd /d \"%V\" && wscript \"C:\\Cygwin\\silent_run.vbs\" \"C:\\Cygwin\\bin\\mintty.exe\" -w max"
Now, Fix all hard coded paths, i.e, C:\\Cygwin
to the actual location where your Cygwin installation is on.
Then, just open the file C:\\Cygwin\\AddMinttyToContextMenu.reg
to install your new registry entries and you are done.
The file C:\\Cygwin\\AddMinttyToContextMenu.reg
works by opening first a cmd.exe
, changing to the directory where you are in, then, calling C:\\Cygwin\\silent_run.vbs
to open the C:\\Cygwin\\bin\\mintty.exe
terminal with the command line options -w max
, i.e., to open it maximized.
The script C:\\Cygwin\\silent_run.vbs
is required to open the C:\\Cygwin\\bin\\mintty.exe
terminal without keeping the first cmd.exe
we opened, open.
The first entry of C:\\Cygwin\\AddMinttyToContextMenu.reg
does not use C:\\Cygwin\\silent_run.vbs
because by default the HKEY_CLASSES_ROOT\Directory\Background\shell
keys are already open in the current directory, then, we can just call C:\\Cygwin\\bin\\mintty.exe
directly to get it working out of the box.
References:
I don't know why I had to wast so much time, but this works for me on win 10, 64 bit:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\Background\shell\cygwin_bash]
@="Open Cygwin Here"
[HKEY_CLASSES_ROOT\Directory\Background\shell\cygwin_bash\command]
@="C:\\cygwin64\\bin\\mintty.exe -i /Cygwin-Terminal.ico C:\\cygwin64\\bin\\bash.exe --login -c \"cd \\\"%V\\\" ; exec bash -rcfile ~/.bashrc\""
Probably the simplest one:
1) Create file foo.reg
2) Insert content:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\background\shell\open_mintty]
@="open mintty"
[HKEY_CLASSES_ROOT\Directory\background\shell\open_mintty\command]
@="cmd /C mintty"
3) Execute foo.reg
Now just right-click in any folder, click open mintty
and it will spawn mintty in that folder.
sudo
, you may not get the expected result. In this case, login as an admin user on the windows machine. – dreftymac