GNU readline support for Python on platforms without readline
npx @tessl/cli install tessl/pypi-readline@2.6.0GNU readline support for Python on platforms without readline. This module provides Python bindings to the GNU readline library, enabling command-line editing, history management, and tab completion functionality for interactive Python sessions.
pip install readlineimport readlineimport readline
# Configure readline
readline.parse_and_bind('tab: complete') # Enable tab completion
readline.parse_and_bind('set editing-mode vi') # Set vi editing mode
# Set up history
readline.read_history_file() # Load history from ~/.history
readline.set_history_length(1000) # Limit history to 1000 entries
# Set up completion
def completer(text, state):
options = ['hello', 'help', 'history', 'home']
matches = [opt for opt in options if opt.startswith(text)]
return matches[state] if state < len(matches) else None
readline.set_completer(completer)
readline.set_completer_delims(' \t\n')
# Interactive input with readline features
user_input = input("Enter command: ") # Now has history, completion, editing
# Save history when done
readline.write_history_file()The readline module provides a Python interface to GNU readline's command-line editing capabilities:
This module automatically integrates with Python's input() function, providing enhanced command-line editing for interactive sessions.
Core configuration functions for setting up readline behavior, parsing initialization files, and configuring the readline environment.
def parse_and_bind(string): ...
def read_init_file(filename=None): ...Comprehensive history management including file operations, length control, and direct history manipulation with functions for reading, writing, and modifying command history.
def read_history_file(filename=None): ...
def write_history_file(filename=None): ...
def set_history_length(length): ...
def get_history_length(): ...
def add_history(string): ...Functions for accessing and manipulating the current line buffer, enabling custom editing behavior and text insertion capabilities.
def get_line_buffer(): ...
def insert_text(string): ...
def redisplay(): ...Comprehensive tab completion system with customizable completion functions, delimiter configuration, and completion context access.
def set_completer(function=None): ...
def get_completer(): ...
def set_completer_delims(string): ...
def get_completer_delims(): ...Advanced customization through event hooks that execute at specific points in the readline process, enabling custom display and input handling.
def set_startup_hook(function=None): ...
def set_pre_input_hook(function=None): ...
def set_completion_display_matches_hook(function=None): ...