[IPython-dev] Multi-line history entries

Fernando Perez Fernando.Perez at colorado.edu
Mon Jul 24 01:37:44 CDT 2006


Hi Jorgen,

Jorgen Cederlof wrote:

> Hi,
> 
> I tried subscribing to the mailing list to send this, but I didn't get
> a confirmation mail. Well, I can just as well send this to you
> instead.

Thanks for the patch.  I've forwarded it to python-dev, so the trunk 
maintainers can have a look and commit it.  They work much more with the shell 
side of things than I do these days, so they're in a better position to 
evaluate it.


> 
> I recently started using IPython, and it is a great enhancement over
> the normal Python shell. I found that the handling of multi-line
> entries could be improved, and I found some discussion on the mailing
> list about that:
> http://www.scipy.net/pipermail/ipython-dev/2005-December/000694.html . 
> Since Bash handles multi-line entries with Readline, I just used the
> same method Bash uses and came up with this:

--- ipython-0.7.2/IPython/iplib.py.orig 2006-07-23 01:45:20.000000000 +0200
+++ ipython-0.7.2/IPython/iplib.py      2006-07-23 02:48:29.000000000 +0200
@@ -1763,7 +1763,8 @@
          # push).

          #print 'push line: <%s>' % line  # dbg
-        self.autoindent_update(line)
+        for subline in line.splitlines():
+            self.autoindent_update(subline)

          self.buffer.append(line)
          more = self.runsource('\n'.join(self.buffer), self.filename)
@@ -1806,6 +1807,14 @@
          if line.strip():
              if continue_prompt:
                  self.input_hist_raw[-1] += '%s\n' % line
+                if self.has_readline: # and some config option is set?
+                    try:
+                        histlen = self.readline.get_current_history_length()
+                        newhist = self.input_hist_raw[-1].rstrip()
+                        self.readline.remove_history_item(histlen-1)
+                        self.readline.replace_history_item(histlen-2,newhist)
+                    except AttributeError:
+                        pass # re{move,place}_history_item are new in 2.4.
              else:
                  self.input_hist_raw.append('%s\n' % line)


> This works fine for me. One thing that might be improved is that
> Up-arrow moves to previous history entry instead of moving up a line
> in the current entry, just like in Bash. And because of what seems to
> be a bug in Readline a saved multi-line history entry is transformed
> to several one-line entries when the history file is read back, again
> just like in Bash. Anyway, with this change I find IPython much easier
> to use.
> 
> Regards,
> Jörgen


More information about the IPython-dev mailing list