To recover killed text that is no longer the most recent kill, use the
M-y command (yank-pop
). It takes the text previously
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 moves it 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.
If you know how many M-y commands it would take to find the text you want, you can yank that text in one step using C-y with a numeric argument. C-y with an argument restores the text the specified number of entries back in the kill ring. Thus, C-u 2 C-y gets the next-to-the-last block of killed text. It is equivalent to C-y M-y. C-y with a numeric argument starts counting from the "last yank" pointer, and sets the "last yank" pointer to the entry that it yanks.
The length of the kill ring is controlled by the variable
kill-ring-max
; no more than that many blocks of killed text are
saved.
The actual contents of the kill ring are stored in a variable named
kill-ring
; you can view the entire contents of the kill ring with
the command C-h v kill-ring.
Go to the first, previous, next, last section, table of contents.