blob: dec37b67ea9c991d979eb991fa08f2cf094ac076 [file] [log] [blame]
Skip Montanaro510ca1d2000-07-06 03:25:26 +00001\section{\module{readline} ---
Fred Drake3c62d9e2000-07-06 04:51:04 +00002 GNU readline interface}
Skip Montanaro510ca1d2000-07-06 03:25:26 +00003
4\declaremodule{builtin}{readline}
Fred Drake3c62d9e2000-07-06 04:51:04 +00005 \platform{Unix}
Skip Montanaro510ca1d2000-07-06 03:25:26 +00006\sectionauthor{Skip Montanaro}{skip@mojam.com}
Fred Drakef8ca7d82000-10-10 17:03:45 +00007\modulesynopsis{GNU readline support for Python.}
Skip Montanaro510ca1d2000-07-06 03:25:26 +00008
Skip Montanaro510ca1d2000-07-06 03:25:26 +00009
Andrew M. Kuchlingdafb1e52006-07-29 14:21:15 +000010The \module{readline} module defines a number of functions to
11facilitate completion and reading/writing of history files from the
12Python interpreter. This module can be used directly or via the
13\refmodule{rlcompleter} module. Settings made using
14this module affect the behaviour of both the interpreter's interactive prompt
15and the prompts offered by the \function{raw_input()} and \function{input()}
16built-in functions.
Skip Montanaro510ca1d2000-07-06 03:25:26 +000017
18The \module{readline} module defines the following functions:
19
Fred Drake3c62d9e2000-07-06 04:51:04 +000020
Skip Montanaro510ca1d2000-07-06 03:25:26 +000021\begin{funcdesc}{parse_and_bind}{string}
22Parse and execute single line of a readline init file.
23\end{funcdesc}
24
25\begin{funcdesc}{get_line_buffer}{}
26Return the current contents of the line buffer.
27\end{funcdesc}
28
29\begin{funcdesc}{insert_text}{string}
30Insert text into the command line.
31\end{funcdesc}
32
33\begin{funcdesc}{read_init_file}{\optional{filename}}
34Parse a readline initialization file.
35The default filename is the last filename used.
36\end{funcdesc}
37
38\begin{funcdesc}{read_history_file}{\optional{filename}}
39Load a readline history file.
Fred Drake3c62d9e2000-07-06 04:51:04 +000040The default filename is \file{\~{}/.history}.
Skip Montanaro510ca1d2000-07-06 03:25:26 +000041\end{funcdesc}
42
43\begin{funcdesc}{write_history_file}{\optional{filename}}
44Save a readline history file.
Fred Drake3c62d9e2000-07-06 04:51:04 +000045The default filename is \file{\~{}/.history}.
Skip Montanaro510ca1d2000-07-06 03:25:26 +000046\end{funcdesc}
47
Martin v. Löwise7a97962003-09-20 16:08:33 +000048\begin{funcdesc}{clear_history}{}
49Clear the current history. (Note: this function is not available if
50the installed version of GNU readline doesn't support it.)
51\versionadded{2.4}
52\end{funcdesc}
53
Skip Montanaro7cb15242000-07-19 16:56:26 +000054\begin{funcdesc}{get_history_length}{}
55Return the desired length of the history file. Negative values imply
56unlimited history file size.
57\end{funcdesc}
58
59\begin{funcdesc}{set_history_length}{length}
60Set the number of lines to save in the history file.
Fred Drake3fe9a982000-08-09 14:37:05 +000061\function{write_history_file()} uses this value to truncate the
62history file when saving. Negative values imply unlimited history
63file size.
Skip Montanaro7cb15242000-07-19 16:56:26 +000064\end{funcdesc}
65
Phillip J. Eby5068c872004-05-04 19:20:22 +000066\begin{funcdesc}{get_current_history_length}{}
67Return the number of lines currently in the history. (This is different
68from \function{get_history_length()}, which returns the maximum number of
69lines that will be written to a history file.) \versionadded{2.3}
70\end{funcdesc}
71
72\begin{funcdesc}{get_history_item}{index}
73Return the current contents of history item at \var{index}.
74\versionadded{2.3}
75\end{funcdesc}
76
Skip Montanaroe5069012004-08-15 14:32:06 +000077\begin{funcdesc}{remove_history_item}{pos}
78Remove history item specified by its position from the history.
79\versionadded{2.4}
80\end{funcdesc}
81
82\begin{funcdesc}{replace_history_item}{pos, line}
83Replace history item specified by its position with the given line.
84\versionadded{2.4}
85\end{funcdesc}
86
Phillip J. Eby5068c872004-05-04 19:20:22 +000087\begin{funcdesc}{redisplay}{}
88Change what's displayed on the screen to reflect the current contents
89of the line buffer. \versionadded{2.3}
90\end{funcdesc}
91
Martin v. Löwis0daad592001-09-30 21:09:59 +000092\begin{funcdesc}{set_startup_hook}{\optional{function}}
93Set or remove the startup_hook function. If \var{function} is specified,
94it will be used as the new startup_hook function; if omitted or
95\code{None}, any hook function already installed is removed. The
96startup_hook function is called with no arguments just
97before readline prints the first prompt.
98\end{funcdesc}
99
100\begin{funcdesc}{set_pre_input_hook}{\optional{function}}
101Set or remove the pre_input_hook function. If \var{function} is specified,
102it will be used as the new pre_input_hook function; if omitted or
103\code{None}, any hook function already installed is removed. The
104pre_input_hook function is called with no arguments after the first prompt
105has been printed and just before readline starts reading input characters.
106\end{funcdesc}
107
Skip Montanaro510ca1d2000-07-06 03:25:26 +0000108\begin{funcdesc}{set_completer}{\optional{function}}
Fred Drake905dc552001-08-01 21:42:45 +0000109Set or remove the completer function. If \var{function} is specified,
110it will be used as the new completer function; if omitted or
111\code{None}, any completer function already installed is removed. The
112completer function is called as \code{\var{function}(\var{text},
113\var{state})}, for \var{state} in \code{0}, \code{1}, \code{2}, ...,
114until it returns a non-string value. It should return the next
115possible completion starting with \var{text}.
Skip Montanaro510ca1d2000-07-06 03:25:26 +0000116\end{funcdesc}
117
Neal Norwitzb7d1d3c2003-02-21 18:57:05 +0000118\begin{funcdesc}{get_completer}{}
Michael W. Hudsonf5dd7532003-02-21 20:11:09 +0000119Get the completer function, or \code{None} if no completer function
120has been set. \versionadded{2.3}
Neal Norwitzb7d1d3c2003-02-21 18:57:05 +0000121\end{funcdesc}
122
Skip Montanaro510ca1d2000-07-06 03:25:26 +0000123\begin{funcdesc}{get_begidx}{}
124Get the beginning index of the readline tab-completion scope.
125\end{funcdesc}
126
127\begin{funcdesc}{get_endidx}{}
128Get the ending index of the readline tab-completion scope.
129\end{funcdesc}
130
131\begin{funcdesc}{set_completer_delims}{string}
132Set the readline word delimiters for tab-completion.
133\end{funcdesc}
134
135\begin{funcdesc}{get_completer_delims}{}
136Get the readline word delimiters for tab-completion.
137\end{funcdesc}
138
Guido van Rossumb6c1d522001-10-19 01:18:43 +0000139\begin{funcdesc}{add_history}{line}
140Append a line to the history buffer, as if it was the last line typed.
141\end{funcdesc}
142
Skip Montanaro510ca1d2000-07-06 03:25:26 +0000143\begin{seealso}
Fred Drake3c62d9e2000-07-06 04:51:04 +0000144 \seemodule{rlcompleter}{Completion of Python identifiers at the
145 interactive prompt.}
Skip Montanaro510ca1d2000-07-06 03:25:26 +0000146\end{seealso}
Fred Drake3c62d9e2000-07-06 04:51:04 +0000147
148
149\subsection{Example \label{readline-example}}
150
151The following example demonstrates how to use the
152\module{readline} module's history reading and writing functions to
153automatically load and save a history file named \file{.pyhist} from
154the user's home directory. The code below would normally be executed
155automatically during interactive sessions from the user's
156\envvar{PYTHONSTARTUP} file.
157
158\begin{verbatim}
159import os
160histfile = os.path.join(os.environ["HOME"], ".pyhist")
161try:
162 readline.read_history_file(histfile)
163except IOError:
164 pass
165import atexit
166atexit.register(readline.write_history_file, histfile)
167del os, histfile
168\end{verbatim}
Skip Montanaro0dc23102004-05-23 17:46:50 +0000169
Skip Montanarob98a8ba2004-05-23 19:06:41 +0000170The following example extends the \class{code.InteractiveConsole} class to
Skip Montanaro79cddc52004-05-24 14:20:16 +0000171support history save/restore.
Skip Montanarob98a8ba2004-05-23 19:06:41 +0000172
173\begin{verbatim}
174import code
175import readline
176import atexit
177import os
178
179class HistoryConsole(code.InteractiveConsole):
180 def __init__(self, locals=None, filename="<console>",
181 histfile=os.path.expanduser("~/.console-history")):
182 code.InteractiveConsole.__init__(self)
183 self.init_history(histfile)
184
185 def init_history(self, histfile):
186 readline.parse_and_bind("tab: complete")
187 if hasattr(readline, "read_history_file"):
188 try:
189 readline.read_history_file(histfile)
190 except IOError:
191 pass
192 atexit.register(self.save_history, histfile)
193
Skip Montanarob98a8ba2004-05-23 19:06:41 +0000194 def save_history(self, histfile):
195 readline.write_history_file(histfile)
196\end{verbatim}