I’d like to share a secret feature within the Python 3.13 REPL which I’ve been finding useful recently: adding custom keyboard shortcuts.
In other words: _pyrepl is designed to be used by Python’s standard library modules and not anyone else. That means that we should assume this code will break in a future Python version.
So we’ve made one custom key combination for ourselves. How can we setup this command automatically whenever the Python REPL starts?
When Python launches, if it sees a PYTHONSTARTUP environment variable it will treat that environment variable as a Python file to run on startup.
I’ve made a /home/trey/.python_startup.py file and I’ve set this environment variable in my shell’s configuration file (~/.zshrc):
Note that this might still raise an exception when Python starts if the reader object’s command attribute or bind method change in a way that breaks our code.