[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

14. Yanking

Yanking means reinserting text previously killed. This is what some systems call "pasting." The usual way to move or copy text is to kill it and then yank it elsewhere one or more times. This is very safe because Emacs remembers many recent kills, not just the last one.

C-y

Yank last killed text (yank).

M-y

Replace text just yanked with an earlier batch of killed text (yank-pop).

M-w

Save region as last killed text without actually killing it (kill-ring-save). Some systems call this "copying."

C-M-w

Append next kill to last batch of killed text (append-next-kill).

On graphical displays with window systems, if there is a current selection in some other application, and you selected it more recently than you killed any text in Emacs, C-y copies the selection instead of text killed within Emacs.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

14.1 The Kill Ring

All killed text is recorded in the kill ring, a list of blocks of text that have been killed. There is only one kill ring, shared by all buffers, so you can kill text in one buffer and yank it in another buffer. This is the usual way to move text from one file to another. (See section Accumulating Text, for some other ways.)

The command C-y (yank) reinserts the text of the most recent kill. It leaves the cursor at the end of the text. It sets the mark at the beginning of the text. See section The Mark and the Region.

C-u C-y leaves the cursor in front of the text, and sets the mark after it. This happens only if the argument is specified with just a C-u, precisely. Any other sort of argument, including C-u and digits, specifies an earlier kill to yank (see section Yanking Earlier Kills).

The yank commands discard certain text properties from the text that is yanked, those that might lead to annoying results. For instance, they discard text properties that respond to the mouse or specify key bindings. The variable yank-excluded-properties specifies the properties to discard. Yanking of register contents and rectangles also discard these properties.

To copy a block of text, you can use M-w (kill-ring-save), which copies the region into the kill ring without removing it from the buffer. This is approximately equivalent to C-w followed by C-x u, except that M-w does not alter the undo history and does not temporarily change the screen.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

14.2 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 append by first typing the command C-M-w (append-next-kill) right before it. The C-M-w tells the following command, if it is a kill command, to append the text it kills to the last killed text, instead of starting a new entry. With C-M-w, you can kill several separated pieces of text and accumulate them to be yanked back in one place.

A kill command following M-w does not append to the text that M-w copied into the kill ring.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

14.3 Yanking Earlier Kills

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 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.

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 from the specified kill ring entry, counting back from the most recent as 1. 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.


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by Mark Kaminski on July, 3 2008 using texi2html 1.70.