Emacs provides many ways to delete text. The simplest way to delete text is to press the Delkey, which deletes the character immediately to the left of the cursor. See Figure 2-4for possible locations of the Delkey on your keyboard. It is sometimes referred to as the Backspacekey. Delis easiest to define by what it does: it deletes the previous character. If you're typing and you decide to erase the last character you typed, what key do you reach for? That's the key Emacs refers to as Del.
Emacs provides a number of other deletion commands—perhaps too many for your taste, although you'll eventually find a reason to use most of them. For example, C-d(for delete-character) deletes the character under the cursor. The command for deleting the next word is M-d(for kill-word). Once again, note how the Metakey augments the command: C-doperates on a character, and M-doperates on a word.
Emacs has commands to delete the next or previous word, sentence, and paragraph. By their names, you can guess what they do when you're between words, sentences, or paragraphs. If you're in the middle of an entity, however, they do something a little surprising: they delete a portion of the current word, sentence, or paragraph, backward or forward depending on whether the command deletes previous or next. For example, here's how M-dacts differently depending on where the cursor is.
If the cursor is here: |
M-d makes this edit: |
It was the w orst of times |
It was the w _ of times |
It was the worst of times |
It was the _ of times |
It was the wors tof times |
It was the wors _ of times |
Similarly, if you are in the middle of a word and ask Emacs to delete the previous word ( M-Del, for backward-kill-word), it deletes from the cursor position back to the beginning of the current word.
If you want to delete an entire line or part of a line, use the command C-k(for kill-line). This command deletes everything from the cursor to the end of the line. Typing C-kon a blank line deletes the line itself. So, it usually takes two C-k's to delete a line: one to delete the text and one to delete the resulting blank line. If you want to delete everything from the beginning of the line up to the cursor, try the more complex incantation Meta - C-k(i.e., hold down Meta, followed by a hyphen, and then C-k).
You can also use C-kto join two lines. If you're at the end of a line, C-kdeletes the newline character, effectively making two lines into one long line.
By now you may have noticed that some deletion commands in Emacs are called kill commands, such as kill-region, kill-word, and the like. In Emacs, killing is not fatal, but in fact, quite the opposite. Text that has been killed is not gone forever but is hidden in an area called the kill ring . The kill ring, though it sounds somewhat like a violent gang, is an internal storage area where Emacs puts things you've copied or deleted. Do not confuse the kill ring with the system clipboard, which allows for copying and pasting between applications. We'll cover how Emacs relates to the system clipboard later in this chapter.
You can get back what you've deleted by typing C-y(for yank). [12]Conveniently, if you kill several lines in succession, Emacs collects them into a single item and places the whole unit into the kill ring; a single C-ycommand will bring everything back. In the following example, we'll use C-kfour times to delete the first two lines of A Tale of Two Cities . (Remember: the first C-kdeletes the text; the second C-kdeletes the remaining blank line.) Then we'll use a single C-yto bring everything back.
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-kin addition to everything you delete with C-wand 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-ufollowed by either Delor C-dgoes into the kill ring. About the only thing that Emacs doesn't save in the kill ring is single characters, deleted with Delor C-d. (If you need to, you can get this type of deletion back using the undocommand, 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-da "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-3summarizes the commands for deleting, killing, and yanking text, including options from the Editmenu.
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 Edit → Cut |
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
Читать дальше