Windows – Use msysgit/“Git for Windows” to navigate Windows shortcuts

bashmsysgitwindows

I use msysgit on Windows to use git, but I often want to navigate through a Windows-style *.lnk shortcut. I typically manage my file structure through Windows' explorer, so using a different type of shortcut (such as creating hard or soft link in git) isn't feasible. How would I navigate through this type of shortcut?

For example:

PCUser@PCName ~
$ cd Desktop

PCUser@PCName ~/Desktop
$ ls
Scripts.lnk

PCUser@PCName ~/Desktop
$ cd Scripts.lnk
sh.exe": cd: Scripts.lnk: Not a directory

Is it possible to change this behavior, so that instead of getting an error, it just goes to the location of the directory? Alternatively, is there a command to get the path in a *.lnk file?

EDIT: I've heard that the inverse of this exists for cygwin, allowing you to create a symlink which works with explorer.

Best Answer

*ahem*

First, compile the following AutoHotkey script:

FileGetShortcut, %1%, shortcut_target
FileAppend, %shortcut_target%, *
ExitApp

Place the .EXE file in a %PATH% directory. I named mine follow.exe.

Now, you can effectively follow a Windows .LNK file in your working directory by using the following syntax:

cd `follow Shortcut.lnk`

where Shortcut.lnk's target is a valid directory.


Demonstration:

shortcut

git bash


Once you've set up your follow.exe, you can add the following shell function to your ~/.bashrc file to simplify the syntax even further. Thanks, Daniel!

function cd
{
    if [[ ".lnk" = "${1:(-4)}" && -f "$1" ]] ;
        then builtin cd "$( follow "$1" )" ;
    else builtin cd "$1" ;
    fi
}

Now, you can follow .LNK files with just cd!

cd Shortcut.lnk

Demonstration:

cd

Related Question