Vim registers and marks: copy many things, jump back
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
| Register | Holds |
|---|---|
"" | the last yank or delete: what plain p puts |
"0 | the last yank, untouched by deletes |
"1 to "9 | the last nine deleted lines, newest first |
"a to "z | yours: 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.
Your task
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.
- Register t holds all three of Tom’s lines (not done yet)
- Set a mark and jump back to it (not done yet)
- Tom’s list has his three lines, saved (not done yet)
The checklist ticks itself off as you work in the terminal: any way that gets the result counts.
Hints come one at a time, then one way to do it. Try each before the next.
What this lesson uses, in one place.
"a- Vim: use register a for the next yank, delete or put ("ayy, "ap); "A adds to it instead of replacing it
"0- Vim: the yank register, which keeps your last yank even after you delete something
:reg- Vim: show what every register holds
ma- Vim: set mark a where the cursor is; 'a jumps back to its line
Every everyday command on one page: the Linux commands cheat sheet.