如何创建在特定文件夹中打开Cygwin的Cygwin快捷方式?这样就不用打字了

cd /cygdrive/c/Users/Tom/Desktop/

每次我发射Cygwin。


当前回答

最后一个答案是独立于Cygwin本身。

这使用的事实是,如果我在目录C:\\Cool,我调用命令C:\\Cygwin\\bin\\mintty.exe, mintty将自动打开当前目录,即C:\\Cool。

首先,您需要创建文件C:\\Cygwin\\silent_run。VBS,内容如下:

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

接下来,要安装这个答案,您将使用一个Windows注册表文件。为此,只需将以下文件的内容放入名为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"

现在,修复所有硬编码路径,即C:\\Cygwin到您的Cygwin安装的实际位置。

然后,只需打开文件C:\\Cygwin\\AddMinttyToContextMenu。Reg来安装新的注册表项,这样就完成了。

文件C:\\Cygwin\\AddMinttyToContextMenu。reg的工作原理是首先打开cmd.exe,切换到您所在的目录,然后调用C:\\Cygwin\\silent_run。vbs打开C:\\Cygwin\\bin\\mint .exe终端,用命令行选项-w max,即最大限度地打开它。

脚本C:\\Cygwin\\silent_run。需要vbs来打开C:\\Cygwin\\bin\\mint .exe终端而不保留我们打开的第一个cmd.exe,打开。

C:\\Cygwin\\AddMinttyToContextMenu的第一个条目。reg不使用C:\\Cygwin\\silent_run。因为默认情况下HKEY_CLASSES_ROOT\Directory\Background\shell键已经在当前目录中打开,那么,我们可以直接调用C:\\Cygwin\\bin\\mint .exe来让它开箱工作。

引用:

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)?

其他回答

当您安装Cygwin时(或者如果您已经安装了Cygwin,请再次下载并重新开始安装以运行更新),请确保您在“shell”类别下选择了chere包。

启动Cygwin后,打开Cygwin终端(以管理员身份)并键入命令:chere -i -t mintty -s bash。

现在在Windows右键上下文菜单中应该有“Bash Prompt Here”。

(mintty是Cygwin的默认终端。如果你不选择-t选项,你的“Bash Prompt Here”将使用与Windows命令提示符相同的终端,这可以防止水平调整大小。)

以管理员身份打开Cygwin终端

在powershell中(使用巧克力):

choco install cyg-get

在cygwin 将安装右键菜单

cyg-get install chere
chere -i

现在您可以在任何文件夹中单击右键并使用“Bash Prompt Here”。

基于@LindseyD的答案,我创建了一个简单的BAT文件,在当前目录中打开cygwin,这可能是有用的(对我来说是)。假设在PATH中有cygwin的bin目录。

FOR /F %%x IN ('sh -c pwd') DO bash -l -i -c 'cd %%x; exec bash'

我已经创建了批处理文件,并将其放到Cygwin的/bin目录中。开发此脚本的目的是允许安装/卸载注册表项,以便在Cygwin中打开选定的文件夹和驱动器。详情请参见http://with-love-from-siberia.blogspot.com/2013/12/cygwin-here.html。

更新:此解决方案与早期建议相同,但Windows注册表的所有操作都隐藏在脚本中。

执行安装命令

cyghere.bat /install

执行卸载命令

cyghere.bat /uninstall

可能是最简单的一个:

1)创建文件foo.reg

2)插入内容:

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)执行foo.reg

现在只需右键单击任何文件夹,单击打开mintty,它将在该文件夹中生成mintty。