Cygwin-添加到右键菜单脚本--一键安装、卸载

平时习惯用一些linux命令来完成工作,在Windows上有cygwin和gitbash两个选择。这两个我都装了。
相对来说cygwin支持的功能更多一些,但是它没有默认绑定到右键菜单。为此,我想到用万能的注册表解决这个事情。网上搜索了一下,把我眼中best answer贴出来供大家分享。
注把下面代码保存成cygwin.bat 放到cygwin安装目录的bin目录里面,然后用管理员权限启动cmd去运行该脚本 如 cygwin.bat /HELP

@echo off

rem current filename
echo "%~f0"
rem current filedir
echo "%~dp0"
echo "Usually it is runned with Administrator power"

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:
 echo:Usage:
 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 (
    echo "longwind"
  reg add "HKEY_CLASSES_ROOT\%%~p\shell\Cygwin Here" /v "Icon" /t "REG_SZ" /d "%~dp0\..\Cygwin.ico" /f
  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"
popd

代码主体是照抄前人的,如侵删~
我加了图标项,以及一些注释。

顺便说一句,windows上的bat脚本相当于linux上的shell脚本,rem相当于#,goto :EOF 相当于exit。
其中reg add,reg query都是reg命令,我修改上述脚本时参考园封记忆

感谢互联网以及无私分享知识的前人们,我等也要继承这份精神!

参考:
1. 好库文摘 http://doc.okbase.net/jevyzhu/archive/82840.html
2. 博客园 http://www.cnblogs.com/fanyf/p/4221488.html

posted @ 2017-09-24 20:01  longwind09  阅读(349)  评论(0编辑  收藏  举报