Thereadline module defines a number of functions to facilitatecompletion and reading/writing of history files from the Python interpreter.This module can be used directly or via therlcompleter module. Settingsmade using this module affect the behaviour of both the interpreter’sinteractive prompt and the prompts offered by the built-ininput()function.
Note
On MacOS X thereadline module can be implemented usingthelibedit library instead of GNU readline.
The configuration file forlibedit is different from thatof GNU readline. If you programmatically load configuration stringsyou can check for the text “libedit” inreadline.__doc__to differentiate between GNU readline and libedit.
Thereadline module defines the following functions:
Parse and execute single line of a readline init file.
Return the current contents of the line buffer.
Insert text into the command line.
Parse a readline initialization file. The default filename is the last filenameused.
Load a readline history file. The default filename is~/.history.
Save a readline history file. The default filename is~/.history.
Clear the current history. (Note: this function is not available if theinstalled version of GNU readline doesn’t support it.)
Return the desired length of the history file. Negative values imply unlimitedhistory file size.
Set the number of lines to save in the history file.write_history_file()uses this value to truncate the history file when saving. Negative values implyunlimited history file size.
Return the number of lines currently in the history. (This is different fromget_history_length(), which returns the maximum number of lines that willbe written to a history file.)
Return the current contents of history item atindex.
Remove history item specified by its position from the history.
Replace history item specified by its position with the given line.
Change what’s displayed on the screen to reflect the current contents of theline buffer.
Set or remove the startup_hook function. Iffunction is specified, it will beused as the new startup_hook function; if omitted orNone, any hook functionalready installed is removed. The startup_hook function is called with noarguments just before readline prints the first prompt.
Set or remove the pre_input_hook function. Iffunction is specified, it willbe used as the new pre_input_hook function; if omitted orNone, any hookfunction already installed is removed. The pre_input_hook function is calledwith no arguments after the first prompt has been printed and just beforereadline starts reading input characters.
Set or remove the completer function. Iffunction is specified, it will beused as the new completer function; if omitted orNone, any completerfunction already installed is removed. The completer function is called asfunction(text,state), forstate in0,1,2, ..., until itreturns a non-string value. It should return the next possible completionstarting withtext.
Get the completer function, orNone if no completer function has been set.
Get the type of completion being attempted.
Get the beginning index of the readline tab-completion scope.
Get the ending index of the readline tab-completion scope.
Set the readline word delimiters for tab-completion.
Get the readline word delimiters for tab-completion.
Set or remove the completion display function. Iffunction isspecified, it will be used as the new completion display function;if omitted orNone, any completion display function alreadyinstalled is removed. The completion display function is called asfunction(substitution,[matches],longest_match_length) onceeach time matches need to be displayed.
Append a line to the history buffer, as if it was the last line typed.
See also
The following example demonstrates how to use thereadline module’shistory reading and writing functions to automatically load and save a historyfile named.pyhist from the user’s home directory. The code below wouldnormally be executed automatically during interactive sessions from the user’sPYTHONSTARTUP file.
importosimportreadlinehistfile=os.path.join(os.path.expanduser("~"),".pyhist")try:readline.read_history_file(histfile)exceptFileNotFoundError:passimportatexitatexit.register(readline.write_history_file,histfile)delos,histfile
The following example extends thecode.InteractiveConsole class tosupport history save/restore.
importcodeimportreadlineimportatexitimportosclassHistoryConsole(code.InteractiveConsole):def__init__(self,locals=None,filename="<console>",histfile=os.path.expanduser("~/.console-history")):code.InteractiveConsole.__init__(self,locals,filename)self.init_history(histfile)definit_history(self,histfile):readline.parse_and_bind("tab: complete")ifhasattr(readline,"read_history_file"):try:readline.read_history_file(histfile)exceptFileNotFoundError:passatexit.register(self.save_history,histfile)defsave_history(self,histfile):readline.write_history_file(histfile)
6.6.stringprep — Internet String Preparation
6.8.rlcompleter — Completion function for GNU readline
Enter search terms or a module, class or function name.