1

I'm using VSCode and the official remote-ssh extension.

I would like to be able to write code /path/to/file in an ssh terminal or in the vscode integrated terminal in a remote window in order to open a file/folder in vscode remote.

I am aware that I can use code --folder-uri=vscode-remote://ssh-remote+ADDRESS/path/to/file from the local machine's terminal, but I want to be able to run a command from within the integrated vscode terminal and any other terminal session where I've ssh'd into the remote machine)

Currently, if I run code from a remote terminal it opens up a new vscode window on the remote machine.

To achieve this goal, in the past I've used the following alias on the remote machine:

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

Which looks for the code executable in ~/.vscode-server/bin/<COMMIT_ID>/bin before defaulting to the local /bin/code.

I got that alias from this related stackoverflow question.

However, this doesn't seem to work right now.

Upon closer inspection, it appears that there is no code executable in the vscode-server directory.

available files in code-serverk

How can I fix this?

Both machines are running MacOS and visual studio code version f80445acd5a3dadef24aa209168452a3d97cc326, if that's relevant.

2 Answers 2

1

I also wanted to be able to run code from the integrated terminal when running VSCode with the "remote ssh" extension. In my case, the "remote" is a Linux box (named "aorus" below), and I want to use VSCode from a laptop running macOS (named "mbp").

As for you, I used to use the VSCODE_GIT_ASKPASS_NODE trick. Recently, I had to change the alias since code (or code-insiders in my case) wasn't available in bin/ anymore. It seems it has been moved to bin/remote-cli. The correct alias (tested with vscode 1.64.2):

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

If you also want this to work from other ssh sessions (not just inside the integrated terminal), you can create a short script that I called coder (r for "remote") which I have in ~/bin on my remote ("aorus"). Note that you need to be able to reach the local machine from your remote (I do that with Tailscale). The script looks like this:

#! /bin/bash

set -ex
remotehost=$(hostname)
localhost=mbp

cmd="code"

while [ $# -gt 0 ]; do
    if [ -f "$1" ]; then
        cmd+=" --file-uri \"vscode-remote://ssh-remote+$remotehost$(readlink -f "$1")\""
    elif [ -d "$1" ]; then
        cmd+=" --folder-uri \"vscode-remote://ssh-remote+$remotehost$(readlink -f "$1")\""
    else
        cmd+=" $1"
    fi
    shift
done

exec ssh $localhost -q -t -x "exec bash -l -c '$cmd'"

On my Mac, when running VSCode connected remotely to my Linux box, I can type this in the integrated terminal to open the file main.go present on my remote Linux box:

coder main.go

The reason I have to wrap code in bash -l is due to the fact that ssh, by default, runs in a non-login shell, which means that the ~/.bashrc on my Mac isn't picked up, meaning code isn't in the PATH. The error message looks like this:

bash:1: command not found: code

Another note: there is a shorter syntax documented here:

ssh -q -t -x mbp bash -l -c "code --remote=ssh-remote+aorus main.go"

I don't use this syntax is because this method isn't able to know whether you are opening just a single file (which should be open in the most recent VSCode remote session) or a folder (which should be open as a new VSCode remote session).

Finally, if you are using VSCode Insiders, you can create a symlink so that the command code works on your local machine (in my case, on my Mac):

sudo ln -sf /usr/local/bin/code-insiders /usr/local/bin/code
Sign up to request clarification or add additional context in comments.

5 Comments

how can I adapt your coder script for a macOS host? I get this error: readlink: illegal option -- f usage: readlink [-n] [file ...]
Ah, right, readlink -f only works with the readlink that comes with GNU coreutils. I'll think of something.
I ended up using brew install coreutils to install coreutils on the macOS host and then substituted readlink with greadlink and all worked fine. However, I'm still at a loss why I can't just figure out a solution that would work with the code command... as that worked before and I'm not sure what's stopping it from working now.
I just found out that macOS's readlink does exactly the same as GNU's readlink -f. Regarding code from within the integrated terminal, it should still work, no need to use the coder script above. From the integrated terminal, if you run echo $PATH | tr -s : $'\n' | grep vscode you will see that another code, specific to the remote session. Its path looks like this: ~/.vscode-server/bin/<somehash>/bin/remote-cli/code Unfortunately, this method doesn't work outside of the integrated terminal, which is why I use the above coder script.
Interesting... because I had a solution that worked outside of the integrated terminal before as well... if only I had a time machine!
1

As already explained by maelvls the path has been changed. But if you use it outside integrated terminal you will got message

Command is only available in WSL or inside a Visual Studio Code terminal

To avoid this you need to export VSCODE_IPC_HOOK_CLI in your .bashrc .

Use this script in your .bashrc

export VSCODE_IPC_HOOK_CLI=`ls -t /run/user/1012/vscode-ipc-* | head -n1`
alias code="~/.vscode-server/bin/*/bin/remote-cli/code"

If you want to open your file in your current visual studio use -r option.

code -r tes.txt

Note : I can't call VSCODE_GIT_ASKPASS_NODE so I use full path, it is working well I don't know if VSCODE_IPC_HOOK_CLI will show in different location, just check it in your integrated terminal visual studio code

tested on remote server Centos 7 local macOS Monterey version 12.2 Visual Studio Code Version: 1.64.2 (Universal) Commit: f80445acd5a3dadef24aa209168452a3d97cc326 extension : remote-ssh

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.