Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a command to open a VSCode Remote SSH connection from the command line via an already open SSH connection #3324

Closed
jkoritzinsky opened this issue Jul 6, 2020 · 19 comments
Assignees
Labels
feature-request Request for new features or functionality *out-of-scope Posted issue is not in scope of VS Code ssh Issue in vscode-remote SSH

Comments

@jkoritzinsky
Copy link
Member

When using the VSCode Remote WSL features, I've come to appreciate and rely on the ability to open a folder or file in VSCode by using the code command in my WSL terminal and it starting up a VSCode on Windows -> VSCode Remote WSL window for the file or opening in a current window as if I had opened the file on my device directly.

With the move to work from home, I've recently moved to using the VSCode Remote SSH features extensively to remote into my box at work instead of using RDP. I've even set up the ability to SSH into my machine via custom terminal profiles. However, I've encountered one issue that regularly messes up my workflow. Whenever I run code . in an ssh shell, it pops open VSCode on the remote machine instead of opening on my local machine and connecting to my remote machine. I'd love if there was a way to open VSCode with a remote SSH connection from the command line, run either on the target machine or the host or within or outside of a VSCode integrated terminal.

@github-actions github-actions bot added the ssh Issue in vscode-remote SSH label Jul 6, 2020
@bamurtaugh bamurtaugh added the feature-request Request for new features or functionality label Nov 4, 2020
@bamurtaugh
Copy link
Member

I believe this is related? #3738

@roblourens roblourens added the *out-of-scope Posted issue is not in scope of VS Code label Nov 6, 2020
@Bacto
Copy link

Bacto commented May 9, 2021

Hi,

Is this something that can be re open as it has almost 20 upvotes yet?

The idea of running code <file> on the remote server (connected via SSH) that will open the file on my running local vscode would be really confortable! :)

@roblourens
Copy link
Member

roblourens commented May 13, 2021

Sorry, this isn't really practical for us to do. It does work in the integrated terminal when you already have a remote session open.

@jkoritzinsky
Copy link
Member Author

Is that also the case when the target device has VSCode (not the remote server) installed?

Last time I tried that (around when I initially opened this issue) it opened VSCode on the target device instead of opening another remote session on my device or opening the file in my current remote session.

@danielhoherd
Copy link

Since vscode can run entirely in a browser, would it be possible to implement this feature as starting vscode on the remote server and providing a URL and authentication information in the terminal to quickly launch a local browser that connects to the remote machine? Obviously this would require an IP route to the destination machine, but given sshuttle and ssh -L etc this might be feasible.

@will-sargent-eero
Copy link

will-sargent-eero commented Jul 20, 2021

This has been working for me from related issue:

code --folder-uri vscode-remote://ssh-remote+1.2.3.4/

@danielhoherd
Copy link

@will-sargent-eero that works for initiating a session from your workstation, not from the remote system you are already connected to.

@account-login
Copy link

ssh back to client works.

function rcode
    set -l tuple (string split ' ' -- $SSH_CONNECTION)
    set -l client $tuple[1]
    set -l server $tuple[3]
    for proj in $argv
        set -l proj (realpath $proj)
        ssh $client command code --folder-uri "vscode-remote://ssh-remote+$server/$proj" &
    end
end

@maelvls
Copy link

maelvls commented Nov 15, 2021

^ The above script works well if you are using fish shell.

For bash/zsh users:

rcode() {
    client=$(cut -d' ' -f1 <<<"$SSH_CONNECTION")
    server=$(cut -d' ' -f3 <<<"$SSH_CONNECTION")
    for proj in "$@"; do
        proj=$(realpath $proj)
        ssh $client /usr/local/bin/code --folder-uri "vscode-remote://ssh-remote+$server$proj"
    done
}
  • I replaced command code with /usr/local/bin/code due to the fact my client is macOS and the code shim is installed in /usr/local/bin which is not in the PATH for non-interactive shells.
  • I removed the background mode & since it I often get prompted the ssh key password and can't do that when in the background.

@Jack-Works
Copy link

Jack-Works commented Dec 24, 2021

Here is the PowerShell version for Windows users

Requirement:

  • psexec installed on the local machine
  • the local and the remote machine must be able to SSH to each other
  • In order to start the GUI program properly, it's required to hard-code your user name and login password
function c {
    # $env:VSCODE_IPC_HOOK_CLI indicates that we might be in the vscode remote ssh terminal
    # in case it handles the code call.
    if ($env:SSH_CONNECTION -and ($null -eq $env:VSCODE_IPC_HOOK_CLI)) {
        $arr = $env:SSH_CONNECTION -split " ";
        $local = $arr[0];
        $remote = $arr[2];

        if ([System.IO.Path]::IsPathRooted($args[0])) {
            $targetPath = $args[0];
        }
        else {
            $targetPath = Join-Path -Resolve $Pwd $args[0];
        }
        $targetPath = $targetPath.Replace("\", "/");
        $remoteURI = "vscode-remote://ssh-remote+$remote/$targetPath"
        Write-Host "Opening $remoteURI on local machine..."

        $user = "YOUR-USER-NAME-HERE"
        $password = "YOUR-PASSWORD-HERE"

        $supressCLIXML = '$ProgressPreference = "SilentlyContinue"'
        $supressSTDOUT = '> $null 2> $null'

        $startVSCode = "$supressCLIXML; Start-Process -WindowStyle Hidden code ""--folder-uri $remoteURI"""
        $startVSCode_encoded = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($startVSCode))
        $sshCommand = @"
            $supressCLIXML
            if ((tasklist | findstr Code).length) {
                echo "Running code --folder-uri $remoteURI"
                code ""--folder-uri $remoteURI""
            }
            else {
                psexec -d -u $user -p $password -i 1 powershell -NoProfile -EncodedCommand $startVSCode_encoded $supressSTDOUT
            }
"@
        ssh $local powershell -NoProfile -EncodedCommand $([Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($sshCommand)))
    }
    else {
        code $args
    }
}

@FreshlyBrewedCode
Copy link

I wrote a script that works cross platform via node and can be installed via npm:

npm i -g ssh-code

then you can run

ssh-code user@host:/path/to/folder

@starball5
Copy link

Is this feature mentioned in any documentation? Maybe I'm just missing it, but a quick ctrl+f doesn't show "code --folder-uri" in https://github.com/microsoft/vscode-remote-release/blob/main/README.md or https://code.visualstudio.com/docs/remote/ssh. I think some user-facing docs somewhere maybe in both those pages would be useful.

Related Stack Overflow question: How can I connect to a Remote SSH project from the command line?

@bamurtaugh
Copy link
Member

Thanks @starball5! We have documentation on this here: https://code.visualstudio.com/docs/remote/troubleshooting#_connect-to-a-remote-host-from-the-terminal.

We also welcome contributions to VS Code docs, so if you have additional ideas, please feel free to open a PR, and I'm happy to review: https://github.com/microsoft/vscode-docs.

@tylersmalley
Copy link

tylersmalley commented Mar 16, 2023

@bamurtaugh thanks so much! Is there currently a way to open a remote via the CLI without providing a path? Ideally, I am looking for a similar experience when you use "Connect to Host", allowing you to select the folder in the subsequent VSCode window after the connection has been made.

@bamurtaugh
Copy link
Member

I don't believe so - that sounds like the original request from this issue. Though if I'm misunderstanding and it's actually different, please let me know.

@liger1978
Copy link

liger1978 commented May 23, 2023

^ The above script works well if you are using fish shell.

For bash/zsh users:

rcode() {
    client=$(cut -d' ' -f1 <<<"$SSH_CONNECTION")
    server=$(cut -d' ' -f3 <<<"$SSH_CONNECTION")
    for proj in "$@"; do
        proj=$(realpath $proj)
        ssh $client /usr/local/bin/code --folder-uri "vscode-remote://ssh-remote+$server$proj"
    done
}
  • I replaced command code with /usr/local/bin/code due to the fact my client is macOS and the code shim is installed in /usr/local/bin which is not in the PATH for non-interactive shells.
  • I removed the background mode & since it I often get prompted the ssh key password and can't do that when in the background.

I found systemd-run more effective:

rcode() {
    client=$(cut -d' ' -f1 <<<"$SSH_CONNECTION")
    server=$(cut -d' ' -f3 <<<"$SSH_CONNECTION")
    for proj in "$@"; do
        proj=$(realpath "$proj")
        ssh $client systemd-run --user --property=Type=forking /usr/local/bin/code --folder-uri="vscode-remote://ssh-remote+$server\"$proj\""
    done
}

@padcom
Copy link

padcom commented Jul 25, 2023

@FreshlyBrewedCode Thank you a 1000x! It's easy, it's what I need, it's what should be included in vscode from the beginning.

@cmellazchy
Copy link

cmellazchy commented Aug 23, 2024

@bamurtaugh thanks so much! Is there currently a way to open a remote via the CLI without providing a path? Ideally, I am looking for a similar experience when you use "Connect to Host", allowing you to select the folder in the subsequent VSCode window after the connection has been made.

Adding this for anyone who finds your comment. You can now achieve this behaviour by doing the following:

code --remote ssh-remote+<remote_server_here>

This is documented here. Lose the path argument and it behaves like the connect to host UI option, not selecting the root by default.

@stuarteberg
Copy link

iTerm2 users who end up here my be interested in the following StackOverflow discussion, in which it is explained how to use iTerm2 to set up a command you can execute on the remote machine which triggers code --remote ... on your local machine, thus opening VSCode locally but viewing a remote file.

https://stackoverflow.com/questions/61699447/iterm2-how-can-i-trigger-a-local-command-from-a-remote-session/61758288#61758288

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request for new features or functionality *out-of-scope Posted issue is not in scope of VS Code ssh Issue in vscode-remote SSH
Projects
None yet
Development

No branches or pull requests

17 participants