blob: b59f680c064daa12722e8cac00ee66eab6c6d2d1 [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.
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
Georg Brandl96593ed2007-09-07 14:15:41 +000014interactive prompt and the prompts offered by the built-in :func:`input`
15function.
Georg Brandl116aa622007-08-15 14:28:22 +000016
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
Georg Brandl116aa622007-08-15 14:28:22 +000056
57.. function:: get_history_length()
58
59 Return the desired length of the history file. Negative values imply unlimited
60 history file size.
61
62
63.. function:: set_history_length(length)
64
65 Set the number of lines to save in the history file. :func:`write_history_file`
66 uses this value to truncate the history file when saving. Negative values imply
67 unlimited history file size.
68
69
70.. function:: get_current_history_length()
71
72 Return the number of lines currently in the history. (This is different from
73 :func:`get_history_length`, which returns the maximum number of lines that will
74 be written to a history file.)
75
Georg Brandl116aa622007-08-15 14:28:22 +000076
77.. function:: get_history_item(index)
78
79 Return the current contents of history item at *index*.
80
Georg Brandl116aa622007-08-15 14:28:22 +000081
82.. function:: remove_history_item(pos)
83
84 Remove history item specified by its position from the history.
85
Georg Brandl116aa622007-08-15 14:28:22 +000086
87.. function:: replace_history_item(pos, line)
88
89 Replace history item specified by its position with the given line.
90
Georg Brandl116aa622007-08-15 14:28:22 +000091
92.. function:: redisplay()
93
94 Change what's displayed on the screen to reflect the current contents of the
95 line buffer.
96
Georg Brandl116aa622007-08-15 14:28:22 +000097
98.. function:: set_startup_hook([function])
99
100 Set or remove the startup_hook function. If *function* is specified, it will be
101 used as the new startup_hook function; if omitted or ``None``, any hook function
102 already installed is removed. The startup_hook function is called with no
103 arguments just before readline prints the first prompt.
104
105
106.. function:: set_pre_input_hook([function])
107
108 Set or remove the pre_input_hook function. If *function* is specified, it will
109 be used as the new pre_input_hook function; if omitted or ``None``, any hook
110 function already installed is removed. The pre_input_hook function is called
111 with no arguments after the first prompt has been printed and just before
112 readline starts reading input characters.
113
114
115.. function:: set_completer([function])
116
117 Set or remove the completer function. If *function* is specified, it will be
118 used as the new completer function; if omitted or ``None``, any completer
119 function already installed is removed. The completer function is called as
120 ``function(text, state)``, for *state* in ``0``, ``1``, ``2``, ..., until it
121 returns a non-string value. It should return the next possible completion
122 starting with *text*.
123
124
125.. function:: get_completer()
126
127 Get the completer function, or ``None`` if no completer function has been set.
128
Georg Brandl116aa622007-08-15 14:28:22 +0000129
Thomas Wouters89d996e2007-09-08 17:39:28 +0000130.. function:: get_completion_type()
131
132 Get the type of completion being attempted.
133
Thomas Wouters89d996e2007-09-08 17:39:28 +0000134
Georg Brandl116aa622007-08-15 14:28:22 +0000135.. function:: get_begidx()
136
137 Get the beginning index of the readline tab-completion scope.
138
139
140.. function:: get_endidx()
141
142 Get the ending index of the readline tab-completion scope.
143
144
145.. function:: set_completer_delims(string)
146
147 Set the readline word delimiters for tab-completion.
148
149
150.. function:: get_completer_delims()
151
152 Get the readline word delimiters for tab-completion.
153
Georg Brandl6554cb92007-12-02 23:15:43 +0000154
Thomas Wouters89d996e2007-09-08 17:39:28 +0000155.. function:: set_completion_display_matches_hook([function])
156
157 Set or remove the completion display function. If *function* is
158 specified, it will be used as the new completion display function;
159 if omitted or ``None``, any completion display function already
160 installed is removed. The completion display function is called as
161 ``function(substitution, [matches], longest_match_length)`` once
162 each time matches need to be displayed.
163
Georg Brandl116aa622007-08-15 14:28:22 +0000164
165.. function:: add_history(line)
166
167 Append a line to the history buffer, as if it was the last line typed.
168
169
170.. seealso::
171
172 Module :mod:`rlcompleter`
173 Completion of Python identifiers at the interactive prompt.
174
175
176.. _readline-example:
177
178Example
179-------
180
181The following example demonstrates how to use the :mod:`readline` module's
182history reading and writing functions to automatically load and save a history
183file named :file:`.pyhist` from the user's home directory. The code below would
184normally be executed automatically during interactive sessions from the user's
185:envvar:`PYTHONSTARTUP` file. ::
186
187 import os
188 histfile = os.path.join(os.environ["HOME"], ".pyhist")
189 try:
190 readline.read_history_file(histfile)
191 except IOError:
192 pass
193 import atexit
194 atexit.register(readline.write_history_file, histfile)
195 del os, histfile
196
197The following example extends the :class:`code.InteractiveConsole` class to
198support history save/restore. ::
199
200 import code
201 import readline
202 import atexit
203 import os
204
205 class HistoryConsole(code.InteractiveConsole):
206 def __init__(self, locals=None, filename="<console>",
207 histfile=os.path.expanduser("~/.console-history")):
Georg Brandlee8783d2009-09-16 16:00:31 +0000208 code.InteractiveConsole.__init__(self, locals, filename)
Georg Brandl116aa622007-08-15 14:28:22 +0000209 self.init_history(histfile)
210
211 def init_history(self, histfile):
212 readline.parse_and_bind("tab: complete")
213 if hasattr(readline, "read_history_file"):
214 try:
215 readline.read_history_file(histfile)
216 except IOError:
217 pass
218 atexit.register(self.save_history, histfile)
219
220 def save_history(self, histfile):
221 readline.write_history_file(histfile)
222