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

[number]yy [buffer]yy

PARAMETERS

number
    The number of lines to yank, starting from the current line. If omitted, defaults to 1.

buffer
    A letter (a-z or A-Z) to specify a named buffer to store the yanked text. If omitted, the default unnamed buffer is used. Uppercase letters append to the existing content of the named buffer.

yy
    The yank command itself. This is typically entered in vi's command mode.

DESCRIPTION

The yank command, commonly used within the vi and vim text editors, is used to copy lines of text into a buffer (or register) for later pasting (putting). It is conceptually similar to the "copy" functionality found in most text editors, though its behavior and integration within the vi editing model are distinct. The yank command typically involves placing the cursor on the line (or selecting lines visually) and then issuing the appropriate yank command, often in combination with a buffer designation. The yanked text remains in the specified buffer until it is overwritten by another yank or delete operation. This allows the copied text to be pasted multiple times or in different locations within the document or even different documents open in vi or vim. Different buffers can be specified to store multiple different blocks of text.
The command doesn't exist as a standalone executable but is an internal command to vim. To use it on the command line, it is necessary to use the echo command with the clipboard option.

CAVEATS

The yank command is specific to vi and vim and does not work outside of these editors. The effect of yy on the command line only applies to the vim editor.

EXAMPLES

To yank the current line into the default buffer: yy
To yank the next 5 lines into the 'a' buffer: 5"ayy
To yank the current line to the system clipboard from the command line:
echo "Hello World" | vim -c "normal \"+yy" -c ":q!"

REGISTERS

vi and vim uses registers to store yanked (copied) or deleted text. By default, if no register is specified, the unnamed register is used. Named registers (a-z, A-Z) can be used to store and retrieve multiple selections.

SEE ALSO

p(1), d(1), vi(1), vim(1)

Copied to clipboard