Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 967
jediahkatz Avatar asked Jun 04 '20 17:06

jediahkatz


3 Answers

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

like image 132
Praveen Lobo Avatar answered Nov 15 '22 07:11

Praveen Lobo


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))
like image 20
Lazy Ren Avatar answered Nov 15 '22 07:11

Lazy Ren


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

like image 26
Daniel Avatar answered Nov 15 '22 08:11

Daniel