How to repeat last command in python interpreter shell?

177,101

Solution 1

I use the following to enable history on python shell.

This is my .pythonstartup file . PYTHONSTARTUP environment variable is set to this file path.

# python startup file 
import readline 
import rlcompleter 
import atexit 
import os 
# tab completion 
readline.parse_and_bind('tab: complete') 
# history file 
histfile = os.path.join(os.environ['HOME'], '.pythonhistory') 
try: 
    readline.read_history_file(histfile) 
except IOError: 
    pass 
atexit.register(readline.write_history_file, histfile) 
del os, histfile, readline, rlcompleter

You will need to have the modules readline, rlcompleter to enable this.

Check out the info on this at : http://docs.python.org/using/cmdline.html#envvar-PYTHONSTARTUP.

Modules required:

  1. http://docs.python.org/library/readline.html
  2. http://docs.python.org/library/rlcompleter.html

Solution 2

In IDLE, go to Options -> Configure IDLE -> Keys and there select history-next and then history-previous to change the keys.

Then click on Get New Keys for Selection and you are ready to choose whatever key combination you want.

Solution 3

Alt + p for previous command from histroy, Alt + n for next command from history.

This is default configure, and you can change these key shortcut at your preference from Options -> Configure IDLE.

Solution 4

You didn't specify which environment. Assuming you are using IDLE.

From IDLE documentation: Command history:

Alt-p retrieves previous command matching what you have typed.
Alt-n retrieves next.
      (These are Control-p, Control-n on the Mac)
Return while cursor is on a previous command retrieves that command.
Expand word is also useful to reduce typing.

Solution 5

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. :)

Share:
177,101
kakarukeys
Author by

kakarukeys

A web developer looking for startup opportunity.

Updated on March 15, 2021

Comments

  • kakarukeys
    kakarukeys about 3 years

    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
    >>> 
    
  • pyfunc
    pyfunc over 13 years
    @user496852: Just set the env variable PYTHONSTARTUP to the filepath containing above code. Also check, if you have the required modules.
  • Timofey
    Timofey about 11 years
    it is not necessary, just follow instruction of basak's answer and assign key bindings
  • creativeby
    creativeby almost 11 years
  • ctzdev
    ctzdev over 9 years
    For anyone who encounters this problem, I am on 14.04 and was still able to use this solution to fix this problem going from 3.4.0 to 3.4.2.
  • Ziggy Eunicien
    Ziggy Eunicien over 9 years
    'pip install readline' worked for me. All my control sequences were coming out with bracket prefixes on Centos 7 after python 3.4 manual install
  • user1063287
    user1063287 over 9 years
    For searchers, this works in Linux Mint 17 Cinnamon too.
  • Rob Watts
    Rob Watts over 8 years
    I needed to do sudo pip install readline after this to get it to work (python 2.7.11)
  • John Doe
    John Doe almost 8 years
    This is the solution I am looking for in IDLE. Up arrow worked on python interpreter launched from bash shell.
  • jyao
    jyao about 7 years
    works for windows 10, python 3.6.1 as well. Thanks a lot, this is clean and clear, esp. useful for new learners.
  • user32882
    user32882 almost 7 years
    An overkill. Just do alt+p
  • Davidson Lima
    Davidson Lima over 6 years
    Most appropriate answer. Should have been chosen as the correct one.
  • DaniPaniz
    DaniPaniz over 6 years
    "Could not find a version that satisfies the requirement py-readline (from versions: ) No matching distribution found for py-readline" I hate this world
  • DaniPaniz
    DaniPaniz over 6 years
    there is no Options -> Configure IDLE for Python 2.7 :/
  • DaniPaniz
    DaniPaniz over 6 years
    I needed to go to settings/preferences (python 2.7, IDLE for mac) and there I found the history-next, thanks so much you are the best :)
  • DaniPaniz
    DaniPaniz over 6 years
    it's called history-next / history-previous
  • not2qubit
    not2qubit over 6 years
    Make sure you use pip2 or pip3, depending on what version you have installed.
  • Andriy Makukha
    Andriy Makukha over 6 years
    Installing libncurses5-dev and libncursesw5-dev was sufficient to fix my Python 3.5 installation, but Python 3.6 broke up after I installed readline module for it. Probably need to recompile.
  • Eduard
    Eduard over 6 years
    does not work, the old shortcut persists even after resetting.
  • ashleedawg
    ashleedawg over 5 years
    this asks if I want to print
  • Roman
    Roman over 5 years
    To get to IDLE, run idle3 in shell
  • akpp
    akpp almost 5 years
    It helped me Ubuntu 18.04 python version 3.7.3 pip install readline
  • Zim
    Zim almost 5 years
    This didn't work for me: UnicodeEncodeError: 'ascii' codec can't encode character u'\uf700' in position 0: ordinal not in range(128) UnicodeEncodeError: 'ascii' codec can't encode character u'\uf701' in position 0: ordinal not in range(128)
  • osjerick
    osjerick almost 5 years
    This worked for me on 64-bit Ubuntu 16 as well. I have 32-bit Python 3.5.2, compiled and installed from sources, in addition to the already apt-installed 64-bit Pythons. Just did sudo apt-get install libncurses-dev libncurses-dev:i386 libreadline-dev libreadline-dev:i386 and reinstalled the source-built Python.
  • Gulzar
    Gulzar about 4 years
    what's IDLE? How do do this in pycharm console?
  • Wolfgang Kuehn
    Wolfgang Kuehn over 3 years
    This breaks python for version 3.8. Do not install!
  • not2qubit
    not2qubit over 3 years
    If arrow-up doesn't work, you need to fix your installation. Probably need pyreadline.
  • not2qubit
    not2qubit over 3 years
    @WolfgangKuehn Have any bug issue been filed about this to their repo? (if not, please file it.) Sound pretty serious.
  • Wolfgang Kuehn
    Wolfgang Kuehn over 3 years
    @not2qubit I have not. This issue was on a compiled Python 3.8.6 on a current Amazon Linux 2. I still don't know why readline is not working (lib missing, compile switch, etc.), haven't got the time.
  • Shlomo Gottlieb
    Shlomo Gottlieb about 3 years
    As a Windows user, I had to first install pyreadline, and change the histfile definition by using histfile = pathlib.Path.home().joinpath('.pythonhistory').
  • kiriloff
    kiriloff over 2 years
    How to put it to "bottom arrow" as in a bash shell ?
  • Alan
    Alan over 2 years
    Indeed, on Windows, with Python 3.9, "pip install pyreadline" was all it took to make up-arrow fetch the previous comment, which wasn't happening previously. Thanks.
  • not2qubit
    not2qubit over 2 years
    pyreadline is dead meat. Use pyreadline3 which now works with Py3.10.
  • not2qubit
    not2qubit over 2 years
    Use pyreadline3 which now works with Py3.10.
  • pretzlstyle
    pretzlstyle about 2 years
    How does one change these bindings? I'm just using python in bash, not IDLE. Alt+p,n works. I'd like them to be mapped to up/down arrows. This works fine in bash.