Sub topic 10
Editing lines and using command history
This chapter assumes you can start the shell, type expressions, and write multi-line blocks. You will learn to fix a typo without retyping a line, bring back earlier input, and know where history is stored.
Why editing matters
In the shell, each line you type may be a small variation of the last one. Retyping whole lines wastes time and adds new typos. The shell lets you recall earlier input and edit it in place.
Editing support comes from a line-editing library, not from Python's language rules. Which library you get depends on your platform and Python version:
| Environment | Line editing comes from |
|---|---|
| Python 3.13 and later (Linux, macOS, Windows) | The built-in REPL, which has its own editor |
| Older Python on Linux and macOS | GNU readline or libedit, if Python was built with it |
| Older Python on Windows | The Windows console itself |
Exact key bindings differ between these. The ones below work almost everywhere.
Moving and editing within a line
Use the arrow keys to move the cursor. Most environments also accept these Emacs-style keys:
| Key | Action |
|---|---|
| Left / Right | Move one character |
| Home or Ctrl+A | Jump to the start of the line |
| End or Ctrl+E | Jump to the end of the line |
| Backspace or Delete | Remove a character |
| Ctrl+K | Delete from the cursor to the end of the line |
Suppose you type a wrong name:
>>> math.sqrt(16)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'math' is not definedPress Up to bring the line back. Then run import math, press Up twice, and press Enter. Or fix the first line with the cursor keys.
Walking through history
The Up arrow recalls the previous input. Up again goes further back, and Down moves forward. Pressing Enter runs the recalled line, edited or not, and adds it to history as a new entry.
Searching history
When history is long, pressing Up repeatedly is slow. Press Ctrl+R and type part of an earlier line. The shell shows the most recent match. Press Ctrl+R again to move to an older match, and press Enter to run it. You can also press the right arrow to put the match on the prompt for editing.
Ctrl+R works with readline, libedit, and the Python 3.13 REPL. The Windows console in older versions does not have it. There, press F7 to open a list of earlier commands.
Multi-line blocks and history
Older readline-based shells store each physical line of a block as a separate history entry. Recalling a loop means pressing Up once per line, and you cannot go back and edit an earlier line of the block after you have pressed Enter on it.
Python 3.13 and later recall a whole block as one unit. You can move the cursor up and down inside it and edit any line. Press F2 to open a browsable history view. Press F3 to enter paste mode, which lets you paste several lines without each newline running immediately.
Tab completion
Press Tab after typing part of a name, and the shell completes it or lists the choices. After import ma, Tab suggests names such as math. After math.sq, Tab completes to math.sqrt. Completion is a quick way to explore a module without calling dir().
>>> import math
>>> math.sq<Tab>
>>> math.sqrt(At the start of an indented line, Tab inserts indentation instead of completing.
Where history is saved
On Linux and macOS, history from one session is saved when you exit normally. It goes to a file called .python_history in your home directory. The next session loads it, so Up can recall commands from earlier days. Python 3.13 and later also store history in that file on all platforms, which includes Windows.
You can inspect or delete the file like any other text file:
tail -n 5 ~/.python_historyCommon problems
- Arrow keys print characters like
^[[A. Your Python was built without readline support. Install a build that includes it, or use Python 3.13 or later. - Ctrl+R does nothing. Your shell may not support it. Try F7 on older Windows consoles.
- History is empty after a restart. The shell was probably closed by killing the terminal instead of exiting normally, so history was never written.
Exercise
You typed a 4-line for loop in Python 3.12 on Linux and want to run it again with one number changed. What happens when you press Up, and how does Python 3.13 differ?
Show answer
In Python 3.12 with readline, Up recalls only the last line of the block, so you must press Up once per line and re-enter each one in order. Python 3.13 and later recall the whole block as one entry, so you can move through its lines, change the number, and run it in one go.
Exercise
Where does the shell save your history between sessions, and why should you be careful with it?
Show answer
It is saved in .python_history in your home directory. The file is plain text, so any secret you typed at the prompt can be stored there.