LinuxCommandLibrary

yank

Copy lines of text, often within vim

TLDR

Yank using the default delimiters (\f, \n, \r, \s, \t)

$ [sudo dmesg] | yank
copy

Yank an entire line
$ [sudo dmesg] | yank -l
copy

Yank using a specific delimiter
$ [echo hello=world] | yank -d [=]
copy

Only yank fields matching a specific pattern
$ [ps ux] | yank -g "[[0-9]+]"
copy

SYNOPSIS

yank [options] [file...]
command | yank [options]

PARAMETERS

-p, --primary
    Use the primary selection (often associated with mouse selection) instead of the standard clipboard.

-c, --clipboard
    Explicitly use the clipboard selection. This is typically the default behavior on X11 and Wayland systems.

-s , --selection
    Specify the selection type to use: 'primary' or 'clipboard'.

-o, --output
    Instead of copying, print the current content of the specified clipboard selection to standard output.

-q, --quiet
    Suppress all informational messages, such as 'Copied N bytes to clipboard'.

-l , --line
    Copy only the N-th line from the input (1-indexed).

-r , --range
    Copy lines from N to M (inclusive) from the input.

-a, --all
    Copy the entire input. This is the default action if no specific lines or ranges are provided.

-z, --null
    Use null characters as input delimiters, useful when piping output from commands like find -print0.

--trim-whitespace
    Remove leading and trailing whitespace from each line before copying to the clipboard.

--exec-clipboard
    Execute the content retrieved from the clipboard as a shell command. Use with extreme caution!

-h, --help
    Display a brief help message and exit.

-v, --version
    Display version information for yank and exit.

DESCRIPTION

yank is a command-line utility designed to streamline the process of copying text directly from the terminal to the system clipboard. It allows users to pipe the standard output of other commands or specify file content, and then effortlessly make that text available for pasting into other applications, whether they are terminal-based or graphical. The utility supports various copying modes, including copying entire inputs, specific lines, or defined ranges of lines. yank is particularly valuable for developers and system administrators who frequently need to transfer command output, log snippets, or configuration details from the console to other applications without resorting to cumbersome manual mouse selection and copying. It integrates with existing system clipboard utilities like xclip or xsel (for X11) and wl-copy (for Wayland) to perform its core function, enhancing command-line efficiency.

CAVEATS

yank is not a standard Linux utility that comes pre-installed with most distributions; it typically needs to be installed separately. Its functionality relies heavily on other underlying clipboard utilities like xclip or xsel (for X11 display servers) or wl-copy and wl-paste (for Wayland compositors). If these dependencies are not met, yank will be unable to interact with the system clipboard. Furthermore, the --exec-clipboard option poses a significant security risk, as it allows arbitrary commands to be executed from text copied into your clipboard, potentially by malicious sources.

INSTALLATION

As yank is not a core utility, it must be installed. Users can often find it in community-maintained repositories (e.g., Arch Linux AUR) or install it by compiling from its source code. In some cases, it might also be installable via Python's pip if the necessary system dependencies (like xclip) are already present.

BASIC USAGE EXAMPLES

To copy the output of a command to the clipboard:
ls -l | yank

To copy a specific line from a text file to the clipboard:
cat /etc/hosts | yank -l 3

To copy a range of lines from piped input:
seq 1 10 | yank -r 4-6

To paste content from the clipboard to your terminal (standard output):
yank --output

HISTORY

The yank utility, developed by Michael Treanor (mptre), emerged as an open-source project to address a common efficiency gap in command-line environments. While traditional methods of copying terminal output often involve manual mouse selection, this is cumbersome and unscriptable for large or dynamically generated content. yank was designed to provide a simple, pipe-friendly, and programmatic way to transfer text from standard input or files directly to the system clipboard. Its development is part of a broader trend of creating specialized command-line tools that enhance interoperability between the terminal and graphical applications, making text manipulation and transfer more seamless for developers and system administrators.

SEE ALSO

xclip(1), xsel(1), wl-copy(1), wl-paste(1), pbcopy(1), pbpaste(1), vim(1), tmux(1)

Copied to clipboard