On this page
readline
Prev: re | Up: stdlib_module | Next: reprlib
Rel: string, re, difflib, textwrap, unicodedata
Ref: docs.python.org — readline
Tags: #public #python3 #stdlib
Import
import readline
Overview
Python interface to the GNU Readline (or macOS libedit) line-editing library. Importing readline automatically equips input(), cmd, and the interactive Python REPL with arrow-key navigation, persistent command history, and programmable tab completion.
Quick Start
import readline
# Enable basic tab completion
COMMANDS = ["status", "start", "stop", "restart"]
def completer(text: str, state: int) -> str | None:
matches = [c for c in COMMANDS if c.startswith(text)]
return matches[state] if state < len(matches) else None
readline.set_completer(completer)
# Note: libedit (macOS) vs GNU readline use different bind syntax
if "libedit" in (readline.__doc__ or ""):
readline.parse_and_bind("bind ^I rl_complete")
else:
readline.parse_and_bind("tab: complete")
Implementation Walkthrough
1. Persisting History Across CLI Sessions
Pair read_history_file() with atexit and write_history_file() so users keep their history across runs:
import atexit
from pathlib import Path
import readline
def setup_history(history_path: Path, max_entries: int = 500) -> None:
readline.set_history_length(max_entries)
if history_path.exists():
readline.read_history_file(str(history_path))
atexit.register(readline.write_history_file, str(history_path))
Gotchas & Boundaries
- Unix-only:
readlineis not available in the Windows standard library (Windows users typically installpyreadline3or useprompt_toolkit). - macOS ships
libedit, not GNU Readline: Check"libedit" in (readline.__doc__ or "")before callingparse_and_bind(), becausetab: completeis silently ignored bylibedit. - 1-based history indexing:
readline.get_history_item(index)is 1-indexed (1toget_current_history_length()), unlike Python lists. - Process-global state: Completers and keybindings apply globally to
input()across the entire process.
Common Functions
- readline.add_history() —
add_history(string, /)— Add an item to the history buffer. - readline.clear_history() —
clear_history()— Clear the current readline history. - readline.get_begidx() —
get_begidx()— Get the beginning index of the completion scope. - readline.get_completer() —
get_completer()— Get the current completer function. - readline.get_completer_delims() —
get_completer_delims()— Get the word delimiters for completion. - readline.get_completion_type() —
get_completion_type()— Get the type of completion being attempted. - readline.get_current_history_length() —
get_current_history_length()— Return the current (not the maximum) length of history. - readline.get_endidx() —
get_endidx()— Get the ending index of the completion scope. - readline.get_history_item() —
get_history_item(index, /)— Return the current contents of history item at one-based index. - readline.get_history_length() —
get_history_length()— Return the maximum number of lines that will be written to the history file. - readline.get_line_buffer() —
get_line_buffer()— Return the current contents of the line buffer. - readline.insert_text() —
insert_text(string, /)— Insert text into the line buffer at the cursor position. - readline.parse_and_bind() —
parse_and_bind(string, /)— Execute the init line provided in the string argument. - readline.read_history_file() —
read_history_file(filename=None, /)— Load a readline history file. - readline.read_init_file() —
read_init_file(filename=None, /)— Execute a readline initialization file. - readline.redisplay() —
redisplay()— Change what's displayed on the screen to reflect contents of the line buffer. - readline.remove_history_item() —
remove_history_item(pos, /)— Remove history item given by its zero-based position. - readline.replace_history_item() —
replace_history_item(pos, line, /)— Replaces history item given by its position with contents of line. - readline.set_auto_history() —
set_auto_history(enabled, /)— Enables or disables automatic history. - readline.set_completer() —
set_completer(function=None, /)— Set or remove the completer function. - readline.set_completer_delims() —
set_completer_delims(string, /)— Set the word delimiters for completion. - readline.set_completion_display_matches_hook() —
set_completion_display_matches_hook(function=None, /)— Set or remove the completion display function. - readline.set_history_length() —
set_history_length(length, /)— Set the maximal number of lines which will be written to the history file. - readline.set_pre_input_hook() —
set_pre_input_hook(function=None, /)— Set or remove the function invoked by the rl_pre_input_hook callback. - readline.set_startup_hook() —
set_startup_hook(function=None, /)— Set or remove the function invoked by the rl_startup_hook callback. - readline.write_history_file() —
write_history_file(filename=None, /)— Save a readline history file.
Constants & Attributes
readline.backend(str) ='editline'
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/cmd
- tuts/python3/stdlib_module/re
- tuts/python3/stdlib_module/readline/add_history
- tuts/python3/stdlib_module/readline/clear_history
- tuts/python3/stdlib_module/readline/get_begidx
- tuts/python3/stdlib_module/readline/get_completer
- tuts/python3/stdlib_module/readline/get_completer_delims
- tuts/python3/stdlib_module/readline/get_completion_type
- tuts/python3/stdlib_module/readline/get_current_history_length
- tuts/python3/stdlib_module/readline/get_endidx
- tuts/python3/stdlib_module/readline/get_history_item
- tuts/python3/stdlib_module/readline/get_history_length
- tuts/python3/stdlib_module/readline/get_line_buffer
- tuts/python3/stdlib_module/readline/insert_text
- tuts/python3/stdlib_module/readline/parse_and_bind
- tuts/python3/stdlib_module/readline/read_history_file
- tuts/python3/stdlib_module/readline/read_init_file
- tuts/python3/stdlib_module/readline/redisplay
- tuts/python3/stdlib_module/readline/remove_history_item
- tuts/python3/stdlib_module/readline/replace_history_item
- tuts/python3/stdlib_module/readline/set_auto_history
- tuts/python3/stdlib_module/readline/set_completer
- tuts/python3/stdlib_module/readline/set_completer_delims
- tuts/python3/stdlib_module/readline/set_completion_display_matches_hook
- tuts/python3/stdlib_module/readline/set_history_length
- tuts/python3/stdlib_module/readline/set_pre_input_hook
- tuts/python3/stdlib_module/readline/set_startup_hook
- tuts/python3/stdlib_module/readline/write_history_file
- tuts/python3/stdlib_module/reprlib
- tuts/python3/stdlib_module/rlcompleter
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/cmd Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/re Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/readline/add_history Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/readline/clear_history Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/readline/get_begidx Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/readline/get_completer Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/readline/get_completer_delims Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/readline/get_completion_type Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/readline/get_current_history_length Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/readline/get_endidx Linked here; links to this page; shared tags: #stdlib