1
:mod:`readline` --- GNU readline interface
2
==========================================
6
:synopsis: GNU readline support for Python.
7
.. sectionauthor:: Skip Montanaro <skip@pobox.com>
10
The :mod:`readline` module defines a number of functions to facilitate
11
completion and reading/writing of history files from the Python interpreter.
12
This module can be used directly or via the :mod:`rlcompleter` module. Settings
13
made using this module affect the behaviour of both the interpreter's
14
interactive prompt and the prompts offered by the :func:`raw_input` and
15
:func:`input` built-in functions.
19
On MacOS X the :mod:`readline` module can be implemented using
20
the ``libedit`` library instead of GNU readline.
22
The configuration file for ``libedit`` is different from that
23
of GNU readline. If you programmatically load configuration strings
24
you can check for the text "libedit" in :const:`readline.__doc__`
25
to differentiate between GNU readline and libedit.
28
The :mod:`readline` module defines the following functions:
31
.. function:: parse_and_bind(string)
33
Parse and execute single line of a readline init file.
36
.. function:: get_line_buffer()
38
Return the current contents of the line buffer.
41
.. function:: insert_text(string)
43
Insert text into the command line.
46
.. function:: read_init_file([filename])
48
Parse a readline initialization file. The default filename is the last filename
52
.. function:: read_history_file([filename])
54
Load a readline history file. The default filename is :file:`~/.history`.
57
.. function:: write_history_file([filename])
59
Save a readline history file. The default filename is :file:`~/.history`.
62
.. function:: clear_history()
64
Clear the current history. (Note: this function is not available if the
65
installed version of GNU readline doesn't support it.)
70
.. function:: get_history_length()
72
Return the desired length of the history file. Negative values imply unlimited
76
.. function:: set_history_length(length)
78
Set the number of lines to save in the history file. :func:`write_history_file`
79
uses this value to truncate the history file when saving. Negative values imply
80
unlimited history file size.
83
.. function:: get_current_history_length()
85
Return the number of lines currently in the history. (This is different from
86
:func:`get_history_length`, which returns the maximum number of lines that will
87
be written to a history file.)
92
.. function:: get_history_item(index)
94
Return the current contents of history item at *index*.
99
.. function:: remove_history_item(pos)
101
Remove history item specified by its position from the history.
103
.. versionadded:: 2.4
106
.. function:: replace_history_item(pos, line)
108
Replace history item specified by its position with the given line.
110
.. versionadded:: 2.4
113
.. function:: redisplay()
115
Change what's displayed on the screen to reflect the current contents of the
118
.. versionadded:: 2.3
121
.. function:: set_startup_hook([function])
123
Set or remove the startup_hook function. If *function* is specified, it will be
124
used as the new startup_hook function; if omitted or ``None``, any hook function
125
already installed is removed. The startup_hook function is called with no
126
arguments just before readline prints the first prompt.
129
.. function:: set_pre_input_hook([function])
131
Set or remove the pre_input_hook function. If *function* is specified, it will
132
be used as the new pre_input_hook function; if omitted or ``None``, any hook
133
function already installed is removed. The pre_input_hook function is called
134
with no arguments after the first prompt has been printed and just before
135
readline starts reading input characters.
138
.. function:: set_completer([function])
140
Set or remove the completer function. If *function* is specified, it will be
141
used as the new completer function; if omitted or ``None``, any completer
142
function already installed is removed. The completer function is called as
143
``function(text, state)``, for *state* in ``0``, ``1``, ``2``, ..., until it
144
returns a non-string value. It should return the next possible completion
145
starting with *text*.
148
.. function:: get_completer()
150
Get the completer function, or ``None`` if no completer function has been set.
152
.. versionadded:: 2.3
155
.. function:: get_completion_type()
157
Get the type of completion being attempted.
159
.. versionadded:: 2.6
161
.. function:: get_begidx()
163
Get the beginning index of the readline tab-completion scope.
166
.. function:: get_endidx()
168
Get the ending index of the readline tab-completion scope.
171
.. function:: set_completer_delims(string)
173
Set the readline word delimiters for tab-completion.
176
.. function:: get_completer_delims()
178
Get the readline word delimiters for tab-completion.
180
.. function:: set_completion_display_matches_hook([function])
182
Set or remove the completion display function. If *function* is
183
specified, it will be used as the new completion display function;
184
if omitted or ``None``, any completion display function already
185
installed is removed. The completion display function is called as
186
``function(substitution, [matches], longest_match_length)`` once
187
each time matches need to be displayed.
189
.. versionadded:: 2.6
191
.. function:: add_history(line)
193
Append a line to the history buffer, as if it was the last line typed.
197
Module :mod:`rlcompleter`
198
Completion of Python identifiers at the interactive prompt.
201
.. _readline-example:
206
The following example demonstrates how to use the :mod:`readline` module's
207
history reading and writing functions to automatically load and save a history
208
file named :file:`.pyhist` from the user's home directory. The code below would
209
normally be executed automatically during interactive sessions from the user's
210
:envvar:`PYTHONSTARTUP` file. ::
214
histfile = os.path.join(os.path.expanduser("~"), ".pyhist")
216
readline.read_history_file(histfile)
217
# default history len is -1 (infinite), which may grow unruly
218
readline.set_history_length(1000)
222
atexit.register(readline.write_history_file, histfile)
225
The following example extends the :class:`code.InteractiveConsole` class to
226
support history save/restore. ::
233
class HistoryConsole(code.InteractiveConsole):
234
def __init__(self, locals=None, filename="<console>",
235
histfile=os.path.expanduser("~/.console-history")):
236
code.InteractiveConsole.__init__(self, locals, filename)
237
self.init_history(histfile)
239
def init_history(self, histfile):
240
readline.parse_and_bind("tab: complete")
241
if hasattr(readline, "read_history_file"):
243
readline.read_history_file(histfile)
246
atexit.register(self.save_history, histfile)
248
def save_history(self, histfile):
249
readline.set_history_length(1000)
250
readline.write_history_file(histfile)