Ssh terminal with local files

Hello,
This is not Julia-specific. I have my files locally and want to continue to edit them locally. However, it would be convenient if my terminal could run on a remote machine via ssh, so that anything I run gets executed there.
Right now I copy and paste in another window outside of VSCode, which is not ideal. Being able to use alt+Enter would be nicer.
Thanks!

If both machines are Linux and the target machine can connect to your local machine then I would look at using sshfs. With sshfs you can mount a directory over ssh. So what you could do is in the VSCode terminal ssh to the target machine, run sshfs on the target machine to mount your source folder on the local machine. Then run julia from the mounted source directory. When you make changes and save the file locally you would just enter include("test.jl") into the remote Julia to load them again.

I don’t think you will be able to debug Julia or anything like that, but it should make running your changes remotely fairly easy.

1 Like

@pixel27 Thanks for the tip, but that’s not quite what I’m looking for. I want to be able to continue working on the code if I disconnect from the remote machine (which happens quite a bit).
Maybe I’ll have to continue pasting on the terminal, which is not very practical or elegant.
Thanks again!

This is possible with the julia-repl extension for emacs. You can basically set it up to start the repl via ssh but the file is local. If you are interested I can send you my setup.

1 Like

This is slightly tricky. In theory it’s fairly easy to implement (in fact, Juno supports this use case), but would require us implementing our own port forwarding and remote code execution story instead of re-using VSCode’s, which I’d very much like to avoid.
And then there’s of course a whole other can of worms as soon as your Julia code accesses the FS – what do you want include to do on the remote if you haven’t copied over your code first? That’s of course solvable for simple cases, but in general you’d need to run a program locally to figure out which files it accesses and then upload those.

Writing (or finding) an extension that just pastes your code into the currently active terminal might be a decent solution to automate your current workflow as far as possible though.

That sounds great! But then do you need to use emacs, or the REPL can still behave as usual? Please do share your settings and the specific extension. Thanks!

@pfitzseb Yep, pointing at files is tricky, but I could work around that for now. As I said, I don’t think this is Julia specific and I understand the Julia extension not accounting for this (I was hoping VS Code did). Maybe I should look into Juno. Thanks!

My extension for VSCode should do what you want. It is a language-generic setup for sending selected code from a file to a specified terminal. For your setup you probably want to use the following settings

"[julia]": {
  "terminal-polyglot.launchCommand": "ssh [hostname here]",
  "terminal-polyglot.runCommand": "include(\"%\")",
  "terminal-polyglot.changeDirectoryCommand": "cd(\"%\")",
  "terminal-polyglot.bracketedPasteMode": true,
  "termianl-polyglot.sendTextBlockCommand": "begin; %; end"
},

And then start julia once you’ve logged in to ssh.

That said, if you do this you’ll miss out on some of the cooler features of the julia extension for vscode: you don’t get inline plotting or inline results, but presumably you’re not getting that anyways right now the way you are just copying the code. (I personally use the extension daily, since I prefer its features over the inline plots and results).

3 Likes

@haberdashPI That does look like exactly what I need!
Currently, I use the VS features you mentioned while doing some of my testing (plotting results, checking code accuracy), while using a separate terminal and pasting code for testing some other stuff (mostly CUDA and performance). Does your extension allow for switching back and forward from the local and remote terminals?
Thanks a lot!

Yes, it does. There are two ways you could do that.

  1. Just use my extension for the remote session (and send code using Ctrl+Shift-Enter, or whatever shortcut you define), and use the julia extension for the local session (sending code the usual way for that extension, I think it’s Alt-Enter by default, but I don’t remember).

  2. Use my extension exclusively, using its support for multiple terminals: set the launch command to your shell (e.g. bash), open two terminals using the extension, and then manually start julia locally in one and remotely in the other.

The unique advantage of 1 is that you will get the cool features that julia-vscode has, the unique advantage of 2 is that you will have easy command to switch between the two terminals and have a given file remember which terminal it used most recently.

1 Like

@haberdashPI Ok, almost there: when I click Ctrl+Shift+Enter, a new terminal is launched, but the first command it uses is julia instead of what I set here:

"terminal-polyglot.launchCommand": "ssh myusername@hostname"

I can then go down to the terminal and manually type the ssh command and then launch Julia, but clearly it’s designed to do that automatically, so what am I doing wrong?
Thanks a lot! Very useful extension!

I’m not sure what is happening without more context. One guess would be that your setting is in the wrong place. If it helps, here’s a snapshot of my exact preference file, with only the relevant field included:

{
   // etc...
    "[julia]": {
        "terminal-polyglot.launchCommand": "tmux new-session -A -s '%' zsh -c \"julia -t auto --project=.\"",
        // etc...
    }
   // etc...
}     

Maybe it’s a bug?? Maybe just open an issue. At the very least, it’s probably a documentation issue, which would be good to fix too!

Did you double-check what commands that keybinding resolves to?

I’m changing the setting in the GUI:


And the keyboard shortcut does work. Also, using the other shortcuts (open new terminal and so on) also result in the powershell opening and trying to run julia.
Polyglot is version 0.4.2. VSCode 1.53.2.
Not sure what other info I could provide… ideas?
Thanks!

Oh, the GUI doesn’t work, but the JSON file does. Problem solved! Thanks a lot!

Yeah, language specific settings have no gui support. Didn’t realize that meant changes you make there just silently don’t work. I’ll update the readme to make that clearer.

You would be using emacs as an editor but the REPL behaves the same way as anywhere

(use-package julia-repl
:ensure t
:init (require 'julia-repl)
:config
 ; for remote repl:
 ; ssh -t host /path/to/julia

 (setq julia-repl-executable-records
 '(
 (default "julia")                  ; in the executable path
 (remote "ssh -t ME@MYIP /usr/bin/julia")))
)

(defun remotejl ()
(interactive)
(make-term "julia-ssh" "ssh" nil "-t" "ME@MYIP" "julia")
(funcall-interactively 'julia-repl-prompt-set-inferior-buffer-name-suffix '(ssh))
)

then M-x remotejl creates the remote REPL. Of course you can bind this to a key combination. I use evil for example

  (evil-define-key 'normal 'julia-mode-map (kbd "<leader>jr") 'remotejl)

@danielw2904 Thanks! I’ll have a look!

1 Like