<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>A. Interactive Input Editing and History Substitution
</TITLE>
<META NAME="description" CONTENT="A. Interactive Input Editing and History Substitution
">
<META NAME="keywords" CONTENT="tut">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="tut.css">
<LINK REL="next" HREF="node14.html">
<LINK REL="previous" HREF="node12.html">
<LINK REL="up" HREF="tut.html">
<LINK REL="next" HREF="node14.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="node14.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="tut.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="node12.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Tutorial</b></td>
<td><A HREF="node2.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="../icons/blank.gif"></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="../icons/blank.gif"></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="node14.html">About this document ...</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="tut.html">Python Tutorial</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="node12.html">10. What Now?</A></span>
<br><hr></div>
<!--End of Navigation Panel-->
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></A>

<UL>
<LI><A NAME="tex2html350"
 HREF="node13.html#SECTION0013100000000000000000">A1. Line Editing </A>
<LI><A NAME="tex2html351"
 HREF="node13.html#SECTION0013200000000000000000">A2. History Substitution </A>
<LI><A NAME="tex2html352"
 HREF="node13.html#SECTION0013300000000000000000">A3. Key Bindings </A>
<LI><A NAME="tex2html353"
 HREF="node13.html#SECTION0013400000000000000000">A4. Commentary </A>
</UL>
<!--End of Table of Child-Links-->
<HR>

<H1><A NAME="SECTION0013000000000000000000"></A>
<BR>
A. Interactive Input Editing and History Substitution
         
</H1>

<P>
Some versions of the Python interpreter support editing of the current
input line and history substitution, similar to facilities found in
the Korn shell and the GNU Bash shell.  This is implemented using the
<i>GNU Readline</i> library, which supports Emacs-style and vi-style
editing.  This library has its own documentation which I won't
duplicate here; however, the basics are easily explained.  The
interactive editing and history described here are optionally
available in the Unix and CygWin versions of the interpreter.

<P>
This chapter does <i>not</i> document the editing facilities of Mark
Hammond's PythonWin package or the Tk-based environment, IDLE,
distributed with Python.  The command line history recall which
operates within DOS boxes on NT and some other DOS and Windows flavors 
is yet another beast.

<P>

<H1><A NAME="SECTION0013100000000000000000"></A>
<BR>
A1. Line Editing 
</H1>

<P>
If supported, input line editing is active whenever the interpreter
prints a primary or secondary prompt.  The current line can be edited
using the conventional Emacs control characters.  The most important
of these are: C-A (Control-A) moves the cursor to the beginning of the
line, C-E to the end, C-B moves it one position to the left, C-F to
the right.  Backspace erases the character to the left of the cursor,
C-D the character to its right.  C-K kills (erases) the rest of the
line to the right of the cursor, C-Y yanks back the last killed
string.  C-underscore undoes the last change you made; it can be
repeated for cumulative effect.

<P>

<H1><A NAME="SECTION0013200000000000000000"></A>
<BR>
A2. History Substitution 
</H1>

<P>
History substitution works as follows.  All non-empty input lines
issued are saved in a history buffer, and when a new prompt is given
you are positioned on a new line at the bottom of this buffer.  C-P
moves one line up (back) in the history buffer, C-N moves one down.
Any line in the history buffer can be edited; an asterisk appears in
front of the prompt to mark a line as modified.  Pressing the Return
key passes the current line to the interpreter.  C-R starts an
incremental reverse search; C-S starts a forward search.

<P>

<H1><A NAME="SECTION0013300000000000000000"></A>
<BR>
A3. Key Bindings 
</H1>

<P>
The key bindings and some other parameters of the Readline library can
be customized by placing commands in an initialization file called
<tt class=file>$HOME/.inputrc</tt>.  Key bindings have the form

<P>
<dl><dd><pre class=verbatim>
key-name: function-name
</pre></dl>

<P>
or

<P>
<dl><dd><pre class=verbatim>
"string": function-name
</pre></dl>

<P>
and options can be set with

<P>
<dl><dd><pre class=verbatim>
set option-name value
</pre></dl>

<P>
For example:

<P>
<dl><dd><pre class=verbatim>
# I prefer vi-style editing:
set editing-mode vi
# Edit using a single line:
set horizontal-scroll-mode On
# Rebind some keys:
Meta-h: backward-kill-word
"\C-u": universal-argument
"\C-x\C-r": re-read-init-file
</pre></dl>

<P>
Note that the default binding for TAB in Python is to insert a TAB
instead of Readline's default filename completion function.  If you
insist, you can override this by putting

<P>
<dl><dd><pre class=verbatim>
TAB: complete
</pre></dl>

<P>
in your <tt class=file>$HOME/.inputrc</tt>.  (Of course, this makes it hard to type
indented continuation lines...)

<P>
Automatic completion of variable and module names is optionally
available.  To enable it in the interpreter's interactive mode, add
the following to your <tt class=file>$HOME/.pythonrc.py</tt> file:<a name="l2h-21"></a>
<a name="l2h-22"></a>
<a name="l2h-23"></a>

<P>
<dl><dd><pre class=verbatim>
import rlcompleter, readline
readline.parse_and_bind('tab: complete')
</pre></dl>

<P>
This binds the TAB key to the completion function, so hitting the TAB
key twice suggests completions; it looks at Python statement names,
the current local variables, and the available module names.  For
dotted expressions such as <tt>string.a</tt>, it will evaluate the the
expression up to the final "<tt class=character>.</tt>" and then suggest completions
from the attributes of the resulting object.  Note that this may
execute application-defined code if an object with a
<tt class=method>__getattr__()</tt> method is part of the expression.

<P>

<H1><A NAME="SECTION0013400000000000000000"></A>
<BR>
A4. Commentary 
</H1>

<P>
This facility is an enormous step forward compared to previous
versions of the interpreter; however, some wishes are left: It would
be nice if the proper indentation were suggested on continuation lines
(the parser knows if an indent token is required next).  The
completion mechanism might use the interpreter's symbol table.  A
command to check (or even suggest) matching parentheses, quotes etc.
would also be useful.

<P>
<p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="node14.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="tut.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="node12.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Tutorial</b></td>
<td><A HREF="node2.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="../icons/blank.gif"></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="../icons/blank.gif"></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="node14.html">About this document ...</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="tut.html">Python Tutorial</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="node12.html">10. What Now?</A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
