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
| To | Press |
|---|---|
| Copy the current line | yy |
| Paste after the cursor, or below the line | p |
| Paste before the cursor, or above the line | P |
| Duplicate a line | yyp |
| Copy three lines | 3yy |
| Copy the word under the cursor | yiw |
| Copy to the end of the line | y$ |
| Copy the whole file | ggyG |
| Cut a line and paste it somewhere else | dd, 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:
| Keys | Cursor starts on | Copies |
|---|---|---|
yw | the b of big | big (the word and the space after it) |
ye | the b of big | big |
yiw | the e of red | red |
yaw | the e of red | red |
y$ | the r of red | red box |
yb | the r of red | big |
yy | anywhere | the 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.
- Text copied from inside a line (
yw,yiw,y$) goes right after the cursor withp, or right before it withP. - Whole lines (
yy,3yy,yj) go on new lines below the current one withp, or above it withP. Where the cursor is in the line does not matter.
On big box, yiw copies big and w moves to the b of box. Then:
| Keys | After |
|---|---|
p | big bbigox (after the b) |
P | big 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:
| Keys | Lines after |
|---|---|
yyp | one / one / two / three |
yyGp | one / two / three / one |
yyGP | one / 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
- Put the cursor anywhere on the line and press
yy. - Move to where the copy should go.
- Press
pto paste it below that line, orPto 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.
| Before | Keys | After |
|---|---|---|
one / two / three / four | 3yyGp | one / two / three / four / one / two / three |
one / two | yy3p | one / one / one / one / two |
one / two | yyp.. | 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).
| Before | Keys | After |
|---|---|---|
world / hello ! | yyjf!P | world / world / hello ! |
world / hello ! | 0y$jf!P | world / 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:
| Before | Keys | After |
|---|---|---|
one / two / three, cursor on two | ddp | one / three / two (moved down a line) |
one / two / three, cursor on two | ddkP | two / one / three (moved up a line) |
teh, cursor on the e | xp | the |
one / two / three / four / five | 3ddGp | four / 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:
| Before | Keys | After |
|---|---|---|
apple banana | yiwwdiwp | apple 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:
- Select, then paste over it.
yiw, move to the other word, select it withveand pressp. Onapple banana,yiwwvepgivesapple apple. The Visual mode guide explains whyPis better for replacing several words in a row. - Paste the yank, not the delete (Vim only). Register 0 holds your last yank, and deletes don't touch it.
yiwwdiw"0pgivesapple apple. - Delete into the black hole (Vim only).
"_diwdeletes without storing anything, so the nextpstill pastes your yank. - Paste while typing (Vim only). In Insert mode, Ctrl-r followed by a register name pastes it:
ciwthen Ctrl-r0replaces the word with your last yank.
: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:
| Before | Keys | After |
|---|---|---|
ha! | yt!PP | hhaaha! |
ha! | yt!gPgP | hahaha! |
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:
| Before | Keys | After |
|---|---|---|
--- / one / two / three | ddgp.. | one / --- / two / --- / three / --- |
--- / one / two / three | ddp.. | 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.
| Keys | What it does |
|---|---|
"+yy | copy the line to the clipboard |
"+yiw | copy the word to the clipboard |
ggVG"+y or gg"+yG | copy the whole file to the clipboard |
"+p | paste from the clipboard after the cursor |
Ctrl-r + | paste from the clipboard in Insert mode |
A few things decide whether this works:
- Vim has to be built with clipboard support. Run
vim --version:+clipboardmeans it works,-clipboardmeans it doesn't. Some Linux packages ship without it. On Debian and Ubuntu, for example, thevim-gtk3package has it. - Neovim uses an external clipboard tool. It picks one it finds on the system, such as
wl-copyorxclipon Linux andpbcopyon macOS.:checkhealthtells you which one it found. "*and"+differ on Linux. On X11,"*is the primary selection (the text you paste with the middle mouse button) and"+is the clipboard that Ctrl-c and Ctrl-v use in other programs. On Windows and macOS both are the same clipboard.- To make every yank use the clipboard on Linux, add
set clipboard=unnamedplusto your vimrc. On macOS and Windows, useset clipboard=unnamed. (set clipboard=unnamed,unnamedpluscovers all three.) In Neovim,vim.opt.clipboard = 'unnamedplus'ininit.luaworks on every system. Then plainyyandpuse the clipboard.
: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