Copy code from IPython without leading triple dots

Question:

I’m using IPython Qt Console and when I copy code FROM Ipython it comes out like that:

    class notathing(object):
        ...:
        ...:     def __init__(self):
        ...:         pass
        ...:

Is there any way to copy them without those leading triple dots and doublecolon?

P.S. I tried both Copy and Copy Raw Text in context menu and it’s still the same. OS: Debian Linux 7.2 (KDE).

Asked By: LetMeSOThat4U

||

Answers:

This may be too roundabout for you, but you could use the %save magic function to save the lines in question and then copy them from the save file.

Answered By: Chris Bonnell

I tend to keep an open gvim window for this kind of things. Paste your class definition as is and then do something like:

:%s/^.*.://
Answered By: Nicola Musatti

One of the cool features of ipython is session logging. If you enable it, the code you input in your session is logged to a file. It’s very useful, I use it all the time.

To make things even niftier for me, I have a shell alias ipy_log_cat, which prints the entire file. You can do something like: ipy_log_cat | tail to get the most recent input lines. (this is also useful for greping session history, etc.). You can also save a few keyboard/mouse strokes by piping it into xclip!

Answered By: shx2

How about using %hist n to print line n (or a range of lines) without prompts (including line continuations), and doing your copy from that? (Simply scrolling back to that line is nearly as good).

In [1]: def foo():
   ...:     return 1+2
   ...: 

In [6]: %history 1
def foo():
    return 1+2
Answered By: hpaulj

This QTconsole copy regression has been fixed, see https://github.com/ipython/ipython/issues/3206 – I can confirm that the desired behavior is again present in the QtConsole in the Canopy 1.2 GUI and, I suspect, in the ipython egg installable by free users from the Enthought egg repo.

Answered By: Jonathan March
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.