Copy and Paste a Line
- Press
Escto be in normal mode. - Put the cursor on the line and press
yy. - Move to where the copy should go and press
p.
p pastes a copied line below the cursor line; P pastes it above. A count works as usual: 3yy copies three lines, and 3p pastes the copy three times.
Copy Words and Parts of Lines
| Keys | What it copies |
|---|---|
yw | To the start of the next word |
yiw | The word under the cursor |
y$ | To the end of the line |
y0 | To the start of the line |
yG | To the end of the file |
When the copy is part of a line, p pastes it after the cursor and P before it. In Vim, Y copies the whole line like yy; in Neovim, Y copies to the end of the line.
Select with Visual Mode
v: selects character by character.V: selects whole lines.Ctrl+v: selects a rectangular block, useful for columns.
After pressing one of them, move the cursor with the arrow keys, j, k, or a search. Then press y to copy or d to cut. Esc leaves visual mode without doing anything.
Cut and Paste
Vim has no separate cut command: every delete saves the text, so a delete is a cut. dd cuts a line, dw a word, and d in visual mode the selection. Then paste with p. To move a line down by one, press ddp. More delete commands are in how to delete a line in Vim.
Registers
Vim stores copied and deleted text in registers, named by a character. Put a double quote and the register name before the command:
"ayy
"ap
:registers
- Unnamed register: used by every
y,d, andpwithout a name. "0: holds the last copy. A delete does not overwrite it, so"0ppastes what you copied even after you deleted something."ato"z: named registers that keep text until you replace it."_: the black hole;"_dddeletes without saving the text.
:registers (or :reg) shows what each one holds. In insert mode, Ctrl+r followed by a register name, such as 0, pastes it at the cursor.
Paste from Your Own Computer over SSH
When Vim runs on a server, text you paste with your terminal's paste command arrives as if you typed it. With automatic indenting on, each pasted line can be indented further than the one before. Current Vim versions use bracketed paste when the terminal supports it, which avoids this. If the indent still breaks, turn on paste mode while you paste:
:set paste
Then press i, paste, press Esc, and turn it off again with :set nopaste. Paste mode disables indenting and some mappings while it is on. Neovim handles pasted text on its own and does not need it.
The System Clipboard
The registers "+ and "* are the system clipboard, and they work only when Vim was built with clipboard support. Check with:
vim --version | grep clipboard
+clipboard means support is built in; -clipboard means it is not. On a server over SSH, the clipboard would be the server's, not yours, so it does not help there. To copy from the server to your computer, select the text with your terminal's mouse selection; turn off line numbers with :set nonumber first so they are not copied. If you are stuck in a mode, see how to save and quit in Vim, and to step back after a bad paste, undo with u.