Advanced kill and yank commands
You already know about killing and yanking text, which were described in the first tutorial in this series (see Resources), but there's more to it than those basic operations. This section describes some advanced techniques.
You know that
C-k kills everything from point to the
end of the line, but a few other interesting kill commands work on other parts
of a buffer.
If you precede
C-k with a numeric argument -- using
either the Meta key (as described in
"Specify a digit
prefix") or with the
universal argument -- you kill that many lines.
If you precede
C-k with a zero, you kill all the
text from point to the beginning of the line.
Try killing everything to the left of point on the current line: Type
Now, when you yank, you bring back the beginning of the line. Try it:
You can also go backward. A negative argument kills that many lines backward,
beginning with everything on the current line before point. For example,
C-k kills from point to the beginning of the
line and five lines before it.
M-k, to kill all the text from point to the end of
the current sentence.
Try it out:
- Move point to the line that begins with In what distant: Type
M-< M-6 C-n M-2 C-f.
M-kto kill the sentence.
C-k, this command starts its kill from point --
and it can also take a numeric argument. So,
kills from point to the beginning of the sentence.
zap-to-char function, which is bound to
M-z, is useful for deleting a range of characters
from the cursor up to and including a particular character. Run it by typing
M-z and then giving the character to zap to.
Try it now:
Dto zap everything up to and including the first D character.
As with the other commands described here when preceded with a numeric argument, this function zaps to that number of the given character, either forward from point (a positive integer) or backward (a negative integer).
Try zapping everything from point to the fifth e character behind it:
M-- M-5 M-z e.
Just as there's a mark ring to save the marks that are set (as described in Use the mark ring), there's also a kill ring, which saves the text that's killed each time you kill it. Each time you kill some text, it's saved in a slot in the kill ring. The only exception is a rectangle -- the last rectangle you kill is saved in its own special place outside of the kill ring (see Rectangles). Every buffer's kill ring has 30 slots in it by default.
When you type
C-y to yank the last text you killed,
you actually yank from the top of the kill ring. To go back one slot further in
the kill ring, type
M-y. Doing so replaces the text
you just yanked with the previous text in the kill ring. Each time you type
M-y again, you go back further in the kill ring.
You can also precede the usual
C-y with a numeric
argument to yank that particular position in the kill ring, with the most recent
being the lowest number in the ring -- so
C-u 1 C-y
is the same as
C-y, which is the same as a simple
Try yanking the third item in the kill ring at point: Type
C-u C-y, with no numeric arguments, has a
special meaning. Normally,
C-y yanks the text at the
top entry in the kill ring at point, and then point moves to the end of the
yanked text. But with
C-u C-y, point remains at the
beginning of the text you yank. This is good for editing the text you're about
Try it now by yanking text at the beginning of the line: Type
C-a C-u C-y.
You can look at the contents of the kill ring by viewing the
kill-ring variable. Try it: Type
C-h v kill-ring.
This command opens a new window showing the contents of the kill ring. Type
C-x 1 to close the window.
Table 3 provides a summary of the advanced kill and yank commands described in this section.
Table 3. Advanced Emacs kill and yank commands
||Kills integer number of lines. If 0, kills from point to the beginning of the line; if negative, kills in reverse.|
||Kills from point to the end of the sentence.|
||Zaps all text from point to the specified character.|
||Moves to the next slot in the kill ring.|
||Yanks the specified slot in the kill ring.|