Skip to content

Instantly share code, notes, and snippets.

@kennycyb
Last active June 9, 2025 05:48
Show Gist options
  • Select an option

  • Save kennycyb/c9e219d35165c7fabd9d67f76013b012 to your computer and use it in GitHub Desktop.

Select an option

Save kennycyb/c9e219d35165c7fabd9d67f76013b012 to your computer and use it in GitHub Desktop.
SSH to VM with Path Synchronization

sshex: SSH to VM with Path Synchronization

A simple Bash/Zsh function to SSH into a remote machine and automatically cd into a directory that mirrors your current local path, relative to your home directory.

Usage

  1. Save the function: Add this entire block to your shell's configuration file (e.g., ~/.bashrc, ~/.zshrc, or ~/.bash_profile).

  2. Source your config: After saving, apply the changes by running source ~/.bashrc (or your respective file), or simply open a new terminal session.

  3. Execute the command: From your local machine, navigate to a directory like /Users/user133/Projects/my-app. Then, run sshex <your_vm_user>@<your_vm_host>:

    cd ~/Projects/my-app
    sshex user@my-vm-ip

    This will SSH into user@my-vm-ip and attempt to change the directory to /home/user/Projects/my-app.

    Note: The script automatically handles the conversion of your local ~ (home directory) path to the remote ~ path.


The Script

# Function to SSH to a VM and switch to a similar path
#
# Usage: sshex <user>@<host>
# Example: sshex [email protected]
# Example: sshex [email protected]
sshex() {
    if [ -z "$1" ]; then
        echo "Usage: sshex <user>@<host>"
        echo "Example: sshex [email protected]"
        return 1
    fi

    # Get the current working directory on your local machine
    local_current_path=$(pwd)

    # Calculate the target path on the remote VM.
    # This assumes that your local home directory structure
    # should mirror the remote home directory structure.
    #
    # ${local_current_path/#$HOME/}: Removes the local home directory prefix (e.g., /Users/user133/)
    #                                from the current path.
    # ${target_path#/}: Removes any leading slash that might remain after the above substitution.
    #                   This makes the path relative to the VM's home directory.
    target_path="${local_current_path/#$HOME/}"
    target_path="${target_path#/}" # Remove leading slash (if present)

    echo "Attempting to SSH to '$1' and navigate to: ~/$target_path"

    # Execute the SSH command:
    # -t: Forces pseudo-terminal allocation, essential for interactive shells.
    # cd \"~/$target_path\": Changes directory on the VM.
    #                          '~/' ensures it's relative to the VM user's home directory.
    #                          Quotes are crucial for paths with spaces or special characters.
    # && exec bash -l: If 'cd' is successful, replace the SSH session with a new interactive login shell.
    # || exec bash -l: If 'cd' fails (e.g., directory doesn't exist on VM), fall back to a login shell
    #                  in the VM's default home directory.
    ssh -t "$1" "cd \"$target_path\" && exec bash -l || exec bash -l"
}

Important Notes

SSH Keys: For the best experience, ensure you have SSH keys set up for passwordless login to your VM. This makes the sshex command truly seamless.

Path Mapping: This script assumes your desired remote path is relative to your remote home directory (e.g., /home/youruser/). If your remote projects are in a different base path (e.g., /opt/projects), you'd need to modify the target_path logic slightly, but for mirroring local home directory structures, this works perfectly.

Directory Existence: If the exact target directory (~/path/to/my-app) doesn't exist on the VM, the cd command will fail, and the script will gracefully drop you into your VM's default home directory.

Shell Compatibility: This script is written for Bash/Zsh compatibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment