13

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.

octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
  • Technically, up-arrow and down-arrow are interpreted just fine; the issue is that, as IDLE uses a textbox/textfield-type window to display its data, pressing "up" and "down" just moves the cursor up or down in the window. Don't treat IDLE like a terminal window. – JAB Jul 25 '12 at 18:43
  • @JAB Noted, but how do I treat it to get the last command? – octopusgrabbus Jul 25 '12 at 18:46
  • I've removed IDLE from the title and post, because it is not clear to me there is another term for Python command line interpreter. – octopusgrabbus Jul 25 '12 at 19:03
  • In that case, the command-line interpreter normally does support the usage of arrow keys for command completion, so check whatever customization you've performed if any to make sure you aren't overriding such functionality somehow. – JAB Jul 25 '12 at 19:09
  • @JAB but that's what I'm asking. What kind of terminal does the command line interpreter expect? – octopusgrabbus Jul 25 '12 at 19:10
  • you should have something like "python (command line)" which will open a terminal with a python interactive session. and I think the term you're looking for is "python shell" – Ryan Haining Jul 25 '12 at 19:12
  • What Python implementation are you using? CPython, the default/most common one, does use a Unix-style interface, but other implementations may not. http://docs.python.org/using/cmdline.html#interface-options – JAB Jul 25 '12 at 19:14
  • I'm on Linux using default Python 2.7 that I downloaded from the official Python site, so it must be CPython. – octopusgrabbus Jul 25 '12 at 19:15
  • @xhainingx "shell" isn't really an appropriate term for the Python interpreter. – JAB Jul 25 '12 at 19:16
  • When you Python in a terminal outside of the VT100 emulator, can you use the up- and down- arrows to cycle through commands? – machow Jul 25 '12 at 19:19
  • @JAB python shell is what IDLE calls an interactive session – Ryan Haining Jul 25 '12 at 19:25
  • @xhainingx: In that situation, I'd say that the IDLE window is acting as a shell for the interpreter. ...Wikipedia does describe the Python interpreter's interactive mode as being a "shell mode", however, so I guess you are right after all. Oh well. – JAB Jul 25 '12 at 19:26
  • @JAB yeah it's probably not the most accurate term but because of IDLE (I assume), it has become a fairly common one – Ryan Haining Jul 25 '12 at 19:40

3 Answers3

8

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.

phant0m
  • 16,595
  • 5
  • 50
  • 82
  • I've edited the original question to indicate this is the command line version of IDLE. How can I set options via the command line? Thanks. – octopusgrabbus Jul 25 '12 at 18:45
  • I don't remember if this is true for terminal input or not, but in IDLE you can also type the first part of a previous statement from further back before pressing Alt+N in order to retrieve that earlier statement without going through the ones in between. – JAB Jul 25 '12 at 18:46
  • 3
    @octopusgrabbus: Last I checked, IDLE is a (fairly basic) IDE that utilizes Python's `tkinter` module in order to display a Tk/TCL-based GUI. I've never heard of there being a command-line version of it (though you can start it from a terminal, of course). – JAB Jul 25 '12 at 18:48
  • 2
    @octopusgrabbus I'm wondering about the same thing? How do you start IDLE in the terminal? – phant0m Jul 25 '12 at 18:50
  • @JonClements Indeed, though that only really makes a difference when the text you type first matches with multiple previous commands, in which case an initial press of alt+p will retrieve the newest and of alt+n will retrieve the oldest (because the history commands wrap around). – JAB Jul 25 '12 at 18:56
  • 1
    Perhaps the tag is wrong, but IDLE is the Python command line that ships with Python. I'm not referring to a GUI IDE. If IDLE is called something else, I'll gladly edit the original question to use the correct term. – octopusgrabbus Jul 25 '12 at 18:57
  • 1
    Hang on - do you mean you just type "python" in the terminal window and work from that? – Jon Clements Jul 25 '12 at 18:58
  • 1
    @JonClements Yes. I have a VT100 emulated window, and type Python. – octopusgrabbus Jul 25 '12 at 19:08
  • 1
    @octopusgrabbus: You are using the Python interpreter in a terminal. That is not the same as IDLE, and the two should not be confused with each other. – Noctis Skytower Jul 25 '12 at 21:12
3

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.

JAB
  • 20,783
  • 6
  • 71
  • 80
0

You probably need the readline package:

pip install readline
Eino Gourdin
  • 4,169
  • 3
  • 39
  • 67