Copy code from IPython without leading triple dots
Asked Answered
L

5

33

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).

Leavitt answered 31/12, 2013 at 13:45 Comment(0)
L
4

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.

Leoine answered 1/1, 2014 at 16:18 Comment(0)
H
64

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
Hoplite answered 31/12, 2013 at 21:52 Comment(2)
This is the way.Diallage
Magical! %hist without any argument will print everythingAra
R
7

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!

Rood answered 31/12, 2013 at 18:19 Comment(0)
L
4

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.

Leoine answered 1/1, 2014 at 16:18 Comment(0)
T
2

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.

Thai answered 31/12, 2013 at 13:49 Comment(0)
C
0

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/^.*\.://
Canberra answered 31/12, 2013 at 14:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.