Skip to content

Vim registers and marks: copy many things, jump back

NinjaLesson 55 of 668 min

Stop losing what you yanked: Vim’s named registers, the yank register "0, appending with "A, and marks to jump back to a place. Practise in your browser.

You yank a line, delete a different one, press p, and get the deleted line back instead of the one you wanted. Vim didn't lose your yank. It put it in a different register, and once you know where to look you'll never lose one again.

Where things go

RegisterHolds
""the last yank or delete: what plain p puts
"0the last yank, untouched by deletes
"1 to "9the last nine deleted lines, newest first
"a to "zyours: nothing goes there unless you say so
"_the black hole: "_dd deletes without touching any register

To use a register, type " and its name before the command: "ayy yanks the line into a, "ap puts it. So after yank, delete, p went wrong: "0p puts the yank. :reg shows every register at once.

Capital letters append. "Ayy adds the line to register a instead of replacing it. That's how you collect lines from all over a file and put them down in one go.

Marks: bookmarks in the file

mt sets mark t where the cursor is. Later, 't jumps back to that line (a backtick instead of the quote goes to the exact column), from anywhere in the file. Marks and registers are separate, so register t and mark t don't clash. '' jumps back to where you were before your last jump.

The plan for this lesson

standup.md has Tom's actions scattered over three days, and an empty "Tom's list" at the end:

G          go to the last line (Tom's list)
mt         mark it as t
gg         back to the top
/Tom:      find Tom's first line (Enter), then "tyy to yank it into t
n          the next one: "Tyy adds it to t (and again for the third)
't         jump back to the mark
"tp        put all three lines under the heading

Your turn

In standup.md, collect Tom’s three action lines in register t (keep them where they are, too), put them under “Tom’s list”, and save. Use a mark to jump back to the list.

Next