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
yyis a shorthand fory_(yank the current line)- The yanked line is stored in the default register (
") - You can then paste it with
p(below) orP(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
3yyto yank 3 lines starting from the cursor - Use
Ywhich behaves the same asyyby default - Use
"+yyto yank a line directly to the system clipboard - Combine with
Pto paste above the current line instead of below