How to repeat last command in python interpreter shell?
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:
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. :)
Comments
-
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 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 about 11 yearsit is not necessary, just follow instruction of basak's answer and assign key bindings
-
creativeby almost 11 yearsFor further information: docs.python.org/2/library/idle.html#python-shell-window
-
ctzdev over 9 yearsFor 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
to3.4.2
. -
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 over 9 yearsFor searchers, this works in Linux Mint 17 Cinnamon too.
-
Rob Watts over 8 yearsI needed to do
sudo pip install readline
after this to get it to work (python 2.7.11) -
John Doe almost 8 yearsThis is the solution I am looking for in IDLE. Up arrow worked on python interpreter launched from bash shell.
-
jyao about 7 yearsworks for windows 10, python 3.6.1 as well. Thanks a lot, this is clean and clear, esp. useful for new learners.
-
user32882 almost 7 yearsAn overkill. Just do alt+p
-
Davidson Lima over 6 yearsMost appropriate answer. Should have been chosen as the correct one.
-
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 over 6 yearsthere is no Options -> Configure IDLE for Python 2.7 :/
-
DaniPaniz over 6 yearsI 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 over 6 yearsit's called history-next / history-previous
-
not2qubit over 6 yearsMake sure you use
pip2
orpip3
, depending on what version you have installed. -
Andriy Makukha over 6 yearsInstalling 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 over 6 yearsdoes not work, the old shortcut persists even after resetting.
-
ashleedawg over 5 yearsthis asks if I want to print
-
Roman over 5 yearsTo get to IDLE, run
idle3
in shell -
akpp almost 5 yearsIt helped me Ubuntu 18.04 python version 3.7.3
pip install readline
-
Zim almost 5 yearsThis 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 almost 5 yearsThis 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 about 4 yearswhat's IDLE? How do do this in pycharm console?
-
Wolfgang Kuehn over 3 yearsThis breaks python for version 3.8. Do not install!
-
not2qubit over 3 yearsIf arrow-up doesn't work, you need to fix your installation. Probably need
pyreadline
. -
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 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 about 3 yearsAs a Windows user, I had to first install pyreadline, and change the
histfile
definition by usinghistfile = pathlib.Path.home().joinpath('.pythonhistory')
. -
kiriloff over 2 yearsHow to put it to "bottom arrow" as in a bash shell ?
-
Alan over 2 yearsIndeed, 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 over 2 years
-
not2qubit over 2 yearsUse
pyreadline3
which now works withPy3.10
. -
pretzlstyle about 2 yearsHow does one change these bindings? I'm just using
python
inbash
, not IDLE. Alt+p,n works. I'd like them to be mapped to up/down arrows. This works fine inbash
.