Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 289
octopusgrabbus Avatar asked Jul 25 '12 18:07

octopusgrabbus


People also ask

What does the up arrow key do at the CLI?

To access previous commands, simply press the up arrow. This will give you the last command you typed in. Pressing it again will produce the command before that, and so on.

What is command line interpreter in Python?

A command line interpreter allows the user to interact with a program using commands in the form of text lines. It was frequently used till the 1970's. However, in modern times many command line interpreters are replaced by graphical user interfaces and menu-driven interfaces.


2 Answers

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.

like image 69
phant0m Avatar answered Oct 03 '22 19:10

phant0m


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. C-P moves one line up (back) in the history buffer, C-N 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. C-R starts an incremental reverse search; C-S starts a forward search.

like image 24
JAB Avatar answered Oct 03 '22 19:10

JAB