Clipboard

Æsh Readline can automatically copy killed and yanked text to the system clipboard using the OSC 52 escape sequence. When enabled, any text that enters the internal kill ring (via Ctrl+K, Ctrl+W, vi dd, yy, etc.) is also written to the system clipboard, allowing you to paste it into other applications.

This is a write-only operation — Æsh never reads from the clipboard, so there are no security concerns about exposing clipboard contents.

How It Works

OSC 52 sends Base64-encoded text to the terminal, which places it on the system clipboard:

SequencePurpose
ESC ] 52 ; c ; <base64> STCopy text to the system clipboard

The c parameter selects the clipboard buffer (as opposed to the primary selection). The String Terminator (ST) can be either ESC \ or BEL (\u0007). Æsh uses ESC \.

On terminals that don’t recognize OSC 52, the sequence is silently ignored — no visible output or errors.

Quick Start

Clipboard writing is automatic when using the Readline class. If the terminal supports OSC 52, all kill/copy actions write to the system clipboard with no application code needed:

Readline readline = new Readline();

// Clipboard integration is enabled automatically for supporting terminals.
// When the user presses Ctrl+K, Ctrl+W, etc., text goes to both
// the internal kill ring AND the system clipboard.
readline.readline(connection, new Prompt("$ "), input -> {
    // handle input
});

Terminal Support

TerminalSupported
iTerm2Yes
KittyYes
GhosttyYes
WezTermYes
footYes
ContourYes
RioYes
WarpYes
MinttyYes
xtermYes (if allowWindowOps is enabled)
GNOME TerminalYes
KonsoleYes
VS CodeYes
TabbyYes
HyperYes
Windows TerminalNo
AlacrittyNo
Apple TerminalNo
ConEmuNo
Linux ConsoleNo

On unsupported terminals the OSC 52 sequence is never sent — the supportsClipboard() check prevents it.

Connection API

The Connection interface provides methods for clipboard access:

Checking Support

// Heuristic check based on terminal type detection
boolean supported = connection.supportsClipboard();

This uses environment-based terminal detection via TerminalEnvironment and the Device.TerminalType enum. No terminal query is sent.

Writing to the Clipboard

// Copy text to the system clipboard
connection.writeClipboard("Hello, clipboard!");

The method is a no-op if the text is null/empty or the terminal doesn’t support OSC 52. It returns the Connection for chaining:

connection
    .writeClipboard("copied text")
    .write("Text copied to clipboard!\n");

ANSI Utility Methods

The ANSI class provides a static method for building the OSC 52 escape sequence directly:

import org.aesh.terminal.utils.ANSI;

// Build an OSC 52 sequence
String seq = ANSI.buildOsc52Write("text to copy");
// Result: ESC ] 52 ; c ; dGV4dCB0byBjb3B5 ESC \

// Write it manually
connection.write(seq);

Constants

ConstantValueDescription
ANSI.OSC_CLIPBOARD52OSC code for clipboard access

Automatic Readline Integration

When using the Readline class, clipboard writing is handled automatically through the PasteManager. Every call to PasteManager.addText() — which all kill/copy actions use — triggers a clipboard write if OSC 52 is supported.

Actions that write to the clipboard:

ActionKey (Emacs)Key (Vi)
Kill to end of lineCtrl+KD (command mode)
Kill to start of lineCtrl+U
Kill word backwardCtrl+W
Kill word (Unix filename rubout)
Delete characterCtrl+Dx
Delete previous characterBackspaceX
Copy lineyy
Change actionc + motion

Opting Out

To disable automatic clipboard writing, set the NO_CLIPBOARD flag:

import org.aesh.readline.ReadlineFlag;

EnumMap<ReadlineFlag, Integer> flags = new EnumMap<>(ReadlineFlag.class);
flags.put(ReadlineFlag.NO_CLIPBOARD, 0);

readline.readline(connection, new Prompt("$ "), input -> {
    // handle input
}, null, null, null, null, flags);

Manual Usage

For applications that manage their own rendering (outside of Readline), use the Connection method directly:

// Copy selected text to the system clipboard
if (connection.supportsClipboard()) {
    connection.writeClipboard(selectedText);
}

Or build the sequence manually with the ANSI utility:

String osc52 = ANSI.buildOsc52Write(selectedText);
connection.write(osc52);

Specification

The OSC 52 protocol is documented in the xterm control sequences specification:

XTerm Control Sequences — Operating System Commands

See Also