blob: d7e7dc6c2c94ff33c5753a57aff081a4cc124304 [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.
Martin Panteraad86a62016-04-05 07:37:22 +000012This module can be used directly, or via the :mod:`rlcompleter` module, which
13supports completion of Python identifiers at the interactive prompt. Settings
Georg Brandl8ec7f652007-08-15 14:28:01 +000014made using this module affect the behaviour of both the interpreter's
15interactive prompt and the prompts offered by the :func:`raw_input` and
16:func:`input` built-in functions.
17
Ezio Melotti81982562010-01-04 09:00:11 +000018.. note::
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +000019
Martin Panteraad86a62016-04-05 07:37:22 +000020 The underlying Readline library API may be implemented by
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +000021 the ``libedit`` library instead of GNU readline.
Martin Panteraad86a62016-04-05 07:37:22 +000022 On MacOS X the :mod:`readline` module detects which library is being used
23 at run time.
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +000024
25 The configuration file for ``libedit`` is different from that
Georg Brandl09302282010-10-06 09:32:48 +000026 of GNU readline. If you programmatically load configuration strings
Ronald Oussoren9f20d9d2009-09-20 14:18:15 +000027 you can check for the text "libedit" in :const:`readline.__doc__`
28 to differentiate between GNU readline and libedit.
29
30
Martin Panteraad86a62016-04-05 07:37:22 +000031Init file
32---------
33
34The following functions relate to the init file and user configuration:
Georg Brandl8ec7f652007-08-15 14:28:01 +000035
36
37.. function:: parse_and_bind(string)
38
Martin Panteraad86a62016-04-05 07:37:22 +000039 Execute the init line provided in the *string* argument. This calls
40 :c:func:`rl_parse_and_bind` in the underlying library.
Georg Brandl8ec7f652007-08-15 14:28:01 +000041
42
43.. function:: read_init_file([filename])
44
Martin Panteraad86a62016-04-05 07:37:22 +000045 Execute a readline initialization file. The default filename is the last filename
46 used. This calls :c:func:`rl_read_init_file` in the underlying library.
47
48
49Line buffer
50-----------
51
52The following functions operate on the line buffer:
53
54
55.. function:: get_line_buffer()
56
57 Return the current contents of the line buffer (:c:data:`rl_line_buffer`
58 in the underlying library).
59
60
61.. function:: insert_text(string)
62
63 Insert text into the line buffer at the cursor position. This calls
64 :c:func:`rl_insert_text` in the underlying library, but ignores
65 the return value.
66
67
68.. function:: redisplay()
69
70 Change what's displayed on the screen to reflect the current contents of the
71 line buffer. This calls :c:func:`rl_redisplay` in the underlying library.
72
73
74History file
75------------
76
77The following functions operate on a history file:
Georg Brandl8ec7f652007-08-15 14:28:01 +000078
79
80.. function:: read_history_file([filename])
81
Martin Panteraad86a62016-04-05 07:37:22 +000082 Load a readline history file, and append it to the history list.
83 The default filename is :file:`~/.history`. This calls
84 :c:func:`read_history` in the underlying library.
Georg Brandl8ec7f652007-08-15 14:28:01 +000085
86
87.. function:: write_history_file([filename])
88
Martin Panteraad86a62016-04-05 07:37:22 +000089 Save the history list to a readline history file, overwriting any
90 existing file. The default filename is :file:`~/.history`. This calls
91 :c:func:`write_history` in the underlying library.
92
93
94.. function:: get_history_length()
95 set_history_length(length)
96
97 Set or return the desired number of lines to save in the history file.
98 The :func:`write_history_file` function uses this value to truncate
99 the history file, by calling :c:func:`history_truncate_file` in
100 the underlying library. Negative values imply
101 unlimited history file size.
102
103
104History list
105------------
106
107The following functions operate on a global history list:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000108
109
110.. function:: clear_history()
111
Martin Panteraad86a62016-04-05 07:37:22 +0000112 Clear the current history. This calls :c:func:`clear_history` in the
113 underlying library. The Python function only exists if Python was
114 compiled for a version of the library that supports it.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000115
116 .. versionadded:: 2.4
117
118
Georg Brandl8ec7f652007-08-15 14:28:01 +0000119.. function:: get_current_history_length()
120
Martin Panteraad86a62016-04-05 07:37:22 +0000121 Return the number of items currently in the history. (This is different from
Georg Brandl8ec7f652007-08-15 14:28:01 +0000122 :func:`get_history_length`, which returns the maximum number of lines that will
123 be written to a history file.)
124
125 .. versionadded:: 2.3
126
127
128.. function:: get_history_item(index)
129
Martin Panteraad86a62016-04-05 07:37:22 +0000130 Return the current contents of history item at *index*. The item index
131 is one-based. This calls :c:func:`history_get` in the underlying library.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000132
133 .. versionadded:: 2.3
134
135
136.. function:: remove_history_item(pos)
137
138 Remove history item specified by its position from the history.
Martin Panteraad86a62016-04-05 07:37:22 +0000139 The position is zero-based. This calls :c:func:`remove_history` in
140 the underlying library.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000141
142 .. versionadded:: 2.4
143
144
145.. function:: replace_history_item(pos, line)
146
Martin Panteraad86a62016-04-05 07:37:22 +0000147 Replace history item specified by its position with *line*.
148 The position is zero-based. This calls :c:func:`replace_history_entry`
149 in the underlying library.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000150
151 .. versionadded:: 2.4
152
153
Martin Panteraad86a62016-04-05 07:37:22 +0000154.. function:: add_history(line)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000155
Martin Panteraad86a62016-04-05 07:37:22 +0000156 Append *line* to the history buffer, as if it was the last line typed.
157 This calls :c:func:`add_history` in the underlying library.
158
159
160Startup hooks
161-------------
Georg Brandl8ec7f652007-08-15 14:28:01 +0000162
163 .. versionadded:: 2.3
164
165
166.. function:: set_startup_hook([function])
167
Martin Panteraad86a62016-04-05 07:37:22 +0000168 Set or remove the function invoked by the :c:data:`rl_startup_hook`
169 callback of the underlying library. If *function* is specified, it will
170 be used as the new hook function; if omitted or ``None``, any function
171 already installed is removed. The hook is called with no
Georg Brandl8ec7f652007-08-15 14:28:01 +0000172 arguments just before readline prints the first prompt.
173
174
175.. function:: set_pre_input_hook([function])
176
Martin Panteraad86a62016-04-05 07:37:22 +0000177 Set or remove the function invoked by the :c:data:`rl_pre_input_hook`
178 callback of the underlying library. If *function* is specified, it will
179 be used as the new hook function; if omitted or ``None``, any
180 function already installed is removed. The hook is called
Georg Brandl8ec7f652007-08-15 14:28:01 +0000181 with no arguments after the first prompt has been printed and just before
182 readline starts reading input characters.
183
184
Martin Panteraad86a62016-04-05 07:37:22 +0000185Completion
186----------
187
188The following functions relate to implementing a custom word completion
189function. This is typically operated by the Tab key, and can suggest and
190automatically complete a word being typed. By default, Readline is set up
191to be used by :mod:`rlcompleter` to complete Python identifiers for
192the interactive interpreter. If the :mod:`readline` module is to be used
193with a custom completer, a different set of word delimiters should be set.
194
195
Georg Brandl8ec7f652007-08-15 14:28:01 +0000196.. function:: set_completer([function])
197
198 Set or remove the completer function. If *function* is specified, it will be
199 used as the new completer function; if omitted or ``None``, any completer
200 function already installed is removed. The completer function is called as
201 ``function(text, state)``, for *state* in ``0``, ``1``, ``2``, ..., until it
202 returns a non-string value. It should return the next possible completion
203 starting with *text*.
204
Martin Panteraad86a62016-04-05 07:37:22 +0000205 The installed completer function is invoked by the *entry_func* callback
206 passed to :c:func:`rl_completion_matches` in the underlying library.
207 The *text* string comes from the first parameter to the
208 :c:data:`rl_attempted_completion_function` callback of the
209 underlying library.
210
Georg Brandl8ec7f652007-08-15 14:28:01 +0000211
212.. function:: get_completer()
213
214 Get the completer function, or ``None`` if no completer function has been set.
215
216 .. versionadded:: 2.3
217
218
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000219.. function:: get_completion_type()
220
Martin Panteraad86a62016-04-05 07:37:22 +0000221 Get the type of completion being attempted. This returns the
222 :c:data:`rl_completion_type` variable in the underlying library as
223 an integer.
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000224
225 .. versionadded:: 2.6
226
Georg Brandl8ec7f652007-08-15 14:28:01 +0000227.. function:: get_begidx()
Martin Panteraad86a62016-04-05 07:37:22 +0000228 get_endidx()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000229
Martin Panteraad86a62016-04-05 07:37:22 +0000230 Get the beginning or ending index of the completion scope.
231 These indexes are the *start* and *end* arguments passed to the
232 :c:data:`rl_attempted_completion_function` callback of the
233 underlying library.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000234
235
236.. function:: set_completer_delims(string)
Martin Panteraad86a62016-04-05 07:37:22 +0000237 get_completer_delims()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000238
Martin Panteraad86a62016-04-05 07:37:22 +0000239 Set or get the word delimiters for completion. These determine the
240 start of the word to be considered for completion (the completion scope).
241 These functions access the :c:data:`rl_completer_word_break_characters`
242 variable in the underlying library.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000243
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000244.. function:: set_completion_display_matches_hook([function])
245
246 Set or remove the completion display function. If *function* is
247 specified, it will be used as the new completion display function;
248 if omitted or ``None``, any completion display function already
Martin Panteraad86a62016-04-05 07:37:22 +0000249 installed is removed. This sets or clears the
250 :c:data:`rl_completion_display_matches_hook` callback in the
251 underlying library. The completion display function is called as
Martin v. Löwis58bd49f2007-09-04 13:13:14 +0000252 ``function(substitution, [matches], longest_match_length)`` once
253 each time matches need to be displayed.
254
255 .. versionadded:: 2.6
Georg Brandl8ec7f652007-08-15 14:28:01 +0000256
Georg Brandl8ec7f652007-08-15 14:28:01 +0000257.. _readline-example:
258
259Example
260-------
261
262The following example demonstrates how to use the :mod:`readline` module's
263history reading and writing functions to automatically load and save a history
264file named :file:`.pyhist` from the user's home directory. The code below would
265normally be executed automatically during interactive sessions from the user's
266:envvar:`PYTHONSTARTUP` file. ::
267
268 import os
Georg Brandldb235c12010-10-06 09:33:55 +0000269 import readline
Éric Araujo8bea9a52011-03-26 01:24:47 +0100270 histfile = os.path.join(os.path.expanduser("~"), ".pyhist")
Georg Brandl8ec7f652007-08-15 14:28:01 +0000271 try:
272 readline.read_history_file(histfile)
Ezio Melotti167c3362016-01-11 23:30:15 +0200273 # default history len is -1 (infinite), which may grow unruly
274 readline.set_history_length(1000)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000275 except IOError:
276 pass
277 import atexit
278 atexit.register(readline.write_history_file, histfile)
279 del os, histfile
280
281The following example extends the :class:`code.InteractiveConsole` class to
282support history save/restore. ::
283
284 import code
285 import readline
286 import atexit
287 import os
288
289 class HistoryConsole(code.InteractiveConsole):
290 def __init__(self, locals=None, filename="<console>",
291 histfile=os.path.expanduser("~/.console-history")):
Georg Brandlb29709a2009-09-16 09:24:57 +0000292 code.InteractiveConsole.__init__(self, locals, filename)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000293 self.init_history(histfile)
294
295 def init_history(self, histfile):
296 readline.parse_and_bind("tab: complete")
297 if hasattr(readline, "read_history_file"):
298 try:
299 readline.read_history_file(histfile)
300 except IOError:
301 pass
302 atexit.register(self.save_history, histfile)
303
304 def save_history(self, histfile):
Ezio Melotti167c3362016-01-11 23:30:15 +0200305 readline.set_history_length(1000)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000306 readline.write_history_file(histfile)
307