I built this to solve a constant point of friction: getting data out of a remote or local REPL and into my system clipboard. This is a cross-platform IPython plugin that adds smart %copy and %pickle magic commands. It seamlessly handles both local GUI clipboards and headless remote SSH sessions.
uv add ipython-copyOnce installed, load it in your IPython session:
%load_ext ipython_copyTo load it automatically every time you start IPython, you can add it to your ipython_config.py in the c.InteractiveShellApp.extensions list.
The plugin exposes a %copy magic command that tries to be smart about what you want to copy. It handles standard string inputs, looks up variables in your namespace, and can even reference your output history.
# Copy the last output
%copy
# Copy the output from a specific history line
%copy 7
# Copy the string representation of a specific variable
%copy my_var
# Copy a literal string
%copy hello worldYou can also use it as a cell magic to easily copy multi-line blocks of text:
%%copy
Even multi
lines
work!If you are dealing with complex data structures, use the %pickle magic to serialize Python objects directly to your clipboard and unpickle them later.
- Smart line magic (
%copy) for copying the last output, history lines, or specific variables. - Cell magic (
%%copy) for cleanly copying multi-line blocks. %picklemagic to serialize and copy complex Python objects directly to the clipboard.- Fully cross-platform using
pyperclip. - Automatic headless and remote server support via OSC 52 sequence fallbacks.
I frequently work in tmux on remote servers, which used to make clipboard access a nightmare. This plugin relies on pyperclip, which handles remote clipboard access automatically without you needing to configure anything.
Here is how it works under the hood:
- Automatic Fallback: By default,
pyperclipusesdetermine_clipboard()to find a working backend. If it cannot find a local GUI clipboard (likexclip,wl-clipboard, orpbcopy) and it detects that it is running inside a terminal (by checking if theTERMenvironment variable is set andsys.stdoutorsys.stderris a TTY), it will automatically fall back to the new OSC 52 backend. - Explicit Selection (Optional): If you want to bypass GUI clipboards and force the use of OSC 52, you can explicitly set it in your Python code:
import pyperclip pyperclip.set_clipboard("osc52")
The only external requirement: While the library requires no configuration, your terminal emulator (e.g., iTerm2, Alacritty, Windows Terminal, Kitty) must support the OSC 52 escape sequence.
- Most modern terminals support it out of the box.
- Some terminals might have security settings that disable OSC 52 by default or prompt the user for permission the first time a script tries to write to the clipboard.
- IPythonClipboard - cannot copy a variable to the clipboard, only line numbers.
- bwagner/clip.py - can copy variables to clipboard, but relies on AppKit.
- nova77/clip.py - the original script that this project iterated upon.
This project was created from iloveitaly/python-package-template