ipython up and down arrow strange behaviour

2019-04-07 17:00发布

问题:

In my installation of ipython I have this strange problem where I cannot reliably move through command history with up and down arrows... a lot of the time it just doesn't work (nothing happens on the key press). Also sometimes writing normal characters at the end of the command just doesn't work.

My system: Mac OSX Lion

I have readline installed...

thank you for the help! david

回答1:

Make sure you installed readline before ipython.

sudo pip uninstall ipython

sudo pip install readline ipython

(I know this question is a few months old, but for future reference)



回答2:

I had to install readline with easy_install readline and that fixed it. Using pip install readline did not work for me, and ipython gave a warning:

******************************************************************************
libedit detected - readline will not be well behaved, including but not limited to:
   * crashes on tab completion
   * incorrect history navigation
   * corrupting long-lines
   * failure to wrap or indent lines properly
It is highly recommended that you install readline, which is easy_installable:
     easy_install readline
Note that `pip install readline` generally DOES NOT WORK, because
it installs to site-packages, which come *after* lib-dynload in sys.path,
where readline is located.  It must be `easy_install readline`, or to a custom
location on your PYTHONPATH (even --user comes after lib-dyload).
******************************************************************************


回答3:

Following trouble in iPython and up-&-down arrows to access history, and browsing this post, a simple solution (turn off "Scroll lock") turned out to work for me.



回答4:

This is an intentional feature of IPython. If you type "abc" and then hit the up arrow, it's going to scroll only through lines that start with "abc". If you hit lift/right while you're scrolling, it triggers the same behavior. The entire contents of the current line are interpreted as your search prefix, any only lines starting with all that will show up on further up/down keypresses.

You can change this behavior in your PYTHONSTARTUP file. I have the following lines:

import readline
# Prevent ctrl-p/ctrl-n/Up/Down from doing prefix searching 
readline.parse_and_bind('"\\C-p": previous-history')
readline.parse_and_bind('"\\C-n": next-history')
readline.parse_and_bind('"\\e[A": previous-history')
readline.parse_and_bind('"\\e[B": next-history')

If you're curious, here are the bindings in IPython's source code that we're overriding.

Unrelated, but I also like to to override readline's default ctrl-w:

# Ctrl-W behavior more like Vim
readline.parse_and_bind('"\\C-w": backward-kill-word')