5

I'm using using VSCode Remote SSH from my laptop (Linux) to work on projects that resides on a Linux host.

If I open an internal terminal in VSCode I can open files from the host by doing code some_file.txt. I frequently want to be able to do the same from terminals that are not originating from VSCode.

Is there anyway to open files in the VSCode-server while connected to a standard (Non VSCode internal) terminal?

Martin Wallgren
  • 503
  • 5
  • 11

2 Answers2

3

From another StackOverflow answer, I learned that if you open an integrated terminal and find the VSCODE_IPC_HOOK_CLI environment variable, and set it to the same value in the non-integrated terminal, then run code from the code server install directory ~/.vscode-server, it will work. That article didn't mention that you can have more than one install in the ~/.vscode-server directory. The current install can be extracted from the VSCODE_GIT_ASKPASS_MODE variable, it looks like this:

$ echo $VSCODE_GIT_ASKPASS_NODE
/home/<user>/.vscode-server/bin/054a9295330880ed74ceaedda236253b4f39a335/node

Just chop off the node and add bin, and so run something like

$ /home/<user>/.vscode-server/bin/054a9295330880ed74ceaedda236253b4f39a335/bin/code myfile.txt
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
  • Thank you, I figured this out by investigative the environment in the internal terminal. I have a small script that I source that looks up the correct paths for the latest opened vscode instance. I'll add it here once I'm at a computer. – Martin Wallgren May 15 '21 at 22:23
  • `/run/user/$(id -u)/vscode-ipc-*` might now be `/tmp/vscode-ipc-*`. It is for me on VSCode 1.64.1. – posita Feb 12 '22 at 02:08
-1

C. R. Oldham's answer covers the technical detail of why/how.

If you want a 1-liner that works as an alias in your ~/.bash_profile:

# Open file in most recently-connected remote VSCode session.
alias code=$'VSCODE_IPC_HOOK_CLI=/run/user/`id -u`/$(ls -lt /run/user/`id -u`/ | egrep \.sock$ | head -1 | awk \'END {print $NF}\') `ls -lt ~/.vscode-server/bin/** | fgrep bin/remote-cli/code | head -1 | awk \'END {print $NF}\'`'
Techrocket9
  • 2,026
  • 3
  • 22
  • 33
  • 1
    `ls -lt ~/.vscode-server/bin/** | fgrep bin/remote-cli/code` section of the statement above produces zero results. I would suggest this: `alias code="${HOME}/.vscode-server/bin/$(ls -t1 ${HOME}/.vscode-server/bin | head -n 1)/bin/remote-cli/code" ` – Brian Horakh Dec 24 '22 at 03:35