Python Morsels: Python REPL Shortcuts & Features

Python Morsels: Python REPL Shortcuts & Features

Python 3.13 introduced a completely redesigned REPL with a much more modern feel. It's easy to overlook many of the features in the new Python REPL.

Let's explore the various hidden features the new Python REPL supports, with a focus on tips that are useful for everyday usage.

Python REPL Keyboard Shortcuts

Let's start with the keyboard shortcuts the new Python REPL supports. This table is organized with the most useful shortcuts at the top.

Shortcut Type Action OS Support
Left Navigation Go left one character All
Right Navigation Go right one character All
Up Navigation Previous block or line All
Down Navigation Next block or line All
Ctrl+L Feature Clear screen All
Ctrl+C Feature Cancel / interrupt All
Ctrl+D Exit Exit REPL (Sends EOF) Linux/Mac
Ctrl+Z Enter Exit Exit REPL (Sends EOF) Windows
Tab Feature Autocompletion All
Alt+Enter Feature Run current code block All
Ctrl+R Feature Search upward in history All
Ctrl+A Navigation Go to beginning of line Linux/Mac
Home Navigation Go to beginning of line All
Ctrl+E Navigation Go to end of line All
End Navigation Go to end of line All
Ctrl+K Deletion Delete to end of line All
Ctrl+U Deletion Delete to start of line All
Ctrl+Left Navigation Go back one word All
Alt+B Navigation Go back one word All
Ctrl+Right Navigation Go forward one word All
Alt+F Navigation Go forward one word All
Alt+Backspace Deletion Delete to start of word All
Ctrl+W Deletion Delete to start of word All
Alt+D Deletion Delete to end of word All
F2 Feature Enter history mode All
F3 Feature Enter paste/edit mode All
F1 Feature Enter help mode All

Ctrl+A does not work in the usual Windows Command Prompt or Powershell (it selects all text instead) unless they are run within the new Windows 10 Terminal app.

On Linux and Mac, Ctrl+D can be used to exit the REPL, but on Windows you'll need to press Ctrl+Z and then press Enter afterward (to submit). Using Ctrl+D on Linux/Mac or Ctrl+Z and Enter on Windows only works when the shortcut is pressed at a blank prompt.

Note that the above shortcuts assume you are working in the new Python REPL (3.13+).

On Python 3.12 the only shortcuts above that worked on Windows are:

  • Left and Right for navigating characters
  • Up and Down for navigating line-by-line history
  • Ctrl+Z Enter for exiting
  • Ctrl+Left and Ctrl+Right for navigating words

Most of those shortcuts also worked on Python 3.12 on Linux and Mac except:

  • Up and Down only navigated line-by-line history
  • Alt+Enter did nothing
  • F2 and F3 did nothing because these modes were added in the new REPL
  • Some shortcuts (like Ctrl+U) may differ slightly in their abilities

Shortcuts You Probably Won't Use

The above list of shortcuts isn't actually comprehensive. I deliberately left out some shortcuts that are tricky to use and which I recommend trying to learn.

Here are a few of the remaining shortcuts that you might use, but I highly doubt it.

Shortcut Action Example
Ctrl+Y Paste (yank) previously "killed" text Use after any deletion command
Alt+Y Cycle through "kill ring" Press repeatedly after Ctrl+Y
Ctrl+T Transpose with previous character Swap o in improt to make import
Alt+<digit> Repeat command N times Moving left N characters

All the "deletion" commands are actually "kill" commands (Ctrl+K, Ctrl+U, Alt+Backspace, Alt+D) which put deleted text into a "kill ring" that you can "yank" back from and cycle through. This is a very Emacs-like feature and I don't find myself ever using it.

Using Alt+<digit> will load a digit argument, similar to the numeric argument system within Emacs. Here are some examples:

  • Alt+3 Alt+D: delete to end of word 3 times (to delete to end of next 3 words)
  • Alt+1 Alt+5 Left: navigate to the left 15 characters
  • Alt+8 Alt+0 =: enter 80 = characters

If you accidentally press one of the "digit arg" Alt+<digit> commands, you can use Ctrl+G to cancel your input.

Using the Up and Down keys in the new Python REPL now navigates blocks of code instead of just lines of code.

There are some important things to note about this navigation:

  • If you hit Up to go to a previous block, make a change, and accidentally hit Up too many times to go to a previous block, and then hit Down, your changes will not be discarded
  • If you want to discard changes made to a code block or cancel writing a code block you can press Ctrl+C, which will print KeyboardInterrupt and show a fresh prompt
  • While editing a code block, you can add additional lines at the beginning of a block of code to write more than one actual block of code within one "block"

Easy Code Copying with History Mode (F2)

Ever wished you could copy just the code you typed into the REPL, without all the >>> and ... prefixes before each line? You can use history mode for that!

Hitting F2 will enter history mode, which shows the code you typed into the REPL without showing the prompts or output. Then you can copy-paste as usual.

Editing Blocks with Paste Mode (F3)

Occasionally you may also find "paste mode" helpful for editing blocks of code without accidentally running them by hitting Enter a couple times at the end of the block. While editing a block of code (or within a fresh block) you can hit F3 to enter paste mode and then edit your block. Once you're done, hit F3 again to go back to normal mode. You'll never actually need paste mode for pasting because the new REPL supports a "bracketed paste" terminal feature which makes pasting just work as expected, so you can think of paste mode as edit mode.

The _ Variable

The _ variable (if not deliberately set) will hold the value of the previously run statement, if there is one:

This feature exists in both the old and new REPLs.

Notable Features in the New REPL

If you are just upgrading to the new Python REPL (3.13+) be sure to note that:

  • Pasting blocks of text works excellently now
  • The Up and Down keys now navigate code blocks (not just lines of code)
  • Code blocks are auto-indented in the new REPL (4 spaces are used for indentation)
  • Hitting Tab now indents to the next tab stop (using 4 spaces) instead of inserting a tab character
  • Hitting Backspace in or after line indentation deletes back to the previous tab stop
  • Typing exit or quit will exit the REPL
  • You can create custom keyboard shortcuts in the new REPL

The REPL in Python 3.14 also includes syntax highlighting now. You can (unofficially) customize the Python REPL syntax highlighting as well.

Additionally, the Python 3.14 REPL now allows the Tab key to be used for autocompleting module names. Type from coll and hit Tab and you'll likely see from collections. Type import _ and hit Tab twice and you'll see a list of many _-prefixed module names.

Launching the REPL

Running python without any arguments will launch the Python REPL:

$ python
Python 3.14.0
Type "help", "copyright", "credits" or "license" for more information.
>>>

But that's not the only way to launch an interactive Python interpreter!

If you'd like poke around after a .py file has been run, you can pass the -i argument to Python to drop into interactive mode:

$ python -i fibonacci.py
>>>
>>> nth_fibonacci(10)
55
>>> phi
1.618033988749895

If you use Python's breakpoint() function to debug your Python code to, you'll enter PDB (the Python debugger) which is a REPL-like environment. PDB is not quite a REPL but it can run individual Python commands. To enter a full Python REPL from PDB you can use the interact command:

(Pdb) interact
*interactive*
>>>

Also note that the default Python REPL is not the only interactive Python prompt that exists.

Python also includes IDLE, which is a graphical interactive prompt. It has some features the REPL does not and also includes some limitations the REPL does not. You can run IDLE on any machine with:

You could also use IPython, ptpython, or bpython instead. Or if you prefer to work in a web browser, use a Jupyter notebook. These are all REPL-like environments. Do note that many of these other REPL-like environments support slightly different keyboard shortcuts than the default Python REPL.

Learn the Python REPL

I often pop open a Python REPL to perform a calculation or play around with some code. I also spend much of my teaching time within a Python REPL.

If you spend quite a bit of time in a Python REPL as well, it's worth learning the features the REPL supports. Bookmark this page, try to remember the most useful keyboard shortcuts above, and feel free to share this resource with others.

For other useful REPL-related resources see inspecting Python objects, understanding help(), and the features of help().

Stay Informed

Get the best articles every day for FREE. Cancel anytime.