12.2.2 Yanking Earlier Kills
As explained in Yanking, you can use a numeric argument to C-y
to yank text that is no longer the most recent kill. This is useful if you remember which kill ring entry you want. If you don’t, you can use the M-y
(yank-pop
) command to cycle through the possibilities.
If the previous command was a yank command, M-y
takes the text that was yanked and replaces it with the text from an earlier kill. So, to recover the text of the next-to-the-last kill, first use C-y
to yank the last kill, and then use M-y
to replace it with the previous kill. M-y
is allowed only after a C-y
or another M-y
.
You can understand M-y
in terms of a last-yank pointer which points at an entry in the kill ring. Each time you kill, the last-yank pointer moves to the newly made entry at the front of the ring. C-y
yanks the entry which the last-yank pointer points to. M-y
moves the last-yank pointer to a different entry, and the text in the buffer changes to match. Enough M-y
commands can move the pointer to any entry in the ring, so you can get any entry into the buffer. Eventually the pointer reaches the end of the ring; the next M-y
loops back around to the first entry again.
M-y
moves the last-yank pointer around the ring, but it does not change the order of the entries in the ring, which always runs from the most recent kill at the front to the oldest one still remembered.
M-y
can take a numeric argument, which tells it how many entries to advance the last-yank pointer by. A negative argument moves the pointer toward the front of the ring; from the front of the ring, it moves around to the last entry and continues forward from there.
Once the text you are looking for is brought into the buffer, you can stop doing M-y
commands and it will stay there. It’s just a copy of the kill ring entry, so editing it in the buffer does not change what’s in the ring. As long as no new killing is done, the last-yank pointer remains at the same place in the kill ring, so repeating C-y
will yank another copy of the same previous kill.
When you call C-y
with a numeric argument, that also sets the last-yank pointer to the entry that it yanks.