Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ipython: pressing 'esc' key breaks readline

In ipython, if I press 'esc' followed by 'enter' (and possibly other characters?), readline breaks. I can no longer search through command history using the 'up' key, and some commands (e.g., control-K) fail.

Is there a way to reset readline within an ipython session? What is going on when I press these keys?

like image 627
keflavich Avatar asked Apr 21 '26 19:04

keflavich


2 Answers

The poster's suggested answer doesn't seem to work for me in iPython 0.12+. I can run:

get_ipython().init_readline()

but that doesn't seem to help.

However I noticed that I sometimes see similar problems in my iPython sessions. It appears that I had inadvertently switched from the default Emacs readline editing mode to vi-mode(vim-mode). According to the the readline docs to switch between them you are supposed to be able to use the M-C-j key combination but that only seems to allow me to switch to vi-mode. To switch back to Emacs mode one can use C-e but that didn't appear to work for me - I had to instead do M-C-e - on my Mac (where ESC is used as the 'Meta' key) it is: ESC+CTRL+e

The contents of my ~/.inputrc is as follows:

set meta-flag on
set input-meta on
set convert-meta off
set output-meta on
like image 177
Pierz Avatar answered Apr 24 '26 07:04

Pierz


Got impatient. Solution is:

IPython.InteractiveShell.init_readline(get_ipython())

Looks like this might be a known bug too: http://www.catonmat.net/blog/bash-vi-editing-mode-cheat-sheet/

like image 20
keflavich Avatar answered Apr 24 '26 08:04

keflavich