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

How do I duplicate a line in Vim?

Answer

yyp

Explanation

The yyp command duplicates the current line by yanking it and immediately pasting it below. It is a two-step combo that becomes second nature for any Vim user.

How it works

  • yy yanks (copies) the entire current line, including the newline character
  • p pastes the yanked line on a new line below the current one

Example

Given the text:

first line
second line
third line

With the cursor on second line, pressing yyp results in:

first line
second line
second line
third line

The original line stays in place and an identical copy appears directly below it.

Tips

  • Use yyP (uppercase P) to duplicate the line above instead of below
  • Use yy2p to create two copies of the line below
  • Use Y instead of yy — they behave the same way by default
  • In visual mode, select multiple lines with V, then yp to duplicate the selection
  • To duplicate and immediately edit the copy, use yyp followed by a change command like ciw on the part you want to modify
  • If you have already yanked something else, the yy will overwrite the unnamed register — use a named register ("ayy then "ap) to preserve other yanked content

Next

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