vimtricks.wiki Concise Vim tricks, one at a time.

How do I copy an entire line in Vim?

Answer

yy

Explanation

The yy command yanks (copies) the entire current line, including the newline character. This is one of the most fundamental Vim commands for moving text around.

How it works

  • yy is a shorthand for y_ (yank the current line)
  • The yanked line is stored in the default register (")
  • You can then paste it with p (below) or P (above)

Example

Given the text:

first line
second line
third line

With the cursor on second line, pressing yy copies that line. Then pressing p pastes it below:

first line
second line
second line
third line

Tips

  • Use 3yy to yank 3 lines starting from the cursor
  • Use Y which behaves the same as yy by default
  • Use "+yy to yank a line directly to the system clipboard
  • Combine with P to paste above the current line instead of below

Next

How do I edit multiple lines at once using multiple cursors in Vim?