4

Some people like me prefer to keep as much as possible in one emacs window (it saves screen retail), so want to run Julia as an inferior shell in emacs. The "run-julia" command that comes with the emacs Julia packages is inferior to running Julia in a terminal emulator; most obviously, ";" for shell commands and "?" for help don't work.

So the question becomes: How can you get a fully functional Julia REPL in emacs?

Note that it doesn't work to run julia directly in term because as anyone who tries this will see, there are crippling bugs that make it unusable. Running it in any other shell gives you the same limited features as run-julia.

3
  • 1
    Emacs Speaks Statistics appears to have support for Julia.
    – Dan
    Commented Dec 11, 2015 at 11:52
  • It does, but as noted in the question, the interior shell that comes with it isn't fully functional.
    – Zorgoth
    Commented Dec 11, 2015 at 15:01
  • I'm working in integrating the julia-vterm and/or julia-snail with the default julia-repl command. So I get one of these functional repls. Both seem promissing. But, I haven't finished implementing it yet.
    – BuddhiLW
    Commented Dec 18, 2020 at 14:17

2 Answers 2

6

There is a way! The problem with using term is that term doesn't understand some of the escape sequences Julia sends, which causes term to put a bunch of extra prompts on the screen that make it unusable.

This method should also work for more or less any REPL or terminal application you could want to run.

There is a ridiculously useful terminal application called screen that can come to rescue! While my favorite use of screen is to create sessions detachable from a terminal or ssh session (meaning that you can ssh into a computer, run a screen session, detach it, close the ssh connection, and your process will keep running), it also happens to solve our problem here by translating julia's messages into a language term can understand.

Install screen (e.g. "sudo apt-get install screen" in Ubuntu)

Then run screen inside term, and run julia inside screen. Observe: It works perfectly! You can run "screen julia" to make a screen session immediately open julia.

Here's some code you can add to your .emacs file to create a command (julia-repl) to launch a julia in a screen in a term and bind it to a key combination ("C-x j" - you can of course edit it to be whatever combination you prefer):

(defun julia-repl () "Runs Julia in a screen session in a `term' buffer." (interactive) (require 'term) (let ((termbuf (apply 'make-term "Julia REPL" "screen" nil (split-string-and-unquote "julia")))) (set-buffer termbuf) (term-mode) (term-char-mode) (switch-to-buffer termbuf))) (global-set-key (kbd "C-x j") 'julia-repl)


Here is some more useful code - I'm using Google Drive links because otherwise it would be really long:

I strongly recommend the following to make C-x the default escape char for giving emacs commands, rather that C-c:

(setq term-escape-char [24])

This emulates the ESS command Michael mentioned, except that it doesn't load variables (you can presumably do that pretty straightforwardly from the REPL):

https://drive.google.com/file/d/0B7M8PPMhKHOLeWU2SVF4NjZKRVU/view?usp=sharing

(there was a bug in the original version I uploaded, but it is now fixed)

This mode allows one to enter a sort of shell mode within the REPL by automatically sending semicolons. Default keybinding set to C-x ;. You exit the mode by pressing C-x ; again or by hitting backspace at the prompt.

https://drive.google.com/file/d/0B7M8PPMhKHOLTWFfRmtTUXUtN2c/view?usp=sharing

Note: The (setq term-escape char [24]) at the beginning causes C-x to become the escape key instead of C-c. I think that's much better to use, but if you don't want it, don't set it, and use a different hotkey for this mode.

7
  • Inspiration for using screen: reddit.com/r/emacs/comments/3uj1tz/…
    – Zorgoth
    Commented Dec 11, 2015 at 5:18
  • Note: if you need to set environment variables, you will have to override the julia command or make a new command that runs julia with those specific environment variables and pass that to screen instead of "julia." This is very likely to be necessary in order to use the PyPlot package. Since this answer is not that easy to find, I'll mention that on my computer, I need to set the variable PYTHON to go to my anaconda python and LD_PRELOAD to include ".../anaconda3/lib/libopenblas.so" There are other people who need other things in LD_PRELOAD. It depends on your setup.
    – Zorgoth
    Commented Dec 11, 2015 at 5:32
  • Also, you may want to read up on how term works if you don't already know. It has a char-mode and a line-mode and in char-mode doesn't take regular emacs commands except via an escape sequence.
    – Zorgoth
    Commented Dec 11, 2015 at 5:38
  • Thanks a lot for the screen trick and the custom code! It works perfectly except one thing: When I send commands from an *.jl file via ess-eval-line-and-step (bound to C-c C-n) to the created *julia* buffer, the command is shown twice instead of only once before the actual response is shown. Do you also observe this behaviour? Any idea how to fix this? Thanks!
    – Michael
    Commented Dec 21, 2015 at 12:17
  • I don't know exactly; I don't have that function. I'm not surprised that it doesn't really know how to interact with ESS.
    – Zorgoth
    Commented Dec 21, 2015 at 18:00
1

An (inferior) alternative is to use an IJulia console. This is what I had been doing before I learned about the screen trick.

Instead of running "screen julia" in ansi-term, you'd have to run something like "ipython3 console --kernel=julia-0.4" (replace 0.4 with the appropriate number for the Julia version on your computer).

If IJulia console ever gets cool magic like IPython has, there might be a reason to do this. Right now, I can't think of a single reason to use an IJulia console over the standard Julia REPL, while there are a number of reasons the standard REPL is better than the IJulia console. IJulia primarily exists for the sake of the Notebook.

1
  • A function for calling IJulia console (auto-adjusting to make sure you get the right version) is in this thread, which also is the source for the function in my primary answer: emacs.stackexchange.com/questions/18672/…
    – Zorgoth
    Commented Dec 11, 2015 at 5:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.