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-Spaceor 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-5illustrates 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 setappears 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"

The point is at the end of the region to be marked.
Now the region is marked. If the region is not highlighted, you'll want to make sure it is marked correctly before giving the delete command. Press C-x C-x(for exchange-point-and-mark); this command swaps the locations of the mark and the point. If the cursor moves to where you thought the mark was, the region is marked correctly. Especially because you can't see the mark, it's a good habit to check its location using C-x C-xbefore deleting a region. People who have used Emacs for years still forget to set the mark and then make a deletion without knowing what they've just deleted. (The undo command, bound to C-_and C-x u, comes in handy in such a case.)
To cut the region, press C-w(for kill-region). (The scissors icon on the toolbar also works.)
Press: C-w

C-wcuts the region.
If you're not sure of what you deleted, just press C-_to undo it. The text is still marked, and you can delete it again with C-wif you want to. To move text, mark it, press C-wto cut the region, then move the cursor to the place you want to insert the text, and press C-y. If you yank the text back into the wrong location, just type C-_to undo it, then move to the place you really wanted to put the text, and press C-yagain.
When you're defining a region, you normally set the mark at one end and then move the cursor to the other end of the region. A few shortcuts are helpful in some of the most common situations. To mark a paragraph, press M-h. This sets the mark at the end of the paragraph and places the cursor at the beginning automatically. Similarly, C-x h(for mark-whole-buffer) marks the entire buffer; the cursor goes to the beginning, and the mark is placed at the end. Finally, C-x C-pmarks the current page, with pages being defined by the C-lcharacter if you are in text mode. Of course, marking a paragraph, page, or buffer is usually only the prelude to some other operation, like killing ( C-w).
To copy text, mark a region, then press M-w(for kill-ring-save; the toolbar icon with two pieces of paper also runs this command). Move the cursor to the place where you want to insert the copied text and press C-y. Copying text is exactly the same as killing it, except that Emacs doesn't delete anything. The text you have copied is placed in the kill ring, so you can use C-yto access it as often as you like.
One advantage to M-wis that it works on read-only files and buffers. For example, if you wanted to create a file of Emacs hints, you could use M-wto copy some text from online help into one of your buffers.
Here are the steps for some common deletion tasks.
To mark a region:
1. Move the cursor to the beginning of the area you want to delete.
2. Press C-Space. Emacs displays the message Mark set.
3. Move the cursor to the end of the region you want to delete.
To delete a region:
1. Mark the region to be deleted.
2. Press C-wto delete the region.
To move text:
1. Delete the text you want to move using the procedures for marking and deleting a region.
2. Move the cursor where you want to insert the text.
3. Press C-y. Emacs inserts the text you deleted.
To copy text:
1. Mark the region you want to copy.
2. Press M-wto copy the text.
3. Move the cursor where you want to insert the copied text and press C-y. Emacs inserts the text you copied.
2.3.2 Recovering Earlier Deletions
Earlier we mentioned the kill ring, a temporary storage area in which Emacs saves the stuff you delete. So far, we've assumed that you're interested in resurrecting what you've most recently killed. However, the kill ring does a lot more. It actually stores your last 30 deletions. We've seen that C-yrestores the text you deleted most recently. Typing M-ydeletes the text you just yanked and gets the next most recent text from the kill ring.
Here's how it works. In Table 2-4, assume that you've just killed the words "most recent." C-yretrieves these words from the kill ring. When you press M-y, Emacs gets rid of "most recent" and gets the next entry from the kill ring ("second-last").
Table 2-4. The kill ring in action
Keystrokes |
Action |
C-y |
This was the most recent _ deletion. |
M-y |
This was the second-last _ deletion. |
M-y |
This was the third-last _ deletion. |
M-y |
This was the fourth-last _ deletion. |
You can keep on typing M-y, retrieving successively more ancient deletions, until you reach the end of the kill ring (at which point it cycles back to the most recently killed text; that's why it's called a ring ).
Читать дальше