36

I just upgraded to the latest version of IPython and now something is wrong when using it in emacs.

When using it in a regular terminal it works fine (colors, tab-completion, etc.), however either in M-x eshell or M-x shell, instead of outputing In [1]:, it actually outputs [?12l[?25h and Out[1] outputs to [J[?7h[?12l[?25h[?2004l[?7hOut[1]:.

Maybe it's some feature of the terminal not available on emacs used by IPython, but I'd like to know which one it is.

Terminal.app:

In [1]: 2+2
Out[1]: 4

Emacs

[?12l[?25h2+2
[J[?7h[?12l[?25h[?2004l[?7hOut[1]: 4
  • Did you open a bug upstream? I couldn't find anything, but it's also a bit hard to search ATM with Gmane being reworked. – rasmus Sep 9 '16 at 14:09
  • 2
    For reference, upstream bug is #25306 – npostavs Feb 24 '17 at 18:47
41

IPython 5 has a new terminal interface, which is not compatible with Emacs' inferior shells. To fix it, add the --simple-prompt flag to the IPython command:

(setq python-shell-interpreter "ipython"
    python-shell-interpreter-args "--simple-prompt -i")

From the documentation:

The new interface is not compatible with Emacs ‘inferior-shell’ feature. To continue using this, add the --simple-prompt flag to the command Emacs runs. This flag disables most IPython features, relying on Emacs to provide things like tab completion.

Reference: http://ipython.readthedocs.io/en/stable/whatsnew/version5.html#id1

  • 6
    Yes, that's what I did at first, however the lack of multiline support (most important for function definitions) made me revert back to ipython 4. It should be noted though that ipython 5 works fine with M-x term. – Literal Jul 12 '16 at 21:55
  • 3
    Setting this flag is still very useful with Elpy, though—through the variable python-shell-interpreter-args. If you haven't tried Elpy, you should check it out. It allows you to write your function definitions in a separate buffer and easily pass them to the interpreter with just one command. In this case, you can make use of all of Emacs' editing commands and get an interactive session—best of both worlds. – Ricardo Lima Jul 12 '16 at 23:58
  • 1
    @Literal the reason why is also written on the link: they changed from using readline to prompt_toolkit on IPython 5. prompt_toolkit is not compatible with Emacs shells, which work differently from term: as far as I know, shells run dumb terminals with the purpose of working more like a standard Emacs buffer, whereas term runs an actual full terminal emulator. It seems shells are compatible with readline but not with prompt_toolkit, which would explain the prompt showing the escape sequences. Since term runs a full terminal, it correctly interprets the escape sequences. – Ricardo Lima Jul 17 '16 at 2:45
  • 1
    I didn't accept it because I would have liked to retain the other features, perhaps using another shell mode with more features. But I guess I'll have to live with it. – Literal Jul 17 '16 at 3:42
  • 1
    @CodyChan It disables IPython features relating to the UI, like completion, colors, etc. It keeps things like %magic commands, input/output history, !shell commands, ?help commands, etc. – jpkotta May 16 '17 at 20:06
0

There's a way to open IPython in emacs shell without using --simple-prompt

1. pip install rlipython

2. Open IPython in terminal

3. Run import rlipython; rlipython.install() 

After this, you should be able to open ipython directly in emacs shell without experiencing the display problem.

There's one more issue in my case though: For some reason, even after display became correct, I still couldn't directly copy-paste some multi-line class definitions into ipython in emacs shell. My current solution is to use the Ipython %paste command. If you have better approaches, please let me know.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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