Выбрать главу

Initial state:

The cursor is in upper-left corner.

Type: C-k C-k C-k C-k

You have deleted the first two lines with C-k.

Type: C-y

You got everything back with a single command.

What exactly goes into the kill ring? Everything you delete with C-k in addition to everything you delete with C-w and everything you copy with M-w (two commands that you'll learn shortly) go into the kill ring. Words, sentences, and paragraphs that you delete with M-d, M-Del, and their relatives also go into the kill ring. In addition, text that you delete with C-u followed by either Del or C-d goes into the kill ring. About the only thing that Emacs doesn't save in the kill ring is single characters, deleted with Del or C-d. (If you need to, you can get this type of deletion back using the undo command, bound to both C-_ and C-x u.)

Emacs is clever about what it puts into the kill ring: when it is assembling a big block of text from a group of deletions, it always assembles the text correctly. For example, you can type a few M-d's, followed by some M-Del's, with a couple of C-k's thrown in. When you type C-y, Emacs yanks all the text that you've deleted in the proper order.

However, there's one thing you have to watch out for. Emacs stops assembling these blocks of text as soon as you give any command that isn't a kill command. For example, if you type C-k, then delete a single character with C-d, then type another C-k, you've broken the chain. Emacs doesn't consider deletion of a single character with C-d a "kill" command; it's just a deletion and it isn't stored. In this case, you haven't made a single chain of kill commands; you've made two chains. Later, we'll see how to get the older killed text back.

Table 2-3 summarizes the commands for deleting, killing, and yanking text, including options from the Edit menu.

Table 2-3. Deletion commands

Keystrokes Command name Action
C-d delete-char Delete character under cursor.
Del delete-backward-char Delete previous character.
M-d kill-word Delete next word.
M-Del backward-kill-word Delete previous word.
C-k kill-line Delete from cursor to end of line.
M-k kill-sentence Delete next sentence.
C-x Del backward-kill-sentence Delete previous sentence.
C-y yank Restore what you've deleted.
C-w EditCut kill-region Delete a marked region (see next section).
(none) kill-paragraph Delete next paragraph.
(none) backward-kill-paragraph Delete previous paragraph.

2.3 Marking Text to Delete, Move, or Copy

What if the text you want to delete is just a phrase? Or half a paragraph? Or several paragraphs? In Emacs, you select text by defining an area called a region. You can mark regions with the mouse or by using the keyboard. What happens with the mouse is a bit complicated, so we describe it later in this chapter, following our discussion of the system clipboard.

To define a region using the keyboard, you use a secondary pointer called a mark. Some versions of Emacs display the mark on the screen; unfortunately, in GNU Emacs, the mark is invisible.

You set the mark at one end of the region by pressing C-Space or C-@, then move the cursor to the other end of the region. (The cursor is sometimes also referred to as point. There is one minor but important difference between the cursor and the point, however. The cursor is on top of a character; in Emacs, the point is actually in between the character the cursor is on and the previous character. As we said, this difference is minor, but it helps you to visualize where the cursor should be when you mark a region.) Figure 2-5 illustrates point, mark, and region.

Figure 2-5. Point, mark, and region

Let's mark a sample region. In this example, we remove the phrase "it was the worst of times." First, we find the beginning of the phrase. Then we set the mark, move forward to the end of the phrase, and cut.

Move to the beginning of "it" and press C-Space.

Set the mark; Mark set appears in the minibuffer.

Move to the "i" in "it was the age of wisdom." Because the point is really just before the "i," this placement will be just right.

Move to the "i" in "it was the age of wisdom"