readline — GNU readline interface¶
The readline module defines a number of functions to facilitate
completion and reading/writing of history files from the Python interpreter.
This module can be used directly, or via the rlcompleter module, which
supports completion of Python identifiers at the interactive prompt. Settings
made using this module affect the behaviour of both the interpreter’s
interactive prompt and the prompts offered by the built-in input()
function.
Readline keybindings may be configured via an initialization file, typically
.inputrc in your home directory. See Readline Init File
in the GNU Readline manual for information about the format and
allowable constructs of that file, and the capabilities of the
Readline library in general.
Availability: not Android, not iOS, not WASI.
This module is not supported on mobile platforms or WebAssembly platforms.
This is an optional module. If it is missing from your copy of CPython, look for documentation from your distributor (that is, whoever provided Python to you). If you are the distributor, see Requirements for optional modules.
Availability: Unix.
Note
The underlying Readline library API may be implemented by
the editline (libedit) library instead of GNU readline.
On macOS the readline module detects which library is being used
at run time.
The configuration file for editline is different from that
of GNU readline. If you programmatically load configuration strings
you can use backend to determine which library is being used.
If you use editline/libedit readline emulation on macOS, the
initialization file located in your home directory is named
.editrc. For example, the following content in ~/.editrc will
turn ON vi keybindings and TAB completion:
python:bind -v
python:bind ^I rl_complete
Also note that different libraries may use different history file formats. When switching the underlying library, existing history files may become unusable.
- readline.backend¶
The name of the underlying Readline library being used, either
"readline"or"editline".Added in version 3.13.
Init file¶
The following functions relate to the init file and user configuration:
- readline.parse_and_bind(string)¶
Execute the init line provided in the string argument. This calls
rl_parse_and_bind()in the underlying library.
- readline.read_init_file([filename])¶
Execute a readline initialization file. The default filename is the last filename used. This calls
rl_read_init_file()in the underlying library. It raises an auditing eventopenwith the file name if given, and"<readline_init_file>"otherwise, regardless of which file the library resolves.Changed in version 3.14: The auditing event was added.
Line buffer¶
The following functions operate on the line buffer:
- readline.get_line_buffer()¶
Return the current contents of the line buffer (
rl_line_bufferin the underlying library).
- readline.insert_text(string)¶
Insert text into the line buffer at the cursor position. This calls
rl_insert_text()in the underlying library, but ignores the return value.
- readline.redisplay()¶
Change what’s displayed on the screen to reflect the current contents of the line buffer. This calls
rl_redisplay()in the underlying library.
History file¶
The following functions operate on a history file:
- readline.read_history_file([filename])