CMD Command for "Git Bash Here" - windows

In Windows I can run "Git Bash Here" in the current directory by opening right click menu.
I want to place gitbashhere.bat in default path for commands to have got easy access from standard cmd window.
The sh.exe --login -i command only opens git bash in sh.exe's directory instead of the current directory.
How do I check what command is bound to "Git Bash Here"?

How do I check what command is bound to "Git Bash Here"?
Open the Registry Editor using regedt32 or regedit.
Menu "Edit" > "Find" > "Find what" and enter "git_shell"
Examine the returned Registry entries.
One will be:
HKEY_CLASSES_ROOT\Directory\shell\git_shell\command
With the value:
"C:\Windows\SysWOW64\cmd.exe" /c "pushd "%1" && "C:\git_install_dir\bin\sh.exe" --login -i"
where git_install_dir is the directory where you have installed git.
You can see it runs something similar to the following command:
pushd "%1" && "C:\git_install_dir\bin\sh.exe" --login -i

Another way to see how a specific program was started is to launch Process Explorer, look for the process in the list of processes (for git this would be "git-bash.exe") and double-click on the process to open the properties.
On the 'Image' tab there's a text field that yields the command line of the process:

That is a windows shell thing. It depends on the git GUI toolkit that you've installed. For tortoiseGit, there's a setting hidden somewhere in the configuration menu. If that doesn't suit you: It's free and open software, so fix that :)

Related

How open the current directory on Git Bash in windows using keyboard shortcut?

In my Windows laptop i want to set a shortcut key such that whenever I click on the shortcut keys from my keyboard it will open the Git Bash in the current directory and the path would also be the current directory in Git Bash.
Example:
Let i have choose a shortcut Ctrl + G. So, i want that whenever i click on Ctrl+G it will open a git bash and the path on Git bash should be the current directory from where i have entered the shortcut keys.
Inplace of opening Git Bash from right-context, i want to do the same from keyboard only by clicking some key combination to open the Git Bash.
Unable to find any solution to this query. In place of it there are solution to setup on right-click context and i want it to do with the help of keyboard.
You can setup an AutoHotkey script as in here to open a CMD in the current path.
Except instead of opening a new CMD, you can run bash --login, assuming C:\Program Files\Git\bin is in your %PATH%.
Run, cmd /K (start /d "C:\your\path" bash --login)

enable get bash here/get gui here on Windows [duplicate]

How to add a context (aka right click) menu to the windows explorer that, when clicked, opens the git-bash console in the current explorer folder?
Step 1. On your desktop right click "New"->"Text Document" with name OpenGitBash.reg
Step 2. Right click the file and choose "Edit"
Step 3. Copy-paste the code below, save and close the file
Step 4. Execute the file by double clicking it
Note: You need administrator permission to write to the registry.
Windows Registry Editor Version 5.00
; Open files
; Default Git-Bash Location C:\Program Files\Git\git-bash.exe
[HKEY_CLASSES_ROOT\*\shell\Open Git Bash]
#="Open Git Bash"
"Icon"="C:\\Program Files\\Git\\git-bash.exe"
[HKEY_CLASSES_ROOT\*\shell\Open Git Bash\command]
#="\"C:\\Program Files\\Git\\git-bash.exe\" \"--cd=%1\""
; This will make it appear when you right click ON a folder
; The "Icon" line can be removed if you don't want the icon to appear
[HKEY_CLASSES_ROOT\Directory\shell\bash]
#="Open Git Bash"
"Icon"="C:\\Program Files\\Git\\git-bash.exe"
[HKEY_CLASSES_ROOT\Directory\shell\bash\command]
#="\"C:\\Program Files\\Git\\git-bash.exe\" \"--cd=%1\""
; This will make it appear when you right click INSIDE a folder
; The "Icon" line can be removed if you don't want the icon to appear
[HKEY_CLASSES_ROOT\Directory\Background\shell\bash]
#="Open Git Bash"
"Icon"="C:\\Program Files\\Git\\git-bash.exe"
[HKEY_CLASSES_ROOT\Directory\Background\shell\bash\command]
#="\"C:\\Program Files\\Git\\git-bash.exe\" \"--cd=%v.\""
And here is your result :
I had a similar issue and I did this.
Step 1 : Type "regedit" in start menu
Step 2 : Run the registry editor
Step 3 : Navigate to HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell. If you don't have the shell key, create one.
Step 4 : Right-click on "shell" and choose New > Key. name the Key "Bash"
Step 5 : Modify the value and set it to "open in Bash" This is the text that appears in the right click.
Step 6 : Create a new key under Bash and name it "command". Set the value of this key to your git-bash.exe path.
Close the registry editor.
You should now be able to see the option in right click menu in explorer
PS Git Bash by default picks up the current directory.
EDIT : If you want a one click approach, check Ozesh's solution below
The easiest way is to install the latest Git from here. And while installing, make sure you are enabling the option Windows Explorer Integration.
Once you are done, you will get those options in whenever you right click on any folder.
Hope it helps.
Here are the Registry exports (*.reg files) for Git GUI and Git Bash directly from the Windows installer —Git GUI:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\background\shell\git_gui]
#="Git &GUI Here"
"Icon"="C:\\Program Files\\Git\\cmd\\git-gui.exe"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\background\shell\git_gui\command]
#="\"C:\\Program Files\\Git\\cmd\\git-gui.exe\" \"--working-dir\" \"%v.\""
Git bash:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\background\shell\git_shell]
#="Git Ba&sh Here"
"Icon"="C:\\Program Files\\Git\\git-bash.exe"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\background\shell\git_shell\command]
#="\"C:\\Program Files\\Git\\git-bash.exe\" \"--cd=%v.\""
For detail about *.reg files, see “How to add, modify, or delete registry subkeys and values by using a .reg file” from Microsoft.
You can install git for windows or Github for windows , both give you the choice while installing to add this feature to your windows explorer. You can find it here:
Github for Windows
https://windows.github.com/
Git for Windows
http://git-scm.com/
As, #Shaswat Rungta said: "I think the question is more about how to add it after the installation is over."
On my PC(Windows 7) I think that the command "Git Bash here" disappeard after I installed Visual Studio 2017.
I fixt this by downloading and installing Git again.
NOTE: "When installing Git for Windows the context menu options are not 'on' by default. You will have to select them during the install." – #nbushnell
(I did this)
What worked for me was almost this, but with the following REGEDIT path:
HKEY_LOCAL_MACHINE/SOFTWARE/Classes/Directory/background/shell and here I created the key Bash, with the value of what I want the display name to be, and then created another key under this named command with the value as the path to git-bash.exe
I'm on Windows 10 and have a fresh git install that didn't add this automatically for some reason (git version 2.12.0 64bit)
Had a similar issue in adding "Start Command Prompt with Ruby" to context menu as it involves passing parameters along with the patch of cmd. Followed a similar procedure as the solution above
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\Cmd With Ruby]
#="Cmd With Ruby"
"Icon"="C:\\Windows\\System32\\cmd.exe"
[HKEY_CLASSES_ROOT\*\shell\Cmd With Ruby\command]
#="\"C:\\Windows\\System32\\cmd.exe\" \"/E:ON /K
\"C:\\Ruby25-x64\\bin\\setrbvars.cmd\"\" \"--cd=%1\"\""
[HKEY_CLASSES_ROOT\Directory\shell\bash]
#="Cmd With Ruby"
"Icon"="C:\\Windows\\System32\\cmd.exe"
[HKEY_CLASSES_ROOT\Directory\shell\bash\command]
#="\"C:\\Windows\\System32\\cmd.exe\" \"/E:ON /K
\"C:\\Ruby25-x64\\bin\\setrbvars.cmd\"\" \"--cd=%1\"\""
[HKEY_CLASSES_ROOT\Directory\Background\shell\bash]
#="Cmd With Ruby"
"Icon"="C:\\Windows\\System32\\cmd.exe"
[HKEY_CLASSES_ROOT\Directory\Background\shell\bash\command]
#="\"C:\\Windows\\System32\\cmd.exe\" \"/E:ON /K
\"C:\\Ruby25-x64\\bin\\setrbvars.cmd\"\" \"--cd=%v.\"\""
When you install git-scm found in "https://git-scm.com/downloads" uncheck the "Only show new options" located at the very bottom of the installation window
Make sure you check
Windows Explorer integration
Git Bash Here
Git GUI Here
Click Next and you're good to go!
Usually git bash here can be run only on directories so you have to go up a directory and right click on the previous directory then select git bash here (of course on Windows OS).
Note: context menu inside a directory does not have a git bash here option.
Another option but using PowerShell script.
This is basically a PowerShell script which does almost the same as this answer (it also adds icon).
Create file (addOpenGitBashHereContextMenu.ps1), paste in the code below and run it from PowerShell.
You probably need to change exePath to point to your git executable.
Also change contextMenuText if you want to change the text in context menu.
$exePath = $env:USERPROFILE + '\scoop\apps\git\current\git-bash.exe'
$contextMenuText = 'Open Git Bash Here'
$regEntryName = 'git_shell'
$regPathShell = 'HKCU:\SOFTWARE\Classes\Directory\Background\shell'
$regPathContextMenu = $regPathShell + '\' + $regEntryName
$regPathCommand = $regPathContextMenu + '\command'
$user = $env:UserName
echo "Setting '$contextMenuText' context menu..."
echo " User name: '$user'"
echo " Register path: '$regPathCommand'"
echo " Executable path: '$exePath'"
# Key: shell
If (-NOT (Test-Path $regPathShell)) {
New-Item -Path $regPathShell
}
# Key: regEntryName
If (-NOT (Test-Path $regPathContextMenu)) {
New-Item -Path $regPathContextMenu -Value $contextMenuText
# Add Icon to context menu
Set-ItemProperty -Path $regPathContextMenu -Name 'Icon' -Value $exePath
}
# Key: command
If (-NOT (Test-Path $regPathCommand)) {
New-Item -Path $regPathCommand -Value $exePath
}
Try updating your git. For window user you can update your git via cmd by simply writing the command- "git update-git-for-windows".
When done, you can simply right click inside a directory where you want to open the gitbash and check if its showing "Gitbash here" or not. If it's not showing anything, try to look for "Show more options" there and click on it. Now, you may able to see it.
I ran into this problem installing Git-2.38.1-64-bit on a new Windows 10 Pro corporate laptop.
I tried several things ... and considered one of the regedit scripts (the moral equivalent of "Nurse - chainsaw!")... but resolved the problem by simply uninstalling and reinstalling Git (as "Admin").
Specifically:
Control Panel > Programs/Features > Uninstall Git
Reboot Windows
Reinstall Git-2.38.1-64-bit as "Admin"
Carefull check all the options, especially:
"Windows Explorer Integration: Git Bash Here, Git GUI Here
<= These are checked by default ... and I'm sure they were both "Y" on the initial install
I copied my .gitconfig ("C:\Users\USERNAME.gitconfig") from another PC
<= I don't believe this was "essential" ...but I thought I'd mention it...
Browse to a local Git repo and verify the Git context menus work
<= Success after reinstall!
I updated my git and I marked the option of "Git Bash Here"
I have already too many programs that want a spot on my PATH list. Rather than pollute my PATH list, I choose instead to add a symbolic link, from an elevated command prompt, like so.
mklink C:\bin\git-bash.exe "C:\Program Files\Git\git-bash.exe"
While there are exceptions, this works in the majority of cases, including, among others, the 7-zip CLI, selected SysInternals command line tools, the NuGet CLI, and many others.
As a bonus, having the Git Bash command line interface so readily accessible makes activating it from an open command prompt window trivial. With this improvement, I can probably live without yet another item on my context menu for directories.
In addition to the answers with the theory and the minimal examples see the example of the .reg file for Git (and not only, with the instruction) with user-friendly names, shortcut keys (& in a user-friendly name) and icons in the "win-registry-snippets" repository
Do Nothing.
Just open your git bash terminal and type in:
git update-git-for-windows
Once update is completed, this will automatically add "Git Bash here" into your context menu.
Refer this: https://stackoverflow.com/a/65467670
You can install TortoiseGit for Windows and include integration in context menu.
I consider it the best tool to work with Git on Windows.
Add the gitpath to the Environment-path variable (e.g. C:\Program Files\Git\cmd) by which you can access git from any folder using command line.
Use Shift + F10 to open the menu of options which appear when right clicked, and then click s to git bash here.

(ConEmu + Cygwin) How to change tab name of ConEmu from within a cygwin bash script

i've configuring my ConEmu + Cygwin enviroment. I've created a task, when i start it in a tab, the task will run a batch file, which in turn will change dir into cygwin and run
bash --login -i my_ssh_entry_script.sh
Inside my_ssh_entry_script.sh, it will read a config file in my home directory then print a menu for me to select which host to connect. And finally
...
exec ssh -p$port $userhost
Now i can work on the selected machine in the same conemu tab. This works fine. And the script also works on linux machine too.
But there is a little flaw. I can't change the tab's title. I've tried to change it to:
....
exec $(cygpath ${ConEmuDir})/ConEmu.exe /cmd ssh -p$port $userhost -cur_console:t:$title
But this will always create a new tab. Any suggestion to slove it? Thanks~
1) You need GuiMacro, thoroughly described in the project wiki:
http://conemu.github.io/en/GuiMacro.html
ConEmuC -GuiMacro Rename 0 "Title"
2) You do not need to run batch. It causes extra and useless cmd.exe in yours process tree. You can do all required "CD" and "SET" from the task content directly.
At least for cmd shell:
"-new_console:d:C:\Users\dir_name" cmd /V /K -new_console:t:Tab_renamed

How do I launch a Git Bash window with particular working directory using a script?

How can I launch a new Git Bash window with a specified working directory using a script (either Bash or Windows batch)?
My goal is to launch multiple Git Bash windows from a single script, each set to a different working directory. This way I can quickly get to work after booting the computer instead of having to open Git Bash windows and navigating each one to the correct working directory.
I am not asking how to change the default working directory, like this question does, but to launch one or more terminal windows with different working directories from a script.
Another option is to create a shortcut with the following properties:
Target should be:
"%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login
Start in is the folder you wish your Git Bash prompt to launch into.
Try the --cd= option. Assuming your GIT Bash resides in C:\Program Files\Git it would be:
"C:\Program Files\Git\git-bash.exe" --cd="e:\SomeFolder"
If used inside registry key, folder parameter can be provided with %1:
"C:\Program Files\Git\git-bash.exe" --cd="%1"
Git Bash uses cmd.exe for its terminal plus extentions from MSYS/MinGW which are provided by sh.exe, a sort of cmd.exe wrapper. In Windows you launch a new terminal using the start command.
Thus a shell script which launches a new Git Bash terminal with a specific working directory is:
(cd C:/path/to/dir1 && start sh --login) &
(cd D:/path/to/dir2 && start sh --login) &
An equivalent Windows batch script is:
C:
cd \path\to\dir1
start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login
D:
cd \path\to\dir2
start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login
To get the same font and window size as the Git Bash launched from the start menu, it is easiest to copy the start menu shortcut settings to the command console defaults (to change defaults, open cmd.exe, left-click the upper left icon, and select Defaults).
Let yet add up to the answer from #Drew Noakes:
Target:
"C:\Program Files\Git\git-bash.exe" --cd=C:\GitRepo
The cd param should be one of the options how to specify the working directory.
Also notice, that I have not any --login param there: Instead, I use another extra app, dedicated just for SSH keys: Pageant (PuTTY authentication agent).
Start in:
C:\GitRepo
The same possible way, as #Drew Noakes mentioned/shown here sooner, I use it too.
Shortcut key:
Ctrl + Alt + B
Such shortcuts are another less known feature in Windows. But there is a restriction: To let the shortcut take effect, it must be placed somewhere on the User's subdirectory: The Desktop is fine.
If you do not want it visible, yet still activatable, place this .lnk file i.e. to the quick launch folder, as that dir is purposed for such shortcuts. (no matter whether displayed on the desktop) #76080 #3619355
"\Application Data\Microsoft\Internet Explorer\Quick Launch\"
In addition, Win10 gives you an option to open git bash from your working directory by right-clicking on your folder and selecting GitBash here.
Windows 10
This is basically #lengxuehx's answer, but updated for Win 10, and it assumes your bash installation is from Git Bash for Windows from git's official downloads.
cmd /c (start /b "%cd%" "C:\Program Files\GitW\git-bash.exe") && exit
I ended up using this after I lost my context-menu items for Git Bash as my command to run from the registry settings. In case you're curious about that, I did this:
Create a new key called Bash in the shell key at HKEY_CLASSES_ROOT\Directory\Background\shell
Add a string value to Icon (not a new key!) that is the full path to your git-bash.exe, including the git-bash.exe part. You might need to wrap this in quotes.
Edit the default value of Bash to the text you want to use in the context menu
Add a sub-key to Bash called command
Modify command's default value to cmd /c (start /b "%cd%" "C:\Program Files\GitW\git-bash.exe") && exit
Then you should be able to close the registry and start using Git Bash from anywhere that's a real directory. For example, This PC is not a real directory.
This is the command which can be executed directly in Run dialog box (shortcut is win+R) and also works well saved as a .bat script:
cmd /c (start /d "/path/to/dir" bash --login) && exit
I'm not familiar with Git Bash but assuming that it is a git shell (such as git-sh) residing in /path/to/my/gitshell and your favorite terminal program is called `myterm' you can script the following:
(cd dir1; myterm -e /path/to/my/gitshell) &
(cd dir2; myterm -e /path/to/my/gitshell) &
...
Note that the parameter -e for execution may be named differently with your favorite terminal program.
Using Windows Explorer, navigate to any directory you want, type "cmd" in the address bar it will open Windows command prompt in that directory.
Along the same lines, if you have the git directory in your path, you can type "git-bash" in the address bar and a Git Shell will open in that directory.
If using Windows OS :
Right click on git terminal > Properties
Properties>Under shortcut tab>Start in:
add your folder target path like below image

Open Cygwin at a specific folder

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`'
From the cygwin terminal, run this command:
echo "cd your_path" >> ~/.bashrc
The .bashrc script is run when you open a new bash session. The code above with change to the your_path directory when you open a new cygwin session.
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:
Open Cygwin terminal as administrator
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:
Open windows command prompt
Navigate (cd) to custom directory
Execute 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.
FreeCommander
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:
Tools -> Favorite Tools -> Favorite tools edit... (Ctrl + Shift + Y)
Add a new toolbar (+ Icon), Shortcut: Insert
Name: cygwin
Program or folder: C:\cygwin64\bin\mintty.exe
Start folder: %ActivDir%
Parameter: C:\cygwin64\bin\env.exe CHERE_INVOKING=1 C:\cygwin64\bin\bash.exe -l
You can add custom shortcut to open cygwin from FreeCommander:
Tools -> Define keyboard shortcuts
Scroll down to "Favorite tool 01" (or "Favorite tool N")
Assign new shortcut key: I use Ctrl + Shift + T
Great reference: MinTTY Wiki, article Tips: Starting in a particular directory
I use and Icon to launch my cygwin without the chere package.
Create a shortcut on my desktop for the cygwin terminal.
R-click the icon and select properties.
On the shortcut tab, use this for the TARGET:
C:\cygwin64\bin\mintty.exe -i /Cygwin-Terminal.ico -c 'cd'
For START IN, Put the path of the dir/folder where you want to
launch cygwin. i.e. 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)
Find file Cygwin.bat and make content like this:
#echo off
set newpath=%cd:\=/%
pushd "%~dp0"
chdir bin
bash --login -i -c "cd \"%newpath%\"; exec bash"
Add path to cygwin.bat to environment PATH.
Now in any folder you can type to address bar:
cygwin
Also you can type it when you in cmd.exe
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.
Here's 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'"
Open Cygwin terminal as Administrator
In powershell (using chocolatey):
choco install cyg-get
In cygwin
Will Install right-click menu
cyg-get install chere
chere -i
Now you can right click and use "Bash Prompt Here" in any folder.
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"
On my Windows machine it was not taking:
chere -i -t mintty
Because mintty shell was not available,but you might have choice of various other shells like ash bash cmd dash mksh pdksh posh tcsh zsh passwd.
so I used :
chere -i -s bash
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
You can just open cygwin terminal and write: cd and after drag n drop the folder you want end enter!
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:
How add context menu item to Windows Explorer for folders
VBScript pass commandline argument in paths with spaces
How to pass a command with spaces and quotes as a single parameter to CScript?
calling vbscript from another vbscript file passing arguments
https://ss64.com/vb/syntax-args.html
Check if string contains space
Running command line silently with VbScript and getting output?
http://www.vbsedit.com/html/6f28899c-d653-4555-8a59-49640b0e32ea.asp
https://superuser.com/questions/62525/run-a-batch-file-in-a-completely-hidden-way
How to run Batch script received as argument on VBscript?
Can I pass an argument to a VBScript (vbs file launched with cscript)?
#echo off
C:
SET mypath=%~dp0
c:\cygwin\bin\bash -c "cd '%mypath%'; export CHERE_INVOKING=1; exec /bin/bash --login -i"
Copy above commands in a text file and save it as .bat in any of "your folder of interest".
It should open cygwin in "your folder of interest".
based on #LindseyD answer I created a simple BAT file, that opens cygwin in current directory, it may be useful (for me it is). Assuming that You have cygwin's bin directory in PATH.
FOR /F %%x IN ('sh -c pwd') DO bash -l -i -c 'cd %%x; exec bash'
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.
Best to do like below:
HKEY_CLASSES_ROOT\Directory\shell\BashHere
Enter Data: Bash Here
HKEY_CLASSES_ROOT\Directory\shell\BashHere\command
Enter Data:
cmd.exe /c C:\cygwin\bin\bash.exe --login -c "cd '%1'; exec /bin/bash"

Resources