How to repeat last command in python interpreter s

2019-01-16 02:58发布

How do I repeat the last command? The usual keys: Up, Ctrl+Up, Alt-p don't work. They produce nonsensical characters.

(ve)[kakarukeys@localhost ve]$ python
Python 2.6.6 (r266:84292, Nov 15 2010, 21:48:32) 
[GCC 4.4.4 20100630 (Red Hat 4.4.4-10)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "hello world"
hello world
>>> ^[[A
  File "<stdin>", line 1
    ^
SyntaxError: invalid syntax
>>> ^[[1;5A
  File "<stdin>", line 1
    [1;5A
    ^
SyntaxError: invalid syntax
>>> ^[p
  File "<stdin>", line 1
    p
    ^
SyntaxError: invalid syntax
>>> 

25条回答
成全新的幸福
2楼-- · 2019-01-16 03:11

Up Arrow works only in Python command line.

In IDLE (Python GUI) the defaults are: Alt-p : retrieves previous command matching what you have typed. Alt-n : retrieves next... In Python 2.7.9 for example, you can see/change the Action Keys selecting: Options -> Configure IDLE -> (Tab) Keys

查看更多
Emotional °昔
3楼-- · 2019-01-16 03:14

You don't need a custom script like pyfunc's answer for OSX (at least on mavericks). In Idle click on Idle -> Preferences -> Keys, locate "history-next" and "history-previous", and either leave them with their default keyboard shortcut or assign "up arrow" and "down arrow" per typical expected terminal behavior.

This is on Idle 2.7 on OSX Mavericks.

查看更多
劫难
4楼-- · 2019-01-16 03:14

For anaconda for python 3.5, I needed to install ncurses

conda install ncurses

After the ncurses install tab complete, history, and navigating via left and right arrows worked in the interactive shell.

查看更多
Emotional °昔
5楼-- · 2019-01-16 03:18

If using MacOSX, press control p to cycle up and control n to cycle down. I am using IDLE Python 3.4.1 Shell.

查看更多
老娘就宠你
6楼-- · 2019-01-16 03:20

Ctrl+p is the normal alternative to the up arrow. Make sure you have gnu readline enabled in your Python build.

查看更多
相关推荐>>
7楼-- · 2019-01-16 03:21

By default use ALT+p for previous command, you can change to Up-Arrow instead in IDLE GUi >> OPtions >> Configure IDLE >>Key >>Custom Key Binding It is not necesary to run a custom script, besides readlines module doesnt run in Windows. Hope That Help. :)

查看更多
登录 后发表回答