Skip to content

Instantly share code, notes, and snippets.

@nova77
Last active October 24, 2023 18:50
Show Gist options
  • Save nova77/5403446 to your computer and use it in GitHub Desktop.
Save nova77/5403446 to your computer and use it in GitHub Desktop.
copy to clipboard ipython magic
"""
Add copy to clipboard from IPython!
To install, just copy it to your profile/startup directory, typically:
~/.ipython/profile_default/startup/
Example usage:
%clip hello world
# will store "hello world"
a = [1, 2, 3]
%clip a
# will store "[1, 2, 3]"
You can also use it with cell magic
In [1]: %%clip
...: Even multi
...: lines
...: work!
...:
If you don't have a variable named 'clip' you can rely on automagic:
clip hey man
a = [1, 2, 3]
clip a
"""
import sys
if sys.platform == 'darwin':
from AppKit import NSPasteboard, NSArray
elif sys.platform.startswith('linux'):
from subprocess import Popen, PIPE
else:
raise ImportError("Clip magic only works on osx or linux!")
from IPython.core.magic import register_line_cell_magic
def _copy_to_clipboard(arg):
arg = str(globals().get(arg) or arg)
if sys.platform == 'darwin':
pb = NSPasteboard.generalPasteboard()
pb.clearContents()
a = NSArray.arrayWithObject_(arg)
pb.writeObjects_(a)
elif sys.platform.startswith('linux'):
p = Popen(['xsel', '-pi'], stdin=PIPE)
p.communicate(input=arg)
print ('Copied to clipboard!')
@register_line_cell_magic
def clip(line, cell=None):
if line and cell:
cell = '\n'.join((line, cell))
_copy_to_clipboard(cell or line)
# We delete it to avoid name conflicts for automagic to work
del clip
@BrianMiner
Copy link

OSError: [Errno 2] No such file or directory

@vpontis
Copy link

vpontis commented Feb 8, 2016

Removed AppKit dependency for OS X: https://gist.github.com/vpontis/46e5d3154cda92ce3e0f (only works for OS X)

@jemshid
Copy link

jemshid commented Oct 24, 2016

One of the features I was looking forward with Ipython is to copy selected lines from history to my code editor with out using mouse. I have added one more function in your program to copy history of selected line to clipboard. I have made modification in two places in the history.py file of Ipython. If any of you can guide, I would like to push / pass it to the developers.

@cphyc
Copy link

cphyc commented Jul 5, 2017

I made a fork that supports python3: https://gist.github.com/cphyc/47d7dbe756fc2d16d05f6271f647fd03. It's just a matter of adding () around the print statement…

@apatrushev
Copy link

apatrushev commented Feb 29, 2020

Just to note that for most cases following alias will be enough:
alias ck echo "$$_" | pbcopy

@ianozsvald
Copy link

Blast from the past - I just went searching for "how to setup a copy-to-clipboard in IPython like I used to have...and found this, with my previous comments". Thanks to other contributors for their updates too. I'll just verify that using this with my suggestions above on Linux, works for Python 3.11 with IPython 8.10. xsel needs to be installed as a system package and -ib is a better choice on Linux.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment