blob: 685b36f9c531bf02cf8e6d9a097d2a0dc63b1708 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`readline` --- GNU readline interface
2==========================================
3
4.. module:: readline
5 :platform: Unix
6 :synopsis: GNU readline support for Python.
Skip Montanaro54662462007-12-08 15:26:16 +00007.. sectionauthor:: Skip Montanaro <skip@pobox.com>
Georg Brandl8ec7f652007-08-15 14:28:01 +00008
9
10The :mod:`readline` module defines a number of functions to facilitate
11completion and reading/writing of history files from the Python interpreter.
12This module can be used directly or via the :mod:`rlcompleter` module. Settings
13made using this module affect the behaviour of both the interpreter's
14interactive prompt and the prompts offered by the :func:`raw_input` and
15:func:`input` built-in functions.
16
17The :mod:`readline` module defines the following functions:
18
19
20.. function:: parse_and_bind(string)
21
22 Parse and execute single line of a readline init file.
23
24
25.. function:: get_line_buffer()
26
27 Return the current contents of the line buffer.
28
29
30.. function:: insert_text(string)
31
32 Insert text into the command line.
33
34
35.. function:: read_init_file([filename])
36
37 Parse a readline initialization file. The default filename is the last filename
38 used.
39
40
41.. function:: read_history_file([filename])
42
43 Load a readline history file. The default filename is :file:`~/.history`.
44
45
46.. function:: write_history_file([filename])
47
48 Save a readline history file. The default filename is :file:`~/.history`.
49
50
51.. function:: clear_history()
52
53 Clear the current history. (Note: this function is not available if the
54 installed version of GNU readline doesn't support it.)
55
56 .. versionadded:: 2.4
57
58
59.. function:: get_history_length()
60
61 Return the desired length of the history file. Negative values imply unlimited
62 history file size.
63
64
65.. function:: set_history_length(length)
66
67 Set the number of lines to save in the history file. :func:`write_history_file`
68 uses this value to truncate the history file when saving. Negative values imply
69 unlimited history file size.
70
71
72.. function:: get_current_history_length()
73
74 Return the number of lines currently in the history. (This is different from
75 :func:`get_history_length`, which returns the maximum number of lines that will
76 be written to a history file.)
77
78 .. versionadded:: 2.3
79
80
81.. function:: get_history_item(index)
82
83 Return the current contents of history item at *index*.
84
85 .. versionadded:: 2.3
86
87
88.. function:: remove_history_item(pos)
89
90 Remove history item specified by its position from the history.
91
92 .. versionadded:: 2.4
93
94
95.. function:: replace_history_item(pos, line)
96
97 Replace history item specified by its position with the given line.
98
99 .. versionadded:: 2.4
100
101
102.. function:: redisplay()
103
104 Change what's displayed on the screen to reflect the current contents of the
105 line buffer.
106
107 .. versionadded:: 2.3
108
109
110.. function:: set_startup_hook([function])
111
112 Set or remove the startup_hook function. If *function* is specified, it will be
113 used as the new startup_hook function; if omitted or ``None``, any hook function
114 already installed is removed. The startup_hook function is called with no
115 arguments just before readline prints the first prompt.
116
117
118.. function:: set_pre_input_hook([function])
119
120 Set or remove the pre_input_hook function. If *function* is specified, it will
121 be used as the new pre_input_hook function; if omitted or ``None``, any hook
122 function already installed is removed. The pre_input_hook function is called
123 with no arguments after the first prompt has been printed and just before
124 readline starts reading input characters.
125
126
127.. function:: set_completer([function])
128
129 Set or remove the completer function. If *function* is specified, it will be
130 used as the new completer function; if omitted or ``None``, any completer
131 function already installed is removed. The completer function is called as
132 ``function(text, state)``, for *state* in ``0``, ``1``, ``2``, ..., until it
133 returns a non-string value. It should return the next possible completion
134 starting with *text*.
135
136
137.. function:: get_completer()
138
139 Get the completer function, or ``None`` if no completer function has been set.
140
141 .. versionadded:: 2.3
142
143
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000144.. function:: get_completion_type()
145
146 Get the type of completion being attempted.
147
148 .. versionadded:: 2.6
149
Georg Brandl8ec7f652007-08-15 14:28:01 +0000150.. function:: get_begidx()
151
152 Get the beginning index of the readline tab-completion scope.
153
154
155.. function:: get_endidx()
156
157 Get the ending index of the readline tab-completion scope.
158
159
160.. function:: set_completer_delims(string)
161
162 Set the readline word delimiters for tab-completion.
163
164
165.. function:: get_completer_delims()
166
167 Get the readline word delimiters for tab-completion.
168
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000169.. function:: set_completion_display_matches_hook([function])
170
171 Set or remove the completion display function. If *function* is
172 specified, it will be used as the new completion display function;
173 if omitted or ``None``, any completion display function already
174 installed is removed. The completion display function is called as
175 ``function(substitution, [matches], longest_match_length)`` once
176 each time matches need to be displayed.
177
178 .. versionadded:: 2.6
Georg Brandl8ec7f652007-08-15 14:28:01 +0000179
180.. function:: add_history(line)
181
182 Append a line to the history buffer, as if it was the last line typed.
183
184
185.. seealso::
186
187 Module :mod:`rlcompleter`
188 Completion of Python identifiers at the interactive prompt.
189
190
191.. _readline-example:
192
193Example
194-------
195
196The following example demonstrates how to use the :mod:`readline` module's
197history reading and writing functions to automatically load and save a history
198file named :file:`.pyhist` from the user's home directory. The code below would
199normally be executed automatically during interactive sessions from the user's
200:envvar:`PYTHONSTARTUP` file. ::
201
202 import os
203 histfile = os.path.join(os.environ["HOME"], ".pyhist")
204 try:
205 readline.read_history_file(histfile)
206 except IOError:
207 pass
208 import atexit
209 atexit.register(readline.write_history_file, histfile)
210 del os, histfile
211
212The following example extends the :class:`code.InteractiveConsole` class to
213support history save/restore. ::
214
215 import code
216 import readline
217 import atexit
218 import os
219
220 class HistoryConsole(code.InteractiveConsole):
221 def __init__(self, locals=None, filename="<console>",
222 histfile=os.path.expanduser("~/.console-history")):
Georg Brandlb29709a2009-09-16 09:24:57 +0000223 code.InteractiveConsole.__init__(self, locals, filename)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000224 self.init_history(histfile)
225
226 def init_history(self, histfile):
227 readline.parse_and_bind("tab: complete")
228 if hasattr(readline, "read_history_file"):
229 try:
230 readline.read_history_file(histfile)
231 except IOError:
232 pass
233 atexit.register(self.save_history, histfile)
234
235 def save_history(self, histfile):
236 readline.write_history_file(histfile)
237