The three ways to copy in Vim

Copying text in Vim works differently than in most editors because Vim uses a system called registers to store what you cut or copy. The most common way to copy is to position your cursor, select the text you want, and press y (which stands for "yank" — Vim's word for copy). You then move to where you want the text and press p to paste it after your cursor, or P to paste it before your cursor.

The second method is to use Vim's visual mode to highlight text first, then copy it. The third is to copy entire lines or blocks without selecting them visually, using Vim's motion commands. All three methods store the copied text in Vim's default register, and you can paste from that register anywhere in your document.

Key Takeaways

  • Press y followed by a motion (like yy for a whole line) to copy text, then p to paste it after your cursor or P to paste before.
  • Enter visual mode with v, highlight the text you want, press y, then move and paste with p or P.
  • Copying in Vim stores text in registers; the default register is what you use most of the time, but you can save to named registers with "a before yanking.
  • If you paste and the text looks wrong or appears in the wrong place, you likely pasted before or after the cursor when you meant the opposite — use P instead of p or vice versa.

Copy a whole line with yy

The fastest way to copy an entire line is to press yy while your cursor is anywhere on that line. You do not need to position the cursor at the start or end — Vim copies the whole line no matter where you are. After you press yy, the line is stored in Vim's default register and ready to paste.

To paste the line, move your cursor to the line below where you want the copied line to appear and press p. Vim will insert the copied line below your cursor. If you want the copied line to appear above your cursor instead, press P (capital P). This is the most common mistake: forgetting that p pastes after and P pastes before.

Copy multiple lines with a number before yy

To copy more than one line at a time, type a number before yy. For example, 3yy copies the current line plus the two lines below it — three lines total. Position your cursor on the first line you want to copy, type the number, then press yy.

This works because Vim reads the number as a count and applies it to the command that follows. The same pattern works with other Vim commands: 3dd deletes three lines, 3p pastes three times. After you copy with 3yy, move to where you want the lines and press p or P to paste all three lines at once.

Copy part of a line with visual mode

To copy only part of a line, enter visual mode by pressing v. Your cursor will change appearance, and you are now in a mode where you can select text by moving the cursor. Use the arrow keys or h, j, k, l to move and highlight the text you want to copy.

Once the text is highlighted, press y to copy it. The text disappears from the highlight but stays in your register. Move your cursor to where you want to paste and press p or P. If you make a mistake while selecting, press Escape to exit visual mode without copying, then try again.

Copy from cursor to end of line with y$

If you want to copy from where your cursor is to the end of the current line, press y$. The $ tells Vim "to the end of the line," and y means copy. This is faster than entering visual mode and selecting to the end manually.

Similarly, y^ copies from the start of the line to your cursor, and yw copies the current word. These are examples of Vim motions — commands that tell Vim where to copy to or from. Learning a few common motions saves time: yb copies the word before your cursor, y) copies to the end of the sentence, and y} copies to the end of the paragraph.

Save copies to named registers so you do not lose them

Every time you copy something new, it replaces what was in your default register. If you copy line A, then copy line B, line A is gone. To keep multiple copies at once, use named registers — Vim lets you store text in registers labeled a through z.

To copy to a named register, press " (a quote mark), then the letter of the register you want to use, then your copy command. For example, "ayy copies the current line into register a. Later, you can paste from register a by pressing "ap. This way you can hold onto multiple pieces of text without them overwriting each other. If you forget which register holds what, you can see all your registers by typing :registers and pressing Enter.

Paste from the system clipboard with Ctrl+Shift+V

Vim's copy and paste system is separate from your operating system's clipboard. If you copy text in another program and want to paste it into Vim, you cannot just press p — Vim will not see it. Instead, press Ctrl+Shift+V to paste from your system clipboard into Vim.

The reverse is also true: if you copy text in Vim with y, you cannot paste it into another program with Ctrl+V. To copy from Vim to your system clipboard, you need to configure Vim to use the system clipboard, or you can select the text visually, copy it with y, then use a terminal command to move it to your clipboard. Some versions of Vim have this set up already; if Ctrl+Shift+V does not work, check your Vim configuration file.

Frequently Asked Questions

Why does my pasted text look wrong or have extra spaces?

Vim pastes exactly what you copied, including spaces and line breaks. If you copied a line with yy and pasted it with p, Vim inserts it as a complete line below your cursor. If you copied part of a line with visual mode and pasted it, it inserts inline without adding a line break. Check whether you used p (paste after) or P (paste before) — you may have pasted in the right place but the wrong direction.

How do I copy without going into visual mode?

Use Vim motions with the y command. yy copies a line, yw copies a word, y$ copies to the end of the line, and y) copies to the end of a sentence. You can also use 3yy to copy three lines, or y5w to copy five words. These are faster than visual mode once you learn them.

Can I copy and paste between two Vim windows at the same time?

Yes. If you have two Vim windows open (either in the same session or separate), copy text in one window with y, then switch to the other window and paste with p. The text stays in your register until you copy something else. Named registers also work across windows — copy to register a in one window, switch windows, and paste with "ap.

What if I copy something by accident and want to get back what I had before?

Vim stores your last several copies in numbered registers 0 through 9. Register 0 holds your most recent copy, register 1 holds the one before that, and so on. To paste from an older copy, press "1p to paste from register 1, or "2p for register 2. Type :registers to see what is in each numbered register.