clipboard

package
v0.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package clipboard copies text to the system clipboard.

There is no portable system call for this, so Copy runs a small chain of helper commands for the platform, in order, and the first one found in PATH that exits cleanly wins. That keeps the module free of cgo and of X11 headers, and works the same way on macOS, Windows, WSL, Wayland, X11 and Termux:

  • macOS: pbcopy.
  • Windows: clip, which is given UTF-16LE with a byte-order mark, since it reads 8-bit input in the console's code page and garbles anything that is not ASCII.
  • Linux and the BSDs: xclip and xsel, then wl-copy; wl-copy first when WAYLAND_DISPLAY is set, because under XWayland xclip appears to work while it writes to a clipboard the Wayland applications do not read. Then termux-clipboard-set for Termux on Android, and clip.exe, which is reachable from WSL, in UTF-16LE as on Windows.
  • Android: termux-clipboard-set, from the Termux:API package, then the X11 and Wayland helpers.

The whole chain gets 10 seconds: a helper that hangs, such as xclip with an X server that does not answer, fails the copy instead of hanging the command that asked for it. When no helper works, the error names every failure and what to install.

Index

Constants

View Source
const Timeout = 10 * time.Second

Timeout bounds the whole helper chain of one Copy, not one attempt: a helper that hangs, such as wl-copy with a compositor socket nobody answers, must not hang the command that asked for the copy, and several hanging helpers in a row must not add up.

Variables

This section is empty.

Functions

func Copy

func Copy(ctx context.Context, data []byte) (string, error)

Copy writes data, UTF-8 text, to the system clipboard and returns the name of the helper that took it, such as "pbcopy" or "wl-copy". It tries the helpers of the platform in order (see the package documentation) and stops at the first that is installed and exits cleanly. A helper that is installed but fails, as xclip does without a display, is passed over.

The chain stops early when ctx ends, and after Timeout at the latest. When no helper took the text, the error names each failure, with the helper's stderr made plain text, and what could be installed. On a platform without a known helper, Copy fails at once.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL