Easy WSL Windows path switching alias

July 22, 2025 0 By addshore

I have been primarily developing on WSL for some years now, and still love the combination in terms of all around flexibility.

When primarily working on Linux based or focused applications, everything is lovely!

However, I’m spending more time straying into the land of hardware, USB devices, and custom IDEs and debug interfaces that are just far easier to set up on Windows.

In order to keep my git workflows entirely in one place, though, I make use of the /mnt/c mounts that are available in order to keep my git commands in WSL bash.

Navigating to code in the WSL file tree is easy! Everything is in my home dir in a directory called dev that is in turn split up by project and or code location.

  • dev
    • github
      • addshore
        • some-repo
      • some-user
        • repo-2
    • gerrit
      • mediawiki
        • core
    • lb
      • io
        • project-1
        • project-2

I mirror this file system structure on my Windows drive for projects that need it, but would normally run a bunch of cd commands to get there.

However, I now have a neat little switching alias, given that the directory structure is the same in both places :)

switch() {
    local base_path="/mnt/c/Users/$USER/dev"
    local home_path="/home/$USER/dev"
    
    if [[ "$PWD" == "$base_path"* ]]; then
        # Switch from Windows path to Linux path
        local target="${home_path}${PWD#$base_path}"
        cd "$target" || echo "Directory does not exist: $target"
    elif [[ "$PWD" == "$home_path"* ]]; then
        # Switch from Linux path to Windows path
        local target="${base_path}${PWD#$home_path}"
        cd "$target" || echo "Directory does not exist: $target"
    else
        echo "You are not in a switchable directory."
    fi
}
Code language: Bash (bash)

Just add this to your bash aliases file, or bash rc file, reload it and you’re all good!

Let me know if you like the idea ;)

EDIT 1 day later

Let’s add directory creation too, in case the other side doesn’t yet exist!

switch() {
    local base_path="/mnt/c/Users/$USER/dev"
    local home_path="/home/$USER/dev"
    local force_create=false

    [[ "$1" == "--create" ]] && force_create=true

    if [[ "$PWD" == "$base_path"* ]]; then
        local target="${home_path}${PWD#$base_path}"
    elif [[ "$PWD" == "$home_path"* ]]; then
        local target="${base_path}${PWD#$home_path}"
    else
        echo "You are not in a switchable directory."
        return
    fi

    if [[ -d "$target" ]]; then
        cd "$target"
    else
        if $force_create; then
            mkdir -p "$target" && cd "$target"
        else
            read -rp "Directory does not exist: $target. Create it? (y/n) " answer
            [[ "$answer" == "y" ]] && mkdir -p "$target" && cd "$target"
        fi
    fi
}
Code language: Bash (bash)