I sometimes test Python modules as I develop them by running a Python interactive prompt in a terminal, importing my new module and testing out the functionality. Of course, since my code is in development there are bugs, and frequent restarts of the interpreter are required. This isn't too painful when I've only executed a couple of interpreter lines before restarting: my key sequence when the interpreter restart looks like Up Up Enter Up Up Enter
... but extrapolate it to 5 or more statements to be repeated and it gets seriously painful!
Of course I could put my test code into a script which I execute with python -i
, but this is such a scratch activity that it doesn't seem quite "above threshold" for opening a text editor :) What I'm really pining for is the Ctrl-r
behaviour from the bash shell: executing a sequence of 10 commands in sequence in bash involves finding the command in history (repeated Up
or Ctrl-r
for a search -- both work in the Python interpreter shell) and then just pressing Ctrl-o
ten times. One of my favourite bash shell features.
The problem is that while lots of other readline binding functionality like Ctrl-a
, Ctrl-e
, Ctrl-r
, and Ctrl-s
work in the Python interpreter, Ctrl-o
does not. I've not been able to find any references to this online, although perhaps the readline
module can be used to add this functionality to the python
prompt. Any suggestions?
Edit: Yes, I know that using the interactive interpreter is not a development methodology that scales beyond a few lines! But it is convenient for small tests, and IMO the interactiveness can help to work out whether a developing API is natural and convenient, or too heavy. So please confine the answers to the technical question of whether readline history-stepping can be made to work in python, rather than the side-opinion of whether one should or shouldn't choose to (sometimes) work this way!
Edit: Since posting I realised that I am already using the readline
module to make some Python interpreter history functions work. But the Ctrl-o binding to the operate-and-get-next
readline command doesn't seem to be supported, even if I put readline.parse_and_bind("Control-o: operate-and-get-next")
in my PYTHONSTARTUP
file.