blob: 42e0ad56a5e273769740a0b1a3166f01a20a2cb7 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`readline` --- GNU readline interface
2==========================================
3
4.. module:: readline
5 :platform: Unix
6 :synopsis: GNU readline support for Python.
Christian Heimes895627f2007-12-08 17:28:33 +00007.. sectionauthor:: Skip Montanaro <skip@pobox.com>
Georg Brandl116aa622007-08-15 14:28:22 +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 Panter0f767392016-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 Brandl116aa622007-08-15 14:28:22 +000014made using this module affect the behaviour of both the interpreter's
Georg Brandl96593ed2007-09-07 14:15:41 +000015interactive prompt and the prompts offered by the built-in :func:`input`
16function.
Georg Brandl116aa622007-08-15 14:28:22 +000017
Ezio Melotti6e40e272010-01-04 09:29:10 +000018.. note::
Ronald Oussoren2efd9242009-09-20 14:53:22 +000019
Martin Panter0f767392016-04-05 07:37:22 +000020 The underlying Readline library API may be implemented by
Ronald Oussoren2efd9242009-09-20 14:53:22 +000021 the ``libedit`` library instead of GNU readline.
Martin Panter0f767392016-04-05 07:37:22 +000022 On MacOS X the :mod:`readline` module detects which library is being used
23 at run time.
Ronald Oussoren2efd9242009-09-20 14:53:22 +000024
25 The configuration file for ``libedit`` is different from that
Georg Brandl6faee4e2010-09-21 14:48:28 +000026 of GNU readline. If you programmatically load configuration strings
Ronald Oussoren2efd9242009-09-20 14:53:22 +000027 you can check for the text "libedit" in :const:`readline.__doc__`
28 to differentiate between GNU readline and libedit.
29
30
Martin Panter0f767392016-04-05 07:37:22 +000031Init file
32---------
33
34The following functions relate to the init file and user configuration:
Georg Brandl116aa622007-08-15 14:28:22 +000035
36
37.. function:: parse_and_bind(string)
38
Martin Panter0f767392016-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 Brandl116aa622007-08-15 14:28:22 +000041
42
43.. function:: read_init_file([filename])
44
Martin Panter0f767392016-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 Brandl116aa622007-08-15 14:28:22 +000078
79
80.. function:: read_history_file([filename])
81
Martin Panter0f767392016-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 Brandl116aa622007-08-15 14:28:22 +000085
86
87.. function:: write_history_file([filename])
88
Martin Panter0f767392016-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.
Georg Brandl116aa622007-08-15 14:28:22 +000092
93
Benjamin Peterson33f8f152014-11-26 13:58:16 -060094.. function:: append_history_file(nelements[, filename])
95
Martin Panter0f767392016-04-05 07:37:22 +000096 Append the last *nelements* items of history to a file. The default filename is
97 :file:`~/.history`. The file must already exist. This calls
98 :c:func:`append_history` in the underlying library.
Benjamin Peterson33f8f152014-11-26 13:58:16 -060099
100 .. versionadded:: 3.5
101
102
Martin Panter0f767392016-04-05 07:37:22 +0000103.. function:: get_history_length()
104 set_history_length(length)
105
106 Set or return the desired number of lines to save in the history file.
107 The :func:`write_history_file` function uses this value to truncate
108 the history file, by calling :c:func:`history_truncate_file` in
109 the underlying library. Negative values imply
110 unlimited history file size.
111
112
113History list
114------------
115
116The following functions operate on a global history list:
117
118
Georg Brandl116aa622007-08-15 14:28:22 +0000119.. function:: clear_history()
120
Martin Panter0f767392016-04-05 07:37:22 +0000121 Clear the current history. This calls :c:func:`clear_history` in the
122 underlying library. The Python function only exists if Python was
123 compiled for a version of the library that supports it.
Georg Brandl116aa622007-08-15 14:28:22 +0000124
125
126.. function:: get_current_history_length()
127
Martin Panter0f767392016-04-05 07:37:22 +0000128 Return the number of items currently in the history. (This is different from
Georg Brandl116aa622007-08-15 14:28:22 +0000129 :func:`get_history_length`, which returns the maximum number of lines that will
130 be written to a history file.)
131
Georg Brandl116aa622007-08-15 14:28:22 +0000132
133.. function:: get_history_item(index)
134
Martin Panter0f767392016-04-05 07:37:22 +0000135 Return the current contents of history item at *index*. The item index
136 is one-based. This calls :c:func:`history_get` in the underlying library.
Georg Brandl116aa622007-08-15 14:28:22 +0000137
Georg Brandl116aa622007-08-15 14:28:22 +0000138
139.. function:: remove_history_item(pos)
140
141 Remove history item specified by its position from the history.
Martin Panter0f767392016-04-05 07:37:22 +0000142 The position is zero-based. This calls :c:func:`remove_history` in
143 the underlying library.
Georg Brandl116aa622007-08-15 14:28:22 +0000144
Georg Brandl116aa622007-08-15 14:28:22 +0000145
146.. function:: replace_history_item(pos, line)
147
Martin Panter0f767392016-04-05 07:37:22 +0000148 Replace history item specified by its position with *line*.
149 The position is zero-based. This calls :c:func:`replace_history_entry`
150 in the underlying library.
Georg Brandl116aa622007-08-15 14:28:22 +0000151
Georg Brandl116aa622007-08-15 14:28:22 +0000152
Martin Panter0f767392016-04-05 07:37:22 +0000153.. function:: add_history(line)
Georg Brandl116aa622007-08-15 14:28:22 +0000154
Martin Panter0f767392016-04-05 07:37:22 +0000155 Append *line* to the history buffer, as if it was the last line typed.
156 This calls :c:func:`add_history` in the underlying library.
157
158
159Startup hooks
160-------------
Georg Brandl116aa622007-08-15 14:28:22 +0000161
Georg Brandl116aa622007-08-15 14:28:22 +0000162
163.. function:: set_startup_hook([function])
164
Martin Panter0f767392016-04-05 07:37:22 +0000165 Set or remove the function invoked by the :c:data:`rl_startup_hook`
166 callback of the underlying library. If *function* is specified, it will
167 be used as the new hook function; if omitted or ``None``, any function
168 already installed is removed. The hook is called with no
Georg Brandl116aa622007-08-15 14:28:22 +0000169 arguments just before readline prints the first prompt.
170
171
172.. function:: set_pre_input_hook([function])
173
Martin Panter0f767392016-04-05 07:37:22 +0000174 Set or remove the function invoked by the :c:data:`rl_pre_input_hook`
175 callback of the underlying library. If *function* is specified, it will
176 be used as the new hook function; if omitted or ``None``, any
177 function already installed is removed. The hook is called
Georg Brandl116aa622007-08-15 14:28:22 +0000178 with no arguments after the first prompt has been printed and just before
179 readline starts reading input characters.
180
181
Martin Panter0f767392016-04-05 07:37:22 +0000182Completion
183----------
184
185The following functions relate to implementing a custom word completion
186function. This is typically operated by the Tab key, and can suggest and
187automatically complete a word being typed. By default, Readline is set up
188to be used by :mod:`rlcompleter` to complete Python identifiers for
189the interactive interpreter. If the :mod:`readline` module is to be used
190with a custom completer, a different set of word delimiters should be set.
191
192
Georg Brandl116aa622007-08-15 14:28:22 +0000193.. function:: set_completer([function])
194
195 Set or remove the completer function. If *function* is specified, it will be
196 used as the new completer function; if omitted or ``None``, any completer
197 function already installed is removed. The completer function is called as
198 ``function(text, state)``, for *state* in ``0``, ``1``, ``2``, ..., until it
199 returns a non-string value. It should return the next possible completion
200 starting with *text*.
201
Martin Panter0f767392016-04-05 07:37:22 +0000202 The installed completer function is invoked by the *entry_func* callback
203 passed to :c:func:`rl_completion_matches` in the underlying library.
204 The *text* string comes from the first parameter to the
205 :c:data:`rl_attempted_completion_function` callback of the
206 underlying library.
207
Georg Brandl116aa622007-08-15 14:28:22 +0000208
209.. function:: get_completer()
210
211 Get the completer function, or ``None`` if no completer function has been set.
212
Georg Brandl116aa622007-08-15 14:28:22 +0000213
Thomas Wouters89d996e2007-09-08 17:39:28 +0000214.. function:: get_completion_type()
215
Martin Panter0f767392016-04-05 07:37:22 +0000216 Get the type of completion being attempted. This returns the
217 :c:data:`rl_completion_type` variable in the underlying library as
218 an integer.
Thomas Wouters89d996e2007-09-08 17:39:28 +0000219
Thomas Wouters89d996e2007-09-08 17:39:28 +0000220
Georg Brandl116aa622007-08-15 14:28:22 +0000221.. function:: get_begidx()
Martin Panter0f767392016-04-05 07:37:22 +0000222 get_endidx()
Georg Brandl116aa622007-08-15 14:28:22 +0000223
Martin Panter0f767392016-04-05 07:37:22 +0000224 Get the beginning or ending index of the completion scope.
225 These indexes are the *start* and *end* arguments passed to the
226 :c:data:`rl_attempted_completion_function` callback of the
227 underlying library.
Georg Brandl116aa622007-08-15 14:28:22 +0000228
229
230.. function:: set_completer_delims(string)
Martin Panter0f767392016-04-05 07:37:22 +0000231 get_completer_delims()
Georg Brandl116aa622007-08-15 14:28:22 +0000232
Martin Panter0f767392016-04-05 07:37:22 +0000233 Set or get the word delimiters for completion. These determine the
234 start of the word to be considered for completion (the completion scope).
235 These functions access the :c:data:`rl_completer_word_break_characters`
236 variable in the underlying library.
Georg Brandl116aa622007-08-15 14:28:22 +0000237
Georg Brandl6554cb92007-12-02 23:15:43 +0000238
Thomas Wouters89d996e2007-09-08 17:39:28 +0000239.. function:: set_completion_display_matches_hook([function])
240
241 Set or remove the completion display function. If *function* is
242 specified, it will be used as the new completion display function;
243 if omitted or ``None``, any completion display function already
Martin Panter0f767392016-04-05 07:37:22 +0000244 installed is removed. This sets or clears the
245 :c:data:`rl_completion_display_matches_hook` callback in the
246 underlying library. The completion display function is called as
Thomas Wouters89d996e2007-09-08 17:39:28 +0000247 ``function(substitution, [matches], longest_match_length)`` once
248 each time matches need to be displayed.
249
Georg Brandl116aa622007-08-15 14:28:22 +0000250
Georg Brandl116aa622007-08-15 14:28:22 +0000251.. _readline-example:
252
253Example
254-------
255
256The following example demonstrates how to use the :mod:`readline` module's
257history reading and writing functions to automatically load and save a history
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200258file named :file:`.python_history` from the user's home directory. The code
259below would normally be executed automatically during interactive sessions
260from the user's :envvar:`PYTHONSTARTUP` file. ::
Georg Brandl116aa622007-08-15 14:28:22 +0000261
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200262 import atexit
Georg Brandl116aa622007-08-15 14:28:22 +0000263 import os
Georg Brandla102ae32010-10-06 05:08:32 +0000264 import readline
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200265
266 histfile = os.path.join(os.path.expanduser("~"), ".python_history")
Georg Brandl116aa622007-08-15 14:28:22 +0000267 try:
268 readline.read_history_file(histfile)
Ezio Melotti7c018aa2016-01-11 23:30:56 +0200269 # default history len is -1 (infinite), which may grow unruly
270 readline.set_history_length(1000)
Antoine Pitrou62ab10a02011-10-12 20:10:51 +0200271 except FileNotFoundError:
Georg Brandl116aa622007-08-15 14:28:22 +0000272 pass
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200273
Georg Brandl116aa622007-08-15 14:28:22 +0000274 atexit.register(readline.write_history_file, histfile)
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200275
276This code is actually automatically run when Python is run in
277:ref:`interactive mode <tut-interactive>` (see :ref:`rlcompleter-config`).
Georg Brandl116aa622007-08-15 14:28:22 +0000278
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600279The following example achieves the same goal but supports concurrent interactive
280sessions, by only appending the new history. ::
281
282 import atexit
283 import os
Berker Peksag964ec8b2015-11-01 00:55:12 +0300284 import readline
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600285 histfile = os.path.join(os.path.expanduser("~"), ".python_history")
286
287 try:
288 readline.read_history_file(histfile)
289 h_len = readline.get_history_length()
290 except FileNotFoundError:
291 open(histfile, 'wb').close()
292 h_len = 0
293
294 def save(prev_h_len, histfile):
295 new_h_len = readline.get_history_length()
Ezio Melotti7c018aa2016-01-11 23:30:56 +0200296 readline.set_history_length(1000)
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600297 readline.append_history_file(new_h_len - prev_h_len, histfile)
298 atexit.register(save, h_len, histfile)
299
Georg Brandl116aa622007-08-15 14:28:22 +0000300The following example extends the :class:`code.InteractiveConsole` class to
301support history save/restore. ::
302
Georg Brandl116aa622007-08-15 14:28:22 +0000303 import atexit
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200304 import code
Georg Brandl116aa622007-08-15 14:28:22 +0000305 import os
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200306 import readline
Georg Brandl116aa622007-08-15 14:28:22 +0000307
308 class HistoryConsole(code.InteractiveConsole):
309 def __init__(self, locals=None, filename="<console>",
310 histfile=os.path.expanduser("~/.console-history")):
Georg Brandlee8783d2009-09-16 16:00:31 +0000311 code.InteractiveConsole.__init__(self, locals, filename)
Georg Brandl116aa622007-08-15 14:28:22 +0000312 self.init_history(histfile)
313
314 def init_history(self, histfile):
315 readline.parse_and_bind("tab: complete")
316 if hasattr(readline, "read_history_file"):
317 try:
318 readline.read_history_file(histfile)
Antoine Pitrou62ab10a02011-10-12 20:10:51 +0200319 except FileNotFoundError:
Georg Brandl116aa622007-08-15 14:28:22 +0000320 pass
321 atexit.register(self.save_history, histfile)
322
323 def save_history(self, histfile):
Ezio Melotti7c018aa2016-01-11 23:30:56 +0200324 readline.set_history_length(1000)
Georg Brandl116aa622007-08-15 14:28:22 +0000325 readline.write_history_file(histfile)