| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="tut.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="tut.html" title='Python Tutorial' /> |
| 8 | <link rel='contents' href='node2.html' title="Contents" /> |
| 9 | <link rel='index' href='node19.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="node16.html" /> |
| 13 | <link rel="prev" href="node14.html" /> |
| 14 | <link rel="parent" href="tut.html" /> |
| 15 | <link rel="next" href="node16.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>A. Interactive Input Editing and History Substitution</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="12. What Now?" |
| 25 | href="node14.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="Python Tutorial" |
| 28 | href="tut.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="B. Floating Point Arithmetic:" |
| 31 | href="node16.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Tutorial</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="node2.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><img src='../icons/blank.png' |
| 38 | border='0' height='32' alt='' width='32' /></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="node19.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="node14.html">12. What Now?</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="tut.html">Python Tutorial</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="node16.html">B. Floating Point Arithmetic:</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | <div class='online-navigation'> |
| 55 | <!--Table of Child-Links--> |
| 56 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> |
| 57 | |
| 58 | <UL CLASS="ChildLinks"> |
| 59 | <LI><A href="node15.html#SECTION0015100000000000000000">A.1 Line Editing</a> |
| 60 | <LI><A href="node15.html#SECTION0015200000000000000000">A.2 History Substitution</a> |
| 61 | <LI><A href="node15.html#SECTION0015300000000000000000">A.3 Key Bindings</a> |
| 62 | <LI><A href="node15.html#SECTION0015400000000000000000">A.4 Commentary</a> |
| 63 | </ul> |
| 64 | <!--End of Table of Child-Links--> |
| 65 | </div> |
| 66 | <HR> |
| 67 | |
| 68 | <H1><A NAME="SECTION0015000000000000000000"></A><A NAME="interacting"></A> |
| 69 | <BR> |
| 70 | A. Interactive Input Editing and History Substitution |
| 71 | </H1> |
| 72 | |
| 73 | <P> |
| 74 | Some versions of the Python interpreter support editing of the current |
| 75 | input line and history substitution, similar to facilities found in |
| 76 | the Korn shell and the GNU Bash shell. This is implemented using the |
| 77 | <em>GNU Readline</em> library, which supports Emacs-style and vi-style |
| 78 | editing. This library has its own documentation which I won't |
| 79 | duplicate here; however, the basics are easily explained. The |
| 80 | interactive editing and history described here are optionally |
| 81 | available in the <span class="Unix">Unix</span> and Cygwin versions of the interpreter. |
| 82 | |
| 83 | <P> |
| 84 | This chapter does <em>not</em> document the editing facilities of Mark |
| 85 | Hammond's PythonWin package or the Tk-based environment, IDLE, |
| 86 | distributed with Python. The command line history recall which |
| 87 | operates within DOS boxes on NT and some other DOS and Windows flavors |
| 88 | is yet another beast. |
| 89 | |
| 90 | <P> |
| 91 | |
| 92 | <H1><A NAME="SECTION0015100000000000000000"></A><A NAME="lineEditing"></A> |
| 93 | <BR> |
| 94 | A.1 Line Editing |
| 95 | </H1> |
| 96 | |
| 97 | <P> |
| 98 | If supported, input line editing is active whenever the interpreter |
| 99 | prints a primary or secondary prompt. The current line can be edited |
| 100 | using the conventional Emacs control characters. The most important |
| 101 | of these are: <kbd>C-A</kbd> (Control-A) moves the cursor to the beginning |
| 102 | of the line, <kbd>C-E</kbd> to the end, <kbd>C-B</kbd> moves it one position to |
| 103 | the left, <kbd>C-F</kbd> to the right. Backspace erases the character to |
| 104 | the left of the cursor, <kbd>C-D</kbd> the character to its right. |
| 105 | <kbd>C-K</kbd> kills (erases) the rest of the line to the right of the |
| 106 | cursor, <kbd>C-Y</kbd> yanks back the last killed string. |
| 107 | <kbd>C-underscore</kbd> undoes the last change you made; it can be repeated |
| 108 | for cumulative effect. |
| 109 | |
| 110 | <P> |
| 111 | |
| 112 | <H1><A NAME="SECTION0015200000000000000000"></A><A NAME="history"></A> |
| 113 | <BR> |
| 114 | A.2 History Substitution |
| 115 | </H1> |
| 116 | |
| 117 | <P> |
| 118 | History substitution works as follows. All non-empty input lines |
| 119 | issued are saved in a history buffer, and when a new prompt is given |
| 120 | you are positioned on a new line at the bottom of this buffer. |
| 121 | <kbd>C-P</kbd> moves one line up (back) in the history buffer, |
| 122 | <kbd>C-N</kbd> moves one down. Any line in the history buffer can be |
| 123 | edited; an asterisk appears in front of the prompt to mark a line as |
| 124 | modified. Pressing the <kbd>Return</kbd> key passes the current line to |
| 125 | the interpreter. <kbd>C-R</kbd> starts an incremental reverse search; |
| 126 | <kbd>C-S</kbd> starts a forward search. |
| 127 | |
| 128 | <P> |
| 129 | |
| 130 | <H1><A NAME="SECTION0015300000000000000000"></A><A NAME="keyBindings"></A> |
| 131 | <BR> |
| 132 | A.3 Key Bindings |
| 133 | </H1> |
| 134 | |
| 135 | <P> |
| 136 | The key bindings and some other parameters of the Readline library can |
| 137 | be customized by placing commands in an initialization file called |
| 138 | <span class="file">~/.inputrc</span>. Key bindings have the form |
| 139 | |
| 140 | <P> |
| 141 | <div class="verbatim"><pre> |
| 142 | key-name: function-name |
| 143 | </pre></div> |
| 144 | |
| 145 | <P> |
| 146 | or |
| 147 | |
| 148 | <P> |
| 149 | <div class="verbatim"><pre> |
| 150 | "string": function-name |
| 151 | </pre></div> |
| 152 | |
| 153 | <P> |
| 154 | and options can be set with |
| 155 | |
| 156 | <P> |
| 157 | <div class="verbatim"><pre> |
| 158 | set option-name value |
| 159 | </pre></div> |
| 160 | |
| 161 | <P> |
| 162 | For example: |
| 163 | |
| 164 | <P> |
| 165 | <div class="verbatim"><pre> |
| 166 | # I prefer vi-style editing: |
| 167 | set editing-mode vi |
| 168 | |
| 169 | # Edit using a single line: |
| 170 | set horizontal-scroll-mode On |
| 171 | |
| 172 | # Rebind some keys: |
| 173 | Meta-h: backward-kill-word |
| 174 | "\C-u": universal-argument |
| 175 | "\C-x\C-r": re-read-init-file |
| 176 | </pre></div> |
| 177 | |
| 178 | <P> |
| 179 | Note that the default binding for <kbd>Tab</kbd> in Python is to insert a |
| 180 | <kbd>Tab</kbd> character instead of Readline's default filename completion |
| 181 | function. If you insist, you can override this by putting |
| 182 | |
| 183 | <P> |
| 184 | <div class="verbatim"><pre> |
| 185 | Tab: complete |
| 186 | </pre></div> |
| 187 | |
| 188 | <P> |
| 189 | in your <span class="file">~/.inputrc</span>. (Of course, this makes it harder to |
| 190 | type indented continuation lines if you're accustomed to using |
| 191 | <kbd>Tab</kbd> for that purpose.) |
| 192 | |
| 193 | <P> |
| 194 | Automatic completion of variable and module names is optionally |
| 195 | available. To enable it in the interpreter's interactive mode, add |
| 196 | the following to your startup file:<A NAME="tex2html6" |
| 197 | HREF="#foot1939"><SUP>A.1</SUP></A><a id='l2h-36' xml:id='l2h-36'></a> |
| 198 | |
| 199 | <P> |
| 200 | <div class="verbatim"><pre> |
| 201 | import rlcompleter, readline |
| 202 | readline.parse_and_bind('tab: complete') |
| 203 | </pre></div> |
| 204 | |
| 205 | <P> |
| 206 | This binds the <kbd>Tab</kbd> key to the completion function, so hitting |
| 207 | the <kbd>Tab</kbd> key twice suggests completions; it looks at Python |
| 208 | statement names, the current local variables, and the available module |
| 209 | names. For dotted expressions such as <code>string.a</code>, it will |
| 210 | evaluate the expression up to the final "<tt class="character">.</tt>" and then |
| 211 | suggest completions from the attributes of the resulting object. Note |
| 212 | that this may execute application-defined code if an object with a |
| 213 | <tt class="method">__getattr__()</tt> method is part of the expression. |
| 214 | |
| 215 | <P> |
| 216 | A more capable startup file might look like this example. Note that |
| 217 | this deletes the names it creates once they are no longer needed; this |
| 218 | is done since the startup file is executed in the same namespace as |
| 219 | the interactive commands, and removing the names avoids creating side |
| 220 | effects in the interactive environment. You may find it convenient |
| 221 | to keep some of the imported modules, such as |
| 222 | <a class="ulink" href="../lib/module-os.html" |
| 223 | ><tt class="module">os</tt></a>, which turn |
| 224 | out to be needed in most sessions with the interpreter. |
| 225 | |
| 226 | <P> |
| 227 | <div class="verbatim"><pre> |
| 228 | # Add auto-completion and a stored history file of commands to your Python |
| 229 | # interactive interpreter. Requires Python 2.0+, readline. Autocomplete is |
| 230 | # bound to the Esc key by default (you can change it - see readline docs). |
| 231 | # |
| 232 | # Store the file in ~/.pystartup, and set an environment variable to point |
| 233 | # to it: "export PYTHONSTARTUP=/max/home/itamar/.pystartup" in bash. |
| 234 | # |
| 235 | # Note that PYTHONSTARTUP does *not* expand "~", so you have to put in the |
| 236 | # full path to your home directory. |
| 237 | |
| 238 | import atexit |
| 239 | import os |
| 240 | import readline |
| 241 | import rlcompleter |
| 242 | |
| 243 | historyPath = os.path.expanduser("~/.pyhistory") |
| 244 | |
| 245 | def save_history(historyPath=historyPath): |
| 246 | import readline |
| 247 | readline.write_history_file(historyPath) |
| 248 | |
| 249 | if os.path.exists(historyPath): |
| 250 | readline.read_history_file(historyPath) |
| 251 | |
| 252 | atexit.register(save_history) |
| 253 | del os, atexit, readline, rlcompleter, save_history, historyPath |
| 254 | </pre></div> |
| 255 | |
| 256 | <P> |
| 257 | |
| 258 | <H1><A NAME="SECTION0015400000000000000000"></A><A NAME="commentary"></A> |
| 259 | <BR> |
| 260 | A.4 Commentary |
| 261 | </H1> |
| 262 | |
| 263 | <P> |
| 264 | This facility is an enormous step forward compared to earlier versions |
| 265 | of the interpreter; however, some wishes are left: It would be nice if |
| 266 | the proper indentation were suggested on continuation lines (the |
| 267 | parser knows if an indent token is required next). The completion |
| 268 | mechanism might use the interpreter's symbol table. A command to |
| 269 | check (or even suggest) matching parentheses, quotes, etc., would also |
| 270 | be useful. |
| 271 | |
| 272 | <P> |
| 273 | <BR><HR><H4>Footnotes</H4> |
| 274 | <DL> |
| 275 | <DT><A NAME="foot1939">... file:</A><A |
| 276 | HREF="node15.html#tex2html6"><SUP>A.1</SUP></A></DT> |
| 277 | <DD> |
| 278 | Python will execute the contents of a file identified by the |
| 279 | <a class="envvar" id='l2h-35' xml:id='l2h-35'>PYTHONSTARTUP</a> environment variable when you start an |
| 280 | interactive interpreter. |
| 281 | |
| 282 | </DD> |
| 283 | </DL> |
| 284 | <DIV CLASS="navigation"> |
| 285 | <div class='online-navigation'> |
| 286 | <p></p><hr /> |
| 287 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 288 | <tr> |
| 289 | <td class='online-navigation'><a rel="prev" title="12. What Now?" |
| 290 | href="node14.html"><img src='../icons/previous.png' |
| 291 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 292 | <td class='online-navigation'><a rel="parent" title="Python Tutorial" |
| 293 | href="tut.html"><img src='../icons/up.png' |
| 294 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 295 | <td class='online-navigation'><a rel="next" title="B. Floating Point Arithmetic:" |
| 296 | href="node16.html"><img src='../icons/next.png' |
| 297 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 298 | <td align="center" width="100%">Python Tutorial</td> |
| 299 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 300 | href="node2.html"><img src='../icons/contents.png' |
| 301 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 302 | <td class='online-navigation'><img src='../icons/blank.png' |
| 303 | border='0' height='32' alt='' width='32' /></td> |
| 304 | <td class='online-navigation'><a rel="index" title="Index" |
| 305 | href="node19.html"><img src='../icons/index.png' |
| 306 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 307 | </tr></table> |
| 308 | <div class='online-navigation'> |
| 309 | <b class="navlabel">Previous:</b> |
| 310 | <a class="sectref" rel="prev" href="node14.html">12. What Now?</A> |
| 311 | <b class="navlabel">Up:</b> |
| 312 | <a class="sectref" rel="parent" href="tut.html">Python Tutorial</A> |
| 313 | <b class="navlabel">Next:</b> |
| 314 | <a class="sectref" rel="next" href="node16.html">B. Floating Point Arithmetic:</A> |
| 315 | </div> |
| 316 | </div> |
| 317 | <hr /> |
| 318 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 319 | </DIV> |
| 320 | <!--End of Navigation Panel--> |
| 321 | <ADDRESS> |
| 322 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 323 | </ADDRESS> |
| 324 | </BODY> |
| 325 | </HTML> |