Platforms: Unix
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. Settings made using this module affect the behaviour of both the interpreter’s interactive prompt and the prompts offered by the raw_input() and input() built-in functions.
Note
On MacOS X the readline module can be implemented using the libedit library instead of GNU readline.
The configuration file for libedit is different from that of GNU readline. If you programmaticly load configuration strings you can check for the text “libedit” in readline.__doc__ to differentiate between GNU readline and libedit.
The readline module defines the following functions:
Clear the current history. (Note: this function is not available if the installed version of GNU readline doesn’t support it.)
New in version 2.4.
Return the number of lines currently in the history. (This is different from get_history_length(), which returns the maximum number of lines that will be written to a history file.)
New in version 2.3.
Return the current contents of history item at index.
New in version 2.3.
Remove history item specified by its position from the history.
New in version 2.4.
Replace history item specified by its position with the given line.
New in version 2.4.
Change what’s displayed on the screen to reflect the current contents of the line buffer.
New in version 2.3.
Get the completer function, or None if no completer function has been set.
New in version 2.3.
Get the type of completion being attempted.
New in version 2.6.
Set or remove the completion display function. If function is specified, it will be used as the new completion display function; if omitted or None, any completion display function already installed is removed. The completion display function is called as function(substitution, [matches], longest_match_length) once each time matches need to be displayed.
New in version 2.6.
See also
The following example demonstrates how to use the readline module’s history reading and writing functions to automatically load and save a history file named .pyhist from the user’s home directory. The code below would normally be executed automatically during interactive sessions from the user’s PYTHONSTARTUP file.
import os
histfile = os.path.join(os.environ["HOME"], ".pyhist")
try:
readline.read_history_file(histfile)
except IOError:
pass
import atexit
atexit.register(readline.write_history_file, histfile)
del os, histfile
The following example extends the code.InteractiveConsole class to support history save/restore.
import code
import readline
import atexit
import os
class HistoryConsole(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)
def init_history(self, histfile):
readline.parse_and_bind("tab: complete")
if hasattr(readline, "read_history_file"):
try:
readline.read_history_file(histfile)
except IOError:
pass
atexit.register(self.save_history, histfile)
def save_history(self, histfile):
readline.write_history_file(histfile)