How do i open a local terminal from a remote session in VScode? - visual-studio-code

When i open my workspace in VScode i go directly into my SSH work area, which is what i want. But i want to have a split terminal with access to both my SSH-work area and a terminal for my local computer, but i cant access the local one without making an explicitly new terminal from the dropdown window. And if i try to split the new local terminal i get the error "The terminal shell CWD "/Users/asd/work/" does not exist" as if its being looked for in my SSH work directory. Is there any way of specifying the default terminal to be on my local computer? If i do so in the terminal preferences in VScode i get the CWD-error on startup instead.

This should have been resolved with microsoft/vscode-remote-release issue 1479
Both of you don't have C:\Windows\System32 on your path, I don't understand why that is.
But I will change this to not rely on the PATH.
Even if this was for Windows originally, opening a local shell, (Mac or Windows) should now be possible while having a remote session.
Wtih ctrl+shift+p or cmd+shift+p(Command Palette), you have:
Terminal: Create New Integrated Terminal (local)
Bonus, with VSCode 1.53 (Jan. 2021), there is now:
Remote layout persistence
Terminal layout is restored on remote terminal reconnection. In the video below, the terminal layout is restored when reloading VS Code and reconnecting to a Windows Subsystem for Linux (WSL) remote instance.

Related

VSCode SSH auto-open terminal and run commands

I use VSCode with the Remote - SSH extension to connect to a remote dev enviroment.
During development on the remote machine, I use integrated terminal to view application logs. At this moment, I have to open new terminals and run commands manually, which is too much work for my lazy hands ;)
Questions are:
When I connect, I want VScode to open terminal tabs for me and run some commands (like tail ./logs/whatever). Is that possible?
If (1) is possible, can I also configure the terminals to open in VSCode Split Terminal mode?

Is it possible to open VSCode from Windows terminal while SSH'ed into a virtual machine?

I'm working on some tutorials and trying to do something, but no idea if it can be done. I am using windows terminal in Windows 10. I have an Ubuntu virtual machine running.
I'd like to:
Open Windows terminal
SSH into the Linux Virtual Machine
Type in the code . command and have it open a version of VS Code on my Windows PC that is working on the folder in the Ubuntu VM.
I tried install code locally and on the command line in the other machine, but it doesn't work. I am sure there are other ways to do this but wanted to explain how I was doing it to show I at least tried something. Thanks for your help.
It's not quite as easy as the code . technique you get with the Remote - WSL extension, but Microsoft also provides a Remote - SSH extension that can be used to directly access the remote machine (without requiring WSL in-the-middle).
Once the Remote - SSH extension is installed in VSCode (and it may have already been installed in an extension pack with the WSL extension), and you have installed an OpenSSH client in Windows, there are several ways to access files on the remote host through SSH:
From the Command Palette (Ctrl+Shift+P), type Remote SSH to filter on those commands. From Connect to Host, you can add a new configuration, etc.
From the Activity Bar on the left, select the Remote Explorer icon, then in the dropdown at the top, select SSH Targets. You can add hosts here through the + icon.
There's a direct shortcut to Open a Remote Window at the far left of the status bar. This will give you similar filter options on the Command Palette as above.
Once a host is configured, you can browse it just as (well, almost) if it were local, open files, edit, etc. The one thing you can't do (as far as I'm aware) is any type of sudo/su editing on files that you don't have permissions to directly.
Full details in the VSCode docs here.

Vscode (code.exe) command line arguments to immediately open a remote workspace folder over SSH

If I want to open a workspace on a regular local folder under C:\ I can just execute the command:
code.exe C:\the-local-workspace-folder
Is there any equivalent command for opening remote SSH workspaces?
I want to be able to use my keyboard launcher to open them, rather than needing to open them the fiddly way in the internal menus inside vscode.
So I need a regular system command to be able to do this.
I've looked through the command line arguments here: https://code.visualstudio.com/docs/editor/command-line - but can't find anything about remote workspaces at all.
I've also tried commands like:
code.exe username#hostname.example.com:/workspace-folder
code.exe username#hostname.example.com/workspace-folder
...but they don't work for this.
This looks like a recent fix, according to the issue tracking here. I've tested with my own settings, and this works:
"C:\Users\myusername\AppData\Local\Programs\Microsoft VS Code\Code.exe" \
--remote ssh-remote+myubuntumachine /home/myusername/myprojectdirectory
The myubuntumachine should be the name given in the Host myubuntumachine in the SSH config file on CTRL+SHIFT+P/Remote SSH: Open Configuration File...
(Actually, on my machine I don't have the machine name, but some sort of hash value, although either works.)
VSCode 1.63 (Nov. 2021) adds the option -n (issue 137529), to make sure a new window is opened for remote CLI:
The following options got added to the remote CLI:
-n to open a new window of the same remote as the current window
-n --remote=wsl+ubuntu to open a new window of a different remote
-n --remote=local to open a new local window
To get started, you need to:
Install an OpenSSH compatible SSH client if one is not already present.
Install Visual Studio Code or Visual Studio Code Insiders.
Install the Remote Development extension pack.
Read more : https://code.visualstudio.com/docs/remote/ssh
Installations extension :
code.exe --install-extension ms-vscode-remote.remote-ssh
Follow the step-by-step tutorial or if you have a simple SSH host setup, connect to it as follows:
Press F1 and run the Remote-SSH: Open SSH Host... command. Enter
your user and host/IP in the following format in the input box that
appears and press enter: user#host-or-ip or
user#domain#host-or-ip If prompted, enter your password (but we suggest setting up key based authentication). After you are connected,
use File > Open Folder to open a folder on the host.
You can press F1 to bring up the Command Palette and type in Remote-SSH for a full list of available commands.
command list
You can change the location by launching VS Code with the --extensions-dir command-line option.
Where are extensions installed?#
Extensions are installed in a per user extensions folder. Depending on your platform, the location is in the following folder:
Windows %USERPROFILE%.vscode\extensions
Linux ~/.vscode/extensions
macOS ~/.vscode/extensions
To run remote ssh and open the folder in command-line :
code.exe --remote ssh-remote+root#server.com <your-directory>

VS Code using Remote SSH - How to open "local" terminal?

I am connected to a remote server via SSH and using the Remote - SSH plugin by VS Code to do so. However, when opening up a terminal, the default terminal is the terminal of the remote server. I know this makes a lot of sense but there are times where I'd like to actually use the local terminal from VS Code as well.
Is there any way to do this or is the terminal limited to the host that VS Code's session is established?
If you can SSH back home, an easy way is to open a new terminal in VS Code and ssh home.
EDIT in fact, just search "terminal local" in the Command Palette. This feature exists out-of-the-box in VSCode, see : https://github.com/microsoft/vscode-remote-release/issues/730

How does VSCode's Remote Extension open files in my local editor through its internal terminal?

I use VSCode's remote development extension fairly regularly. I can use the terminal in VS Code as if it were on my own machine, and even the code command works correctly. That is, when I'm in a remote VSCode session, I can type code path/to/some/file and it will open another editor tab with that file. The terminal session and the file being opened are on the remote machine to which I've connected.
I have VSCode installed on the remote machine, and the code executable is in my PATH. So my question is, how is this functionality implemented behind the scenes? That is, how does VSCode know that when I type code path/to/some/file it should open that file into another editor tab on my machine instead of trying to fire up VSCode on the remote machine?
Literally seconds after I wrote the question I found the answer.
If I run which code in the terminal, it doesn't resolve to the usual VSCode executable, but instead it resolves to one located at $HOME/.vscode-server/bin/a5d1cc28bb5da32ec67e86cc50f84c67cc690321/bin/code.
If I echo $PATH I can see that $HOME/.config/bin and $HOME/.vscode-server/bin/a5d1cc28bb5da32ec67e86cc50f84c67cc690321/bin has been appended to the beginning of the PATH env var that my bash profile generates.
I assume this means that VSCode is executing bash with a different profile script that
Sources my usual bash profile
Creates the directories above and copies some helper programs into them
Modifies my path to include these directories
I also assume that the injected code executable is communicating with my local instance of VSCode in some way, instructing it to open the file in its editor.