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

Something I love about VS Code is that when I am using a terminal in WSL, I can run code file.txt, and it will open that file with VS Code on my local using the WSL remote extension.

Is it possible to do a similar thing with SSH? I.e., if I am SSH'ed into a remote host, is it possible to set things up so that running code file.txt will open VS Code on my local machine, connected via the remote SSH extension to open that file?



Solution 1:[1]

You shouldn't have to do anything. VSCode automatically sets the path/PATH to the code in the path/PATH environment variable depending on your shell. See this response. You might be overwriting your path/PATH like I was. I was accidentally overwriting path in ~/.cshrc and PATH in ~/.bashrc and was running into the same issue. After fixing it, I can run code on the command line. which code returns the location of the command.

Until I spent time to figure it out, I was using the two methods mentioned below. Both of which worked for me in bash; you can modify it for your shell as you see fit. But really fix your path/PATH rather than using these methods.

  1. Adding location of code to the PATH in ~/.bashrc

    export PATH=${VSCODE_GIT_ASKPASS_NODE%/*}/bin:$PATH

OR

  1. Setting alias to code in ~/.bashrc

    alias code="${VSCODE_GIT_ASKPASS_NODE%/*}/bin/code"


More on path vs. PATH here and here

Solution 2:[2]

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 script=$(echo ~/.vscode-server/bin/*/bin/code(*oc[1]N))
if [[ -z ${script} ]]
then
    echo "VSCode remote script not found"
    exit 1
fi
local socket=$(echo /run/user/$UID/vscode-ipc-*.sock(=oc[1]N))
if [[ -z ${socket} ]]
then
    echo "VSCode IPC socket not found"
    exit 1
fi
export VSCODE_IPC_HOOK_CLI=${socket}
echo $script $@
${script} $@

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))

Solution 3:[3]

Yes, sort of. From a VSCode terminal run the command env | grep VSCODE_IPC_HOOK_CLI then copy-and-paste that line that line with export into your ssh terminal. After that, you should be able to run code from your ~/.vscode-server/bin/XXX/bin directory.

VSCode terminal

enter image description here

SSH terminal

enter image description here

Update:

You can to automate this with a .bashrc and .profile to place the IPC code into a temp file, and source that when you do your ssh login.

For example, this works for me...

Append this to ~/.bashrc

#
if [ "$VSCODE_IPC_HOOK_CLI" != "" ]; then
cat >$HOME/.vscode_env.sh <<EOF
#
if [ "\$VSCODE_IPC_HOOK_CLI" = "" ]; then
export VSCODE_IPC_HOOK_CLI="$VSCODE_IPC_HOOK_CLI"
alias code="${VSCODE_GIT_ASKPASS_NODE%/*}/bin/code"
fi
EOF
fi

And append this to your ~/.profile

[ -f $HOME/.vscode_env.sh ] && . $HOME/.vscode_env.sh

(There may be more elegant ways. And you still have to start at least 1 terminal in your remote VSCode session.)

Solution 4:[4]

Use the below commands to open a folder or a file on the remote terminal. Note: vscode-server must be already installed on the remote host (It would be, if you have already connected to it). Also the absolute path has to be specified for the file or folder. Use -n to launch in new window,-r to reuse same window.

code --folder-uri <absolute-path>
code --file-uri <absolute-path-file-name>

Example:

code -r --folder-uri /home/myscripts/src           
code -n --file-uri /home/myscripts/src/math/sample.py

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Praveen Lobo
Solution 2
Solution 3
Solution 4