blob: 54c54b461ce7e43ed006829a04b10dd5ad104601 [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.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007
Christian Heimes895627f2007-12-08 17:28:33 +00008.. sectionauthor:: Skip Montanaro <skip@pobox.com>
Georg Brandl116aa622007-08-15 14:28:22 +00009
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040010--------------
Georg Brandl116aa622007-08-15 14:28:22 +000011
12The :mod:`readline` module defines a number of functions to facilitate
13completion and reading/writing of history files from the Python interpreter.
Martin Panter0f767392016-04-05 07:37:22 +000014This module can be used directly, or via the :mod:`rlcompleter` module, which
15supports completion of Python identifiers at the interactive prompt. Settings
Georg Brandl116aa622007-08-15 14:28:22 +000016made using this module affect the behaviour of both the interpreter's
Georg Brandl96593ed2007-09-07 14:15:41 +000017interactive prompt and the prompts offered by the built-in :func:`input`
18function.
Georg Brandl116aa622007-08-15 14:28:22 +000019
Ezio Melotti6e40e272010-01-04 09:29:10 +000020.. note::
Ronald Oussoren2efd9242009-09-20 14:53:22 +000021
Martin Panter0f767392016-04-05 07:37:22 +000022 The underlying Readline library API may be implemented by
Ronald Oussoren2efd9242009-09-20 14:53:22 +000023 the ``libedit`` library instead of GNU readline.
Martin Panter0f767392016-04-05 07:37:22 +000024 On MacOS X the :mod:`readline` module detects which library is being used
25 at run time.
Ronald Oussoren2efd9242009-09-20 14:53:22 +000026
27 The configuration file for ``libedit`` is different from that
Georg Brandl6faee4e2010-09-21 14:48:28 +000028 of GNU readline. If you programmatically load configuration strings
Ronald Oussoren2efd9242009-09-20 14:53:22 +000029 you can check for the text "libedit" in :const:`readline.__doc__`
30 to differentiate between GNU readline and libedit.
31
Martin Panter553245c2016-06-10 00:27:46 +000032Readline keybindings may be configured via an initialization file, typically
33``.inputrc`` in your home directory. See `Readline Init File
34<https://cnswww.cns.cwru.edu/php/chet/readline/rluserman.html#SEC9>`_
35in the GNU Readline manual for information about the format and
36allowable constructs of that file, and the capabilities of the
37Readline library in general.
38
Ronald Oussoren2efd9242009-09-20 14:53:22 +000039
Martin Panter0f767392016-04-05 07:37:22 +000040Init file
41---------
42
43The following functions relate to the init file and user configuration:
Georg Brandl116aa622007-08-15 14:28:22 +000044
45
46.. function:: parse_and_bind(string)
47
Martin Panter0f767392016-04-05 07:37:22 +000048 Execute the init line provided in the *string* argument. This calls
49 :c:func:`rl_parse_and_bind` in the underlying library.
Georg Brandl116aa622007-08-15 14:28:22 +000050
51
52.. function:: read_init_file([filename])
53
Martin Panter0f767392016-04-05 07:37:22 +000054 Execute a readline initialization file. The default filename is the last filename
55 used. This calls :c:func:`rl_read_init_file` in the underlying library.
56
57
58Line buffer
59-----------
60
61The following functions operate on the line buffer:
62
63
64.. function:: get_line_buffer()
65
66 Return the current contents of the line buffer (:c:data:`rl_line_buffer`
67 in the underlying library).
68
69
70.. function:: insert_text(string)
71
72 Insert text into the line buffer at the cursor position. This calls
73 :c:func:`rl_insert_text` in the underlying library, but ignores
74 the return value.
75
76
77.. function:: redisplay()
78
79 Change what's displayed on the screen to reflect the current contents of the
80 line buffer. This calls :c:func:`rl_redisplay` in the underlying library.
81
82
83History file
84------------
85
86The following functions operate on a history file:
Georg Brandl116aa622007-08-15 14:28:22 +000087
88
89.. function:: read_history_file([filename])
90
Martin Panter0f767392016-04-05 07:37:22 +000091 Load a readline history file, and append it to the history list.
92 The default filename is :file:`~/.history`. This calls
93 :c:func:`read_history` in the underlying library.
Georg Brandl116aa622007-08-15 14:28:22 +000094
95
96.. function:: write_history_file([filename])
97
Martin Panter0f767392016-04-05 07:37:22 +000098 Save the history list to a readline history file, overwriting any
99 existing file. The default filename is :file:`~/.history`. This calls
100 :c:func:`write_history` in the underlying library.
Georg Brandl116aa622007-08-15 14:28:22 +0000101
102
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600103.. function:: append_history_file(nelements[, filename])
104
Martin Panter0f767392016-04-05 07:37:22 +0000105 Append the last *nelements* items of history to a file. The default filename is
106 :file:`~/.history`. The file must already exist. This calls
Martin Panter6afbc652016-06-14 08:45:43 +0000107 :c:func:`append_history` in the underlying library. This function
108 only exists if Python was compiled for a version of the library
109 that supports it.
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600110
111 .. versionadded:: 3.5
112
113
Martin Panter0f767392016-04-05 07:37:22 +0000114.. function:: get_history_length()
115 set_history_length(length)
116
117 Set or return the desired number of lines to save in the history file.
118 The :func:`write_history_file` function uses this value to truncate
119 the history file, by calling :c:func:`history_truncate_file` in
120 the underlying library. Negative values imply
121 unlimited history file size.
122
123
124History list
125------------
126
127The following functions operate on a global history list:
128
129
Georg Brandl116aa622007-08-15 14:28:22 +0000130.. function:: clear_history()
131
Martin Panter0f767392016-04-05 07:37:22 +0000132 Clear the current history. This calls :c:func:`clear_history` in the
133 underlying library. The Python function only exists if Python was
134 compiled for a version of the library that supports it.
Georg Brandl116aa622007-08-15 14:28:22 +0000135
136
137.. function:: get_current_history_length()
138
Martin Panter0f767392016-04-05 07:37:22 +0000139 Return the number of items currently in the history. (This is different from
Georg Brandl116aa622007-08-15 14:28:22 +0000140 :func:`get_history_length`, which returns the maximum number of lines that will
141 be written to a history file.)
142
Georg Brandl116aa622007-08-15 14:28:22 +0000143
144.. function:: get_history_item(index)
145
Martin Panter0f767392016-04-05 07:37:22 +0000146 Return the current contents of history item at *index*. The item index
147 is one-based. This calls :c:func:`history_get` in the underlying library.
Georg Brandl116aa622007-08-15 14:28:22 +0000148
Georg Brandl116aa622007-08-15 14:28:22 +0000149
150.. function:: remove_history_item(pos)
151
152 Remove history item specified by its position from the history.
Martin Panter0f767392016-04-05 07:37:22 +0000153 The position is zero-based. This calls :c:func:`remove_history` in
154 the underlying library.
Georg Brandl116aa622007-08-15 14:28:22 +0000155
Georg Brandl116aa622007-08-15 14:28:22 +0000156
157.. function:: replace_history_item(pos, line)
158
Martin Panter0f767392016-04-05 07:37:22 +0000159 Replace history item specified by its position with *line*.
160 The position is zero-based. This calls :c:func:`replace_history_entry`
161 in the underlying library.
Georg Brandl116aa622007-08-15 14:28:22 +0000162
Georg Brandl116aa622007-08-15 14:28:22 +0000163
Martin Panter0f767392016-04-05 07:37:22 +0000164.. function:: add_history(line)
Georg Brandl116aa622007-08-15 14:28:22 +0000165
Martin Panter0f767392016-04-05 07:37:22 +0000166 Append *line* to the history buffer, as if it was the last line typed.
167 This calls :c:func:`add_history` in the underlying library.
168
169
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000170.. function:: set_auto_history(enabled)
171
172 Enable or disable automatic calls to :c:func:`add_history` when reading
173 input via readline. The *enabled* argument should be a Boolean value
Serhiy Storchaka7d6dda42016-10-19 18:36:51 +0300174 that when true, enables auto history, and that when false, disables
Martin Panterf0dbf7a2016-05-15 01:26:25 +0000175 auto history.
176
177 .. versionadded:: 3.6
178
179 .. impl-detail::
180 Auto history is enabled by default, and changes to this do not persist
181 across multiple sessions.
182
183
Martin Panter0f767392016-04-05 07:37:22 +0000184Startup hooks
185-------------
Georg Brandl116aa622007-08-15 14:28:22 +0000186
Georg Brandl116aa622007-08-15 14:28:22 +0000187
188.. function:: set_startup_hook([function])
189
Martin Panter0f767392016-04-05 07:37:22 +0000190 Set or remove the function invoked by the :c:data:`rl_startup_hook`
191 callback of the underlying library. If *function* is specified, it will
192 be used as the new hook function; if omitted or ``None``, any function
193 already installed is removed. The hook is called with no
Georg Brandl116aa622007-08-15 14:28:22 +0000194 arguments just before readline prints the first prompt.
195
196
197.. function:: set_pre_input_hook([function])
198
Martin Panter0f767392016-04-05 07:37:22 +0000199 Set or remove the function invoked by the :c:data:`rl_pre_input_hook`
200 callback of the underlying library. If *function* is specified, it will
201 be used as the new hook function; if omitted or ``None``, any
202 function already installed is removed. The hook is called
Georg Brandl116aa622007-08-15 14:28:22 +0000203 with no arguments after the first prompt has been printed and just before
Martin Panter6afbc652016-06-14 08:45:43 +0000204 readline starts reading input characters. This function only exists
205 if Python was compiled for a version of the library that supports it.
Georg Brandl116aa622007-08-15 14:28:22 +0000206
207
Martin Panter0f767392016-04-05 07:37:22 +0000208Completion
209----------
210
211The following functions relate to implementing a custom word completion
212function. This is typically operated by the Tab key, and can suggest and
213automatically complete a word being typed. By default, Readline is set up
214to be used by :mod:`rlcompleter` to complete Python identifiers for
215the interactive interpreter. If the :mod:`readline` module is to be used
216with a custom completer, a different set of word delimiters should be set.
217
218
Georg Brandl116aa622007-08-15 14:28:22 +0000219.. function:: set_completer([function])
220
221 Set or remove the completer function. If *function* is specified, it will be
222 used as the new completer function; if omitted or ``None``, any completer
223 function already installed is removed. The completer function is called as
224 ``function(text, state)``, for *state* in ``0``, ``1``, ``2``, ..., until it
225 returns a non-string value. It should return the next possible completion
226 starting with *text*.
227
Martin Panter0f767392016-04-05 07:37:22 +0000228 The installed completer function is invoked by the *entry_func* callback
229 passed to :c:func:`rl_completion_matches` in the underlying library.
230 The *text* string comes from the first parameter to the
231 :c:data:`rl_attempted_completion_function` callback of the
232 underlying library.
233
Georg Brandl116aa622007-08-15 14:28:22 +0000234
235.. function:: get_completer()
236
237 Get the completer function, or ``None`` if no completer function has been set.
238
Georg Brandl116aa622007-08-15 14:28:22 +0000239
Thomas Wouters89d996e2007-09-08 17:39:28 +0000240.. function:: get_completion_type()
241
Martin Panter0f767392016-04-05 07:37:22 +0000242 Get the type of completion being attempted. This returns the
243 :c:data:`rl_completion_type` variable in the underlying library as
244 an integer.
Thomas Wouters89d996e2007-09-08 17:39:28 +0000245
Thomas Wouters89d996e2007-09-08 17:39:28 +0000246
Georg Brandl116aa622007-08-15 14:28:22 +0000247.. function:: get_begidx()
Martin Panter0f767392016-04-05 07:37:22 +0000248 get_endidx()
Georg Brandl116aa622007-08-15 14:28:22 +0000249
Martin Panter0f767392016-04-05 07:37:22 +0000250 Get the beginning or ending index of the completion scope.
251 These indexes are the *start* and *end* arguments passed to the
252 :c:data:`rl_attempted_completion_function` callback of the
253 underlying library.
Georg Brandl116aa622007-08-15 14:28:22 +0000254
255
256.. function:: set_completer_delims(string)
Martin Panter0f767392016-04-05 07:37:22 +0000257 get_completer_delims()
Georg Brandl116aa622007-08-15 14:28:22 +0000258
Martin Panter0f767392016-04-05 07:37:22 +0000259 Set or get the word delimiters for completion. These determine the
260 start of the word to be considered for completion (the completion scope).
261 These functions access the :c:data:`rl_completer_word_break_characters`
262 variable in the underlying library.
Georg Brandl116aa622007-08-15 14:28:22 +0000263
Georg Brandl6554cb92007-12-02 23:15:43 +0000264
Thomas Wouters89d996e2007-09-08 17:39:28 +0000265.. function:: set_completion_display_matches_hook([function])
266
267 Set or remove the completion display function. If *function* is
268 specified, it will be used as the new completion display function;
269 if omitted or ``None``, any completion display function already
Martin Panter0f767392016-04-05 07:37:22 +0000270 installed is removed. This sets or clears the
271 :c:data:`rl_completion_display_matches_hook` callback in the
272 underlying library. The completion display function is called as
Thomas Wouters89d996e2007-09-08 17:39:28 +0000273 ``function(substitution, [matches], longest_match_length)`` once
274 each time matches need to be displayed.
275
Georg Brandl116aa622007-08-15 14:28:22 +0000276
Georg Brandl116aa622007-08-15 14:28:22 +0000277.. _readline-example:
278
279Example
280-------
281
282The following example demonstrates how to use the :mod:`readline` module's
283history reading and writing functions to automatically load and save a history
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200284file named :file:`.python_history` from the user's home directory. The code
285below would normally be executed automatically during interactive sessions
286from the user's :envvar:`PYTHONSTARTUP` file. ::
Georg Brandl116aa622007-08-15 14:28:22 +0000287
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200288 import atexit
Georg Brandl116aa622007-08-15 14:28:22 +0000289 import os
Georg Brandla102ae32010-10-06 05:08:32 +0000290 import readline
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200291
292 histfile = os.path.join(os.path.expanduser("~"), ".python_history")
Georg Brandl116aa622007-08-15 14:28:22 +0000293 try:
294 readline.read_history_file(histfile)
Ezio Melotti7c018aa2016-01-11 23:30:56 +0200295 # default history len is -1 (infinite), which may grow unruly
296 readline.set_history_length(1000)
Antoine Pitrou62ab10a02011-10-12 20:10:51 +0200297 except FileNotFoundError:
Georg Brandl116aa622007-08-15 14:28:22 +0000298 pass
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200299
Georg Brandl116aa622007-08-15 14:28:22 +0000300 atexit.register(readline.write_history_file, histfile)
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200301
302This code is actually automatically run when Python is run in
303:ref:`interactive mode <tut-interactive>` (see :ref:`rlcompleter-config`).
Georg Brandl116aa622007-08-15 14:28:22 +0000304
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600305The following example achieves the same goal but supports concurrent interactive
306sessions, by only appending the new history. ::
307
308 import atexit
309 import os
Berker Peksag964ec8b2015-11-01 00:55:12 +0300310 import readline
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600311 histfile = os.path.join(os.path.expanduser("~"), ".python_history")
312
313 try:
314 readline.read_history_file(histfile)
315 h_len = readline.get_history_length()
316 except FileNotFoundError:
317 open(histfile, 'wb').close()
318 h_len = 0
319
320 def save(prev_h_len, histfile):
321 new_h_len = readline.get_history_length()
Ezio Melotti7c018aa2016-01-11 23:30:56 +0200322 readline.set_history_length(1000)
Benjamin Peterson33f8f152014-11-26 13:58:16 -0600323 readline.append_history_file(new_h_len - prev_h_len, histfile)
324 atexit.register(save, h_len, histfile)
325
Georg Brandl116aa622007-08-15 14:28:22 +0000326The following example extends the :class:`code.InteractiveConsole` class to
327support history save/restore. ::
328
Georg Brandl116aa622007-08-15 14:28:22 +0000329 import atexit
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200330 import code
Georg Brandl116aa622007-08-15 14:28:22 +0000331 import os
Antoine Pitrou1a6cb302013-05-04 20:08:35 +0200332 import readline
Georg Brandl116aa622007-08-15 14:28:22 +0000333
334 class HistoryConsole(code.InteractiveConsole):
335 def __init__(self, locals=None, filename="<console>",
336 histfile=os.path.expanduser("~/.console-history")):
Georg Brandlee8783d2009-09-16 16:00:31 +0000337 code.InteractiveConsole.__init__(self, locals, filename)
Georg Brandl116aa622007-08-15 14:28:22 +0000338 self.init_history(histfile)
339
340 def init_history(self, histfile):
341 readline.parse_and_bind("tab: complete")
342 if hasattr(readline, "read_history_file"):
343 try:
344 readline.read_history_file(histfile)
Antoine Pitrou62ab10a02011-10-12 20:10:51 +0200345 except FileNotFoundError:
Georg Brandl116aa622007-08-15 14:28:22 +0000346 pass
347 atexit.register(self.save_history, histfile)
348
349 def save_history(self, histfile):
Ezio Melotti7c018aa2016-01-11 23:30:56 +0200350 readline.set_history_length(1000)
Georg Brandl116aa622007-08-15 14:28:22 +0000351 readline.write_history_file(histfile)