Copy and paste in Vim: yank and put

In Vim, copying is called yanking and pasting is called putting. yy copies the current line, p pastes it below and P pastes it above. y followed by a motion copies part of a line: yiw copies the word under the cursor and y$ copies to the end of the line. What you yank stays inside Vim. To reach the system clipboard you name it: "+y.

Each example shows the text before and after, with the keys in between. Unless a row says otherwise, the cursor starts on the first character. Every example was checked in Neovim, and every one the game supports was also replayed in the Vim engine that runs it.

Quick answers

ToPress
Copy the current lineyy
Paste after the cursor, or below the linep
Paste before the cursor, or above the lineP
Duplicate a lineyyp
Copy three lines3yy
Copy the word under the cursoryiw
Copy to the end of the liney$
Copy the whole fileggyG
Cut a line and paste it somewhere elsedd, move, p
Copy to the system clipboard"+yy or "+yiw (needs clipboard support, see below)
Paste from the system clipboard"+p

Yank: y and a motion

y is an operator, like d and c, but it leaves the text as it was. The motion after it says how much to copy. On the line big red box:

KeysCursor starts onCopies
ywthe b of bigbig  (the word and the space after it)
yethe b of bigbig
yiwthe e of redred
yawthe e of redred 
y$the r of redred box
ybthe r of redbig 
yyanywherethe whole line, as a line

yw takes the space after the word, because w moves to the start of the next word. When you want the word and nothing else, use yiw, which works from anywhere inside the word. Other text objects work the same way: yi( copies what is inside the parentheses and yi" what is inside the quotes.

Y also copies the whole line in Vim and in Vimrealm. In Neovim, Y copies to the end of the line, like y$.

After a yank the cursor goes to the start of what you copied, so yb, and yiw from the middle of a word, move it back. A yank is not a change: nothing is added to the undo history, and the dot command does not repeat it.

Put: p and P

Where the text lands depends on how it was copied.

On big box, yiw copies big and w moves to the b of box. Then:

KeysAfter
pbig bbigox (after the b)
Pbig bigbox (before the b)

To get big big box from big box, copy the word with its space (yw), move to box (w) and put it before (P): ywwP.

With lines, the buffer holds one, two and three, and the cursor starts on one:

KeysLines after
yypone / one / two / three
yyGpone / two / three / one
yyGPone / two / one / three

After p or P the cursor sits on the last character of text pasted inside a line, or on the first pasted line.

How to copy and paste a line in Vim

  1. Put the cursor anywhere on the line and press yy.
  2. Move to where the copy should go.
  3. Press p to paste it below that line, or P to paste it above.

yyp duplicates the line in one go. For more lines, put a count in front: 3yy copies the current line and the two below it. y2j does the same with a motion, yG copies to the end of the file and ggyG copies the whole file.

BeforeKeysAfter
one / two / three / four3yyGpone / two / three / four / one / two / three
one / twoyy3pone / one / one / one / two
one / twoyyp..one / one / one / one / two

A count before p pastes that many copies, and . repeats the last paste.

Pasting a line into the middle of another line

A line copied with yy always pastes as a line of its own, even when the cursor is in the middle of text. To paste just its text inside another line, copy it from inside: 0y$ goes to the start of the line and copies up to the end, without the line break (^y$ skips the indentation).

BeforeKeysAfter
world / hello !yyjf!Pworld / world / hello !
world / hello !0y$jf!Pworld / hello world!

It works the other way too. To put a copied word on a line of its own, open an empty line first: on big red box, yiwo<Esc>p adds a line big below.

Cut and paste: delete, then put

Vim has no separate cut command. d, c and x keep what they removed, in the same place a yank goes, so deleting and then pasting is a cut and paste:

BeforeKeysAfter
one / two / three, cursor on twoddpone / three / two (moved down a line)
one / two / three, cursor on twoddkPtwo / one / three (moved up a line)
teh, cursor on the expthe
one / two / three / four / five3ddGpfour / five / one / two / three (three lines moved to the end)

Why did p paste the wrong thing?

Because a delete replaced what you yanked. Copy a word, delete another one to make room for it, press p, and you get the word you just deleted back:

BeforeKeysAfter
apple bananayiwwdiwpapple banana

yiw copied apple, but diw then stored banana in the same place, and p pasted banana. Plain p always pastes the most recent yank or delete, from what Vim calls the unnamed register.

Ways around it:

:registers shows what each register holds.

Paste in order: gp and gP

P leaves the cursor on the last character it pasted, so pressing it again pastes inside the text you just put:

BeforeKeysAfter
ha!yt!PPhhaaha!
ha!yt!gPgPhahaha!

gp and gP paste like p and P, then leave the cursor just after the new text, ready for the next paste. With lines, that is the line below the paste, so . puts a copy under every line in turn:

BeforeKeysAfter
--- / one / two / threeddgp..one / --- / two / --- / three / ---
--- / one / two / threeddp..one / --- / --- / --- / two / three

Copy in Visual mode

When you would rather see what you are copying, select it first. v starts a selection, any motion extends it, and y copies it: vey copies to the end of the word. V selects whole lines, so Vjy copies two lines that paste as lines. ggVGy selects the whole file and copies it.

In Visual mode p replaces the selection with what you copied. The Visual mode guide covers selections in detail.

Copy and paste with the system clipboard

Everything above stays inside Vim. To copy to or paste from other programs, put a register name before the command: " and then + for the system clipboard.

KeysWhat it does
"+yycopy the line to the clipboard
"+yiwcopy the word to the clipboard
ggVG"+y or gg"+yGcopy the whole file to the clipboard
"+ppaste from the clipboard after the cursor
Ctrl-r +paste from the clipboard in Insert mode

A few things decide whether this works:

Neovim's :checkhealth vim.provider output. Under Clipboard (optional): OK, Clipboard tool found: wl-copy.
Neovim 0.12 on Linux (Wayland): :checkhealth vim.provider shows which clipboard tool it found.

You can also paste with your terminal's own shortcut, such as Ctrl-Shift-v or Cmd-v. Current versions of Vim and Neovim recognise a terminal paste and insert it as it is. In older setups the terminal just types the text, so enter Insert mode first, and the indentation can pile up line after line. If it does, run :set paste before pasting and :set nopaste after.

Practise yank and put in Vimrealm

Vimrealm teaches y, yy, Y, p, P, gp, gP and . in chapter 10, Hall of Echoes. Each quest counts your keystrokes against par, so you learn the shortest way to copy and place what you need, not just one way that works. Visual-mode p and P come in chapter 12, and the post-game Trials of Echoes drill yank, put and the dot together.

The game has one register, which works like Vim's unnamed register: yanks and deletes both go into it, so the "wrong paste" above happens in the game too. It does not have "0, "_, "+ or Insert-mode Ctrl-r. Those are Vim features to learn once y and p feel natural.

Chapters 1–5, which teach the motions you yank with, are free, with no sign-up. Chapters 6–13 and the Trials are part of the full version ($7.99, one payment). See how the course works or keep the cheat sheet entry for yank and put open while you practise.

Play Vimrealm free