Learn Practice Daily golf Reference

How to copy a line in Vim

In Normal mode, press yy to copy the line the cursor is on, move to where it goes, and press p to paste it below that line or P above it. 3yy copies three lines.

Try it here

Carry the tasks over

Two of Monday's tasks belong on Tuesday too. The cursor is on the stand-up: yy copies it, G goes to the last line, p pastes below. Then copy line 3 to the end of the file without moving: :3t$ and ↵.

1 of 2Copy the stand-up line to the end
1## Monday
2- [ ] Stand-up at 9:30
1- [ ] Review the release notes
2
3## Tuesday
+- [ ] Stand-up at 9:30
NORMALLoading the editor…1/20.0s2:1

This editor needs a keyboard. Read on, and play it at your computer.

KeysWhat it does
yyCopy the current line.
3yyCopy three lines, starting with this one.
yj / ykCopy this line and the one below / above.
p / PPaste the line below / above the current line, whatever column the cursor is in.
3pPaste three copies.
:t. ↵Copy the current line to just below itself.
:5t10 ↵Copy line 5 to below line 10. The cursor can be anywhere.
:5,8t$ ↵Copy lines 5 to 8 to the end of the file.
:t0 ↵Copy the current line to the top of the file.
:5,8y ↵Yank lines 5 to 8, ready for p.

Lines paste as lines

Vim remembers that you copied a whole line. So p doesn't split the line you're on: it opens a new line below it and puts the copy there, wherever the cursor is in the line, and P does the same above. That's why yy then p is the whole recipe, with no need to go to the end of a line first.

A count goes in either place. 3yy copies three lines once; yy then 3p pastes one line three times.

Copy by line number: :t

:t (short for :copy, also written :co) copies lines to below the line you name, without moving the cursor there first. Read :5t10 as "line 5 to line 10". Ranges work too: :5,8t$ copies four lines to the end of the file, and . means the current line, so :t. copies it below itself.

With set number on, the line numbers are in front of you, and :t is often faster than walking there. It also leaves your registers alone: what you yanked earlier is still there for p.