Open Cygwin at a specific folder

后端 未结 29 2443
悲哀的现实
悲哀的现实 2020-12-12 08:49

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/

相关标签:
29条回答
  • 2020-12-12 09:32

    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"
    
    0 讨论(0)
  • 2020-12-12 09:32

    You can just open cygwin terminal and write: cd and after drag n drop the folder you want end enter!

    0 讨论(0)
  • 2020-12-12 09:33

    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'"
    
    
    0 讨论(0)
  • 2020-12-12 09:33

    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'
    
    0 讨论(0)
  • 2020-12-12 09:35

    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:

    1. How add context menu item to Windows Explorer for folders
    2. VBScript pass commandline argument in paths with spaces
    3. How to pass a command with spaces and quotes as a single parameter to CScript?
    4. calling vbscript from another vbscript file passing arguments
    5. https://ss64.com/vb/syntax-args.html
    6. Check if string contains space
    7. Running command line silently with VbScript and getting output?
    8. http://www.vbsedit.com/html/6f28899c-d653-4555-8a59-49640b0e32ea.asp
    9. https://superuser.com/questions/62525/run-a-batch-file-in-a-completely-hidden-way
    10. How to run Batch script received as argument on VBscript?
    11. Can I pass an argument to a VBScript (vbs file launched with cscript)?
    0 讨论(0)
  • 2020-12-12 09:36

    As two7s_clash said you first need to install chere package and setup mintty:

    1. Open Cygwin terminal as administrator
    2. apt-cyg install chere
    3. 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:

    1. Open windows command prompt
    2. Navigate (cd) to custom directory
    3. 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:

    1. Tools -> Favorite Tools -> Favorite tools edit... (Ctrl + Shift + Y)
    2. 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:

    1. Tools -> Define keyboard shortcuts
    2. Scroll down to "Favorite tool 01" (or "Favorite tool N")
    3. Assign new shortcut key: I use Ctrl + Shift + T

    Great reference: MinTTY Wiki, article Tips: Starting in a particular directory

    0 讨论(0)
提交回复
热议问题