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
yyyanks (copies) the entire current line, including the newline characterppastes 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
yy2pto create two copies of the line below - Use
Yinstead ofyy— they behave the same way by default - In visual mode, select multiple lines with
V, thenypto duplicate the selection - To duplicate and immediately edit the copy, use
yypfollowed by a change command likeciwon the part you want to modify - If you have already yanked something else, the
yywill overwrite the unnamed register — use a named register ("ayythen"ap) to preserve other yanked content