2.2. Deleting Text


Before you start practicing deletion commands, you might want to know the undo command, which is discussed fully later in this chapter. Typing C-_ or C-x u undoes your last edit; typing undo again undoes the edit before that one, and so on.

Emacs provides many ways to delete text. The simplest way to delete text is to press the Del key, which deletes the character immediately to the left of the cursor. See Figure 2-4 for possible locations of the Del key on your keyboard. It is sometimes referred to as the Backspace key. Del is 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 Meta key augments the command: C-d operates on a character, and M-d operates 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-d acts differently depending on where the cursor is.

If the cursor is here:

M-d makes this edit:

It was the worst of times

It was the wof times

It was the worst of times

It was theof times

It was the worst of times

It was the worsof 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-k on 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-k to join two lines. If you're at the end of a line, C-k deletes the newline character, effectively making two lines into one long line.

2.2.1 The Kill Ring

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).[3] 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-y command will bring everything back. In the following example, we'll use C-k four times to delete the first two lines of A Tale of Two Cities. (Remember: the first C-k deletes the text; the second C-k deletes the remaining blank line.) Then we'll use a single C-y to bring everything back.

[3] You may be used to pressing C-v to paste in all applications if you are a Linux or Windows user. Emacs has options to change its default paste, cut, and copy commands to the familiar C-v, C-x, and C-c. See "Making Emacs Work the Way You Want" for details. Also, a quick warning to vi users who are learning Emacs: vi also uses the term yank, but its meaning is almost the exact opposite. Don't let this confuse you.

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

kill-region

Delete a marked region (see next section).

(none)

kill-paragraph

Delete next paragraph.

(none)

backward-kill-paragraph

Delete previous paragraph.




Learning GNU Emacs
Learning GNU Emacs, Third Edition
ISBN: 0596006489
EAN: 2147483647
Year: 2003
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net