# Send content to clipboard in ipython

Last updated on by

Some time I need to copy very long string from ipython to some where such as web browser, this operation is little annoying if the string is too long. I think I can make this operation more comfortable. After some little test and here is my solution.

from IPython.core.magic import register_line_magic
@register_line_magic
def clip(line):
global_dict = globals()
if not line in global_dict:
return
value = global_dict[line]
import os
os.system("echo '%s' | pbcopy" % str(value))
del clip

In [1]: a = "test " * 10

In [2]: a
Out[2]: 'test test test test test test test test test test '

In [3]: %clip a


Now if you press cmd + v, if will paste the 'test test test test test test test test test test '

We use register_line_magic to register this method as magic method so I can call it in ipython. Since my OS is OSX so I use pbcopy to make this done.

If you are using Linux, you can check this post