简体   繁体   中英

Why do up and down arrow commands not work in the Python command line interpreter?

I am using a VT100 terminal emulator on Linux. In bash, up and down arrows scroll through the last commands executed; they work as expected.

Previous (up arrow) and next (down arrow) commands are not interpreted in the Python command line interpreter. What kind of key mappings do I need to make this work?

Thank you.

By default, the keymappings are:

  • older: alt-p
  • more recent: alt-n

You can change it in Options -> Configure IDLE -> Keys -> "history-previous" and "history-next" respectively.

I think I've found the answer, assuming you have the GNU Readline library. (This does mean I was partially wrong about the base implementation using a Unix-style interface, as it only does that when GNU Readline [or a port, I guess] isn't available.)

http://docs.python.org/tutorial/interactive.html#history-substitution

History substitution works as follows. All non-empty input lines issued are saved in a history buffer, and when a new prompt is given you are positioned on a new line at the bottom of this buffer. CP moves one line up (back) in the history buffer, CN moves one down. Any line in the history buffer can be edited; an asterisk appears in front of the prompt to mark a line as modified. Pressing the Return key passes the current line to the interpreter. CR starts an incremental reverse search; CS starts a forward search.

You probably need the readline package:

pip install readline

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM