Skip to content

Instantly share code, notes, and snippets.

@vpontis
Last active January 11, 2024 18:59
Show Gist options
  • Save vpontis/46e5d3154cda92ce3e0f to your computer and use it in GitHub Desktop.
Save vpontis/46e5d3154cda92ce3e0f to your computer and use it in GitHub Desktop.
Copy to clipboard from iPython on Mac OS X
"""
Add copy to clipboard from IPython!
To install, just copy it to your profile/startup directory, typically:
~/.ipython/profile_default/startup/
Example usage:
%copy hello world
# will store "hello world"
a = [1, 2, 3]
%copy a
# will store "[1, 2, 3]"
You can also use it with cell magic
In [1]: %%copy
...: Even multi
...: lines
...: work!
...:
If you don't have a variable named 'copy' you can rely on automagic:
copy hey man
a = [1, 2, 3]
copy a
"""
import subprocess
import sys
from IPython.core.magic import register_line_cell_magic
def _copy_to_clipboard(output):
output = str(globals().get(output) or output)
process = subprocess.Popen(
'pbcopy', env={'LANG': 'en_US.UTF-8'}, stdin=subprocess.PIPE)
process.communicate(output.encode('utf-8'))
print('Copied to clipboard!')
@register_line_cell_magic
def copy(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 copy
@arthurio
Copy link

For those using pudb, you can use the following to load your magic commands after having opened the ipython shell with !:

import IPython
IPython.start_ipython(argv=[])

@tylerkahn
Copy link

This is a great script.

The functionality is such that %copy "hello" copies the literal string "hello" to your clipboard (including the quotes).

If you want it to evaluate the expression first, change line 37 to:

output = str(globals().get(output) or eval(output))

Now %copy "hello" copies the string hello to your clipboard.

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