Skip to content

iloveitaly/ipython-copy

Repository files navigation

Release Notes Downloads GitHub CI Status License: MIT

Copy Text, Variables, and History to Clipboard from IPython

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.

Installation

uv add ipython-copy

Once installed, load it in your IPython session:

%load_ext ipython_copy

To load it automatically every time you start IPython, you can add it to your ipython_config.py in the c.InteractiveShellApp.extensions list.

Usage

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 world

You 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.

Features

  • Smart line magic (%copy) for copying the last output, history lines, or specific variables.
  • Cell magic (%%copy) for cleanly copying multi-line blocks.
  • %pickle magic 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.

Remote Environments and OSC 52

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:

  1. Automatic Fallback: By default, pyperclip uses determine_clipboard() to find a working backend. If it cannot find a local GUI clipboard (like xclip, wl-clipboard, or pbcopy) and it detects that it is running inside a terminal (by checking if the TERM environment variable is set and sys.stdout or sys.stderr is a TTY), it will automatically fall back to the new OSC 52 backend.
  2. 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.

Alternatives


This project was created from iloveitaly/python-package-template

About

IPython magic commands to copy text, variables, and history to your system clipboard.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors