[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

13. Killing and Moving Text

Killing means erasing text and copying it into the kill ring, from which you can bring it back into the buffer by yanking it. (Some systems use the terms "cutting" and "pasting" for these operations.) This is the most common way of moving or copying text within Emacs. Killing and yanking is very safe because Emacs remembers several recent kills, not just the last one. It is versatile, because the many commands for killing syntactic units can also be used for moving those units. But there are other ways of copying text for special purposes.

Most commands which erase text from the buffer save it in the kill ring. These commands are known as kill commands. The commands that erase text but do not save it in the kill ring are known as delete commands. The C-x u (undo) command (see section Undo) can undo both kill and delete commands; the importance of the kill ring is that you can also yank the text in a different place or places. Emacs has only one kill ring for all buffers, so you can kill text in one buffer and yank it in another buffer.

The delete commands include C-d (delete-char) and DEL (delete-backward-char), which delete only one character at a time, and those commands that delete only spaces or newlines. Commands that can erase significant amounts of nontrivial data generally do a kill operation instead. The commands' names and individual descriptions use the words `kill' and `delete' to say which kind of operation they perform.

You cannot kill read-only text, since such text does not allow any kind of modification. But some users like to use the kill commands to copy read-only text into the kill ring, without actually changing it. Therefore, the kill commands work specially in a read-only buffer: they move over text, and copy it to the kill ring, without actually deleting it from the buffer. Normally, kill commands beep and display an error message when this happens. But if you set the variable kill-read-only-ok to a non-nil value, they just print a message in the echo area to explain why the text has not been erased.

You can also use the mouse to kill and yank. See section Killing and Yanking on Graphical Displays.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

13.1 Deletion

Deletion means erasing text and not saving it in the kill ring. For the most part, the Emacs commands that delete text are those that erase just one character or only whitespace.

C-d
DELETE

Delete next character (delete-char). If your keyboard has a DELETE function key (usually located in the edit keypad), Emacs binds it to delete-char as well.

DEL
BS

Delete previous character (delete-backward-char).

M-\

Delete spaces and tabs around point (delete-horizontal-space).

M-SPC

Delete spaces and tabs around point, leaving one space (just-one-space).

C-x C-o

Delete blank lines around the current line (delete-blank-lines).

M-^

Join two lines by deleting the intervening newline, along with any indentation following it (delete-indentation).

The most basic delete commands are C-d (delete-char) and DEL (delete-backward-char). C-d deletes the character after point, the one the cursor is "on top of." This doesn't move point. DEL deletes the character before the cursor, and moves point back. You can delete newlines like any other characters in the buffer; deleting a newline joins two lines. Actually, C-d and DEL aren't always delete commands; when given arguments, they kill instead, since they can erase more than one character this way.

Every keyboard has a large key which is a short distance above the RET or ENTER key and is normally used for erasing what you have typed. It may be labeled DEL, BACKSPACE, BS, DELETE, or even with a left arrow. Regardless of the label on the key, in Emacs it called DEL, and it should delete one character backwards.

Many keyboards (including standard PC keyboards) have a BACKSPACE key a short ways above RET or ENTER, and a DELETE key elsewhere. In that case, the BACKSPACE key is DEL, and the DELETE key is equivalent to C-d--or it should be.

Why do we say "or it should be"? When Emacs starts up using a graphical display, it determines automatically which key or keys should be equivalent to DEL. As a result, BACKSPACE and/or DELETE keys normally do the right things. But in some unusual cases Emacs gets the wrong information from the system. If these keys don't do what they ought to do, you need to tell Emacs which key to use for DEL. See section If DEL Fails to Delete, for how to do this.

On most text-only terminals, Emacs cannot tell which keys the keyboard really has, so it follows a uniform plan which may or may not fit your keyboard. The uniform plan is that the ASCII DEL character deletes, and the ASCII BS (backspace) character asks for help (it is the same as C-h). If this is not right for your keyboard, such as if you find that the key which ought to delete backwards enters Help instead, see If DEL Fails to Delete.

The other delete commands are those which delete only whitespace characters: spaces, tabs and newlines. M-\ (delete-horizontal-space) deletes all the spaces and tab characters before and after point. With a prefix argument, this only deletes spaces and tab characters before point. M-SPC (just-one-space) does likewise but leaves a single space after point, regardless of the number of spaces that existed previously (even if there were none before). With a numeric argument n, it leaves n spaces after point.

C-x C-o (delete-blank-lines) deletes all blank lines after the current line. If the current line is blank, it deletes all blank lines preceding the current line as well (leaving one blank line, the current line). On a solitary blank line, it deletes that line.

M-^ (delete-indentation) joins the current line and the previous line, by deleting a newline and all surrounding spaces, usually leaving a single space. See section M-^.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

13.2 Killing by Lines

C-k

Kill rest of line or one or more lines (kill-line).

C-S-backspace

Kill an entire line at once (kill-whole-line)

The simplest kill command is C-k. If given at the beginning of a line, it kills all the text on the line, leaving it blank. When used on a blank line, it kills the whole line including its newline. To kill an entire non-blank line, go to the beginning and type C-k twice.

More generally, C-k kills from point up to the end of the line, unless it is at the end of a line. In that case it kills the newline following point, thus merging the next line into the current one. Spaces and tabs that you can't see at the end of the line are ignored when deciding which case applies, so if point appears to be at the end of the line, you can be sure C-k will kill the newline.

When C-k is given a positive argument, it kills that many lines and the newlines that follow them (however, text on the current line before point is not killed). With a negative argument -n, it kills n lines preceding the current line (together with the text on the current line before point). Thus, C-u - 2 C-k at the front of a line kills the two previous lines.

C-k with an argument of zero kills the text before point on the current line.

If the variable kill-whole-line is non-nil, C-k at the very beginning of a line kills the entire line including the following newline. This variable is normally nil.

C-S-backspace (kill-whole-line) will kill a whole line including its newline regardless of the position of point within the line. Note that many character terminals will prevent you from typing the key sequence C-S-backspace.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

13.3 Other Kill Commands

C-w

Kill region (from point to the mark) (kill-region).

M-d

Kill word (kill-word). See section Words.

M-DEL

Kill word backwards (backward-kill-word).

C-x DEL

Kill back to beginning of sentence (backward-kill-sentence). See section Sentences.

M-k

Kill to end of sentence (kill-sentence).

C-M-k

Kill the following balanced expression (kill-sexp). See section Expressions with Balanced Parentheses.

M-z char

Kill through the next occurrence of char (zap-to-char).

The most general kill command is C-w (kill-region), which kills everything between point and the mark. With this command, you can kill any contiguous sequence of characters, if you first set the region around them.

A convenient way of killing is combined with searching: M-z (zap-to-char) reads a character and kills from point up to (and including) the next occurrence of that character in the buffer. A numeric argument acts as a repeat count. A negative argument means to search backward and kill text before point.

Other syntactic units can be killed: words, with M-DEL and M-d (see section Words); balanced expressions, with C-M-k (see section Expressions with Balanced Parentheses); and sentences, with C-x DEL and M-k (see section Sentences).


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by Mark Kaminski on July, 3 2008 using texi2html 1.70.