Is it possible to use the “code” command in SSH’ed terminal to open VS Code on local machine with SSH extension?

I found much better & simple answer thanks to this post.

Simply create new script file named code with below contents & put file under any folder from $PATH. (echo $PATH to see what folders you can use)

#! /usr/bin/env zsh

local max_retry=10

for i in {1..$max_retry}
do
    local script=$(echo ~/.vscode-server/bin/*/bin/remote-cli/code(*oc[$i]N))
    if [[ -z ${script} ]]
    then
        echo "VSCode remote script not found"
        exit 1
    fi
    local socket=$(echo /run/user/$UID/vscode-ipc-*.sock(=oc[$i]N))
    if [[ -z ${socket} ]]
    then
        echo "VSCode IPC socket not found"
        exit 1
    fi
    export VSCODE_IPC_HOOK_CLI=${socket}
    ${script} $@ > /dev/null 2>&1
    if [ "$?" -eq "0" ]; then
        exit 0
    fi
done

echo "Failed to find valid VS Code window"

Bash version

#! /bin/bash
                                                                             
max_retry=10                                                                 
                                                                             
for i in $(seq 1 $max_retry)                                                 
do                                                                           
    recent_folder=$(ls ~/.vscode-server/bin/ -t | head -n$i | tail -1)       
    script=$(echo ~/.vscode-server/bin/$recent_folder/bin/remote-cli/code)   
    if [[ -z ${script} ]]                                                    
    then                                                                     
        echo "VSCode remote script not found"                                
        exit 1                                                               
    fi                                                                       
    socket=$(ls /run/user/$UID/vscode-ipc-* -t | head -n$i | tail -1)        
    if [[ -z ${socket} ]]                                                    
    then                                                                     
        echo "VSCode IPC socket not found"                                   
        exit 1                                                               
    fi                                                                       
    export VSCODE_IPC_HOOK_CLI=${socket}                                     
    ${script} $@ 2>/dev/null                                                             
    if [ "$?" -eq "0" ]; then                                                
        exit 0                                                               
    fi                                                                       
done                                                                         
                                                                             
echo "Failed to find valid VS Code window"

Update

Above script doesn’t work with recent updates. I had to change first line to

local script=$(echo ~/.vscode-server/bin/*/bin/remote-cli/code(*oc[1]N))

Update2

Original script may fail if recently opened ssh window is closed, yet there is another SSHed window open. I have enhanced the script to enable retrying the command with recent N(default 10) windows.

Leave a Comment