12.2.3 Appending Kills
Normally, each kill command pushes a new entry onto the kill ring. However, two or more kill commands in a row combine their text into a single entry, so that a single C-y
yanks all the text as a unit, just as it was before it was killed.
Thus, if you want to yank text as a unit, you need not kill all of it with one command; you can keep killing line after line, or word after word, until you have killed it all, and you can still get it all back at once.
Commands that kill forward from point add onto the end of the previous killed text. Commands that kill backward from point add text onto the beginning. This way, any sequence of mixed forward and backward kill commands puts all the killed text into one entry without rearrangement. Numeric arguments do not break the sequence of appending kills. For example, suppose the buffer contains this text:
This is a line ∗of sample text.
with point shown by ∗. If you type M-d M-DEL M-d M-DEL
, killing alternately forward and backward, you end up with ‘a line of sample
’ as one entry in the kill ring, and ‘This is text.
’ in the buffer. (Note the double space between ‘is
’ and ‘text
’, which you can clean up with M-SPC
or M-q
.)
Another way to kill the same text is to move back two words with M-b M-b
, then kill all four words forward with C-u M-d
. This produces exactly the same results in the buffer and in the kill ring. M-f M-f C-u M-DEL
kills the same text, all going backward; once again, the result is the same. The text in the kill ring entry always has the same order that it had in the buffer before you killed it.
If a kill command is separated from the last kill command by other commands (not just numeric arguments), it starts a new entry on the kill ring. But you can force it to combine with the last killed text, by typing C-M-w
(append-next-kill
) right beforehand. The C-M-w
tells its following command, if it is a kill command, to treat the kill as part of the sequence of previous kills. As usual, the kill is appended to the previous killed text if the command kills forward, and prepended if the command kills backward. In this way, you can kill several separated pieces of text and accumulate them to be yanked back in one place.
A kill command following M-w
(kill-ring-save
) does not append to the text that M-w
copied into the kill ring.