3

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.

  • 1
    Emacs Speaks Statistics appears to have support for Julia. – Dan Dec 11 '15 at 11:52
  • It does, but as noted in the question, the interior shell that comes with it isn't fully functional. – Zorgoth Dec 11 '15 at 15:01
5

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.

  • Inspiration for using screen: reddit.com/r/emacs/comments/3uj1tz/… – Zorgoth Dec 11 '15 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 Dec 11 '15 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 Dec 11 '15 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 Dec 21 '15 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 Dec 21 '15 at 18:00
0

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.

  • 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 Dec 11 '15 at 5:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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