Cygwin-添加到右鍵菜單腳本--一鍵安裝、卸載

平時習慣用一些linux命令來完成工做,在Windows上有cygwin和gitbash兩個選擇。這兩個我都裝了。
相對來講cygwin支持的功能更多一些,可是它沒有默認綁定到右鍵菜單。爲此,我想到用萬能的註冊表解決這個事情。網上搜索了一下,把我眼中best answer貼出來供你們分享。
注把下面代碼保存成cygwin.bat 放到cygwin安裝目錄的bin目錄裏面,而後用管理員權限啓動cmd去運行該腳本 如 cygwin.bat /HELPhtml

@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

代碼主體是照抄前人的,如侵刪~
我加了圖標項,以及一些註釋。linux

順便說一句,windows上的bat腳本至關於linux上的shell腳本,rem至關於#,goto :EOF 至關於exit。
其中reg add,reg query都是reg命令,我修改上述腳本時參考園封記憶git

感謝互聯網以及無私分享知識的前人們,我等也要繼承這份精神!shell

參考:
1. 好庫文摘 http://doc.okbase.net/jevyzhu/archive/82840.html
2. 博客園 http://www.cnblogs.com/fanyf/p/4221488.htmlwindows

相關文章
相關標籤/搜索