-1

Is there a command in the interactive shell that copies the last expression to the clipboard?

I know there's the _ command, which repeats the last expression's evaluation, e.g.

>>>  " ".join(['a', 'b', 'c'])
'a b c'
>>> _
'a b c'

But what I'm looking for is a command that copies " ".join(['a', 'b', 'c']) to the clipboard. Is there such a thing?

Mathieu Dhondt
  • 8,405
  • 5
  • 37
  • 58
  • 2
    Which interactive shell are you using? IDLE? – Haskar P Sep 01 '18 at 20:18
  • If you're in Windows console, you want to open the properties of the console window, enable the QuickEdit mode and then you'll be able to select using left mouse button and copy using right mouse button. Right mouse button is for pasting as well, if nothing is selected. – zvone Sep 01 '18 at 20:21
  • 1
    There are modules you can install, like `pyperclip`, that contain functions for copying into and out of the clipboard. – khelwood Sep 01 '18 at 20:26
  • @khelwood - thanks for the pointer. I'll maybe try and roll my own, together with `pyreadline` one day. – Mathieu Dhondt Sep 04 '18 at 15:16
  • Why the downvote? (I'm genuinely interested to know). – Mathieu Dhondt Sep 05 '18 at 20:38

2 Answers2

1

You can do that with your terminal emulator's tools.

In Python itself, there's no such functionality -- but one can add a user-supplied magic command for IPython -- here's one for Linux and MacOS.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
0

Use ipython terminal..

$ ipython

Python 3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 11:07:29) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.5.0 -- An enhanced Interactive Python. Type '?' for help.



In [1]: " ".join(['a', 'b', 'c'])
Out[1]: 'a b c'

In [2]: _
Out[2]: 'a b c'

In [3]: _1
Out[3]: 'a b c'

In [4]: __
Out[4]: 'a b c'

In [5]: _
Out[5]: 'a b c'

In [6]: _1
Out[6]: 'a b c'
Merlin
  • 24,552
  • 41
  • 131
  • 206