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

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 DEL Does Not 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 DEL Does Not 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.  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 M-^.

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.

Other Kill Commands
-------------------

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

M-d
     Kill word (kill-word).  See Words.

M-DEL
     Kill word backwards (backward-kill-word).

C-x DEL
     Kill back to beginning of sentence (backward-kill-sentence). See Sentences.

M-k
     Kill to end of sentence (kill-sentence).

C-M-k
     Kill the following balanced expression (kill-sexp).  See Expressions.

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 Words); balanced expressions, with C-M-k (see Expressions); and sentences, with C-x DEL and M-k (see Sentences).

Killing on Graphical Displays
-----------------------------

On graphical displays with window systems, the most recent kill done in Emacs is also the primary selection, if it is more recent than any selection you made in another program.  This means that the paste commands of other window-based applications copy the text that you killed in Emacs.  In addition, Emacs yank commands treat other applications' selections as part of the kill ring, so you can yank them into Emacs.

Many graphical applications follow the convention that insertion while text is selected deletes the selected text.  You can make Emacs behave this way by enabling Delete Selection mode---with M-x delete-selection-mode or using Custom.  Another effect of this mode is that DEL, C-d and some other keys, when a selection exists, will kill the whole selection.  It also enables Transient Mark mode (see Transient Mark).

Yanking
=======

Yanking means reinserting text previously killed.  This is what some systems call ``pasting.''  The usual way to move or copy text is to kill it and then yank it elsewhere one or more times.  This is very safe because Emacs remembers many recent kills, not just the last one.

C-y
     Yank last killed text (yank).

M-y
     Replace text just yanked with an earlier batch of killed text (yank-pop).

M-w
     Save region as last killed text without actually killing it (kill-ring-save).  Some systems call this ``copying''.

C-M-w
     Append next kill to last batch of killed text (append-next-kill).

On graphical displays with window systems, if there is a current selection in some other application, and you selected it more recently than you killed any text in Emacs, C-y copies the selection instead of text killed within Emacs.

The Kill Ring
-------------

All killed text is recorded in the kill ring, a list of blocks of text that have been killed.  There is only one kill ring, shared by all buffers, so you can kill text in one buffer and yank it in another buffer. This is the usual way to move text from one file to another. (see Accumulating Text, for some other ways.)

The command C-y (yank) reinserts the text of the most recent kill.  It leaves the cursor at the end of the text.  It sets the mark at the beginning of the text.  See Mark.

C-u C-y leaves the cursor in front of the text, and sets the mark after it.  This happens only if the argument is specified with just a C-u, precisely.  Any other sort of argument, including C-u and digits, specifies an earlier kill to yank (see Earlier Kills).

The yank commands discard certain text properties from the text that is yanked, those that might lead to annoying results.  For instance, they discard text properties that respond to the mouse or specify key bindings.  The variable yank-excluded-properties specifies the properties to discard.  Yanking of register contents and rectangles also discard these properties.

To copy a block of text, you can use M-w (kill-ring-save), which copies the region into the kill ring without removing it from the buffer.  This is approximately equivalent to C-w followed by C-x u, except that M-w does not alter the undo history and does not temporarily change the screen.

Appending Kills
---------------

Normally, each kill command pushes a new entry onto the kill ring. However, two or more kill commands in a row combine their text into a single entry, so that a single C-y yanks all the text as a unit, just as it was before it was killed.

Thus, if you want to yank text as a unit, you need not kill all of it with one command; you can keep killing line after line, or word after word, until you have killed it all, and you can still get it all back at once.

Commands that kill forward from point add onto the end of the previous killed text.  Commands that kill backward from point add text onto the beginning.  This way, any sequence of mixed forward and backward kill commands puts all the killed text into one entry without rearrangement. Numeric arguments do not break the sequence of appending kills.  For example, suppose the buffer contains this text:

     This is a line -!-of sample text.

with point shown by -!-.  If you type M-d M-DEL M-d M-DEL, killing alternately forward and backward, you end up with a line of sample as one entry in the kill ring, and This is  text. in the buffer.  (Note the double space between is and text, which you can clean up with M-SPC or M-q.)

Another way to kill the same text is to move back two words with M-b M-b, then kill all four words forward with C-u M-d. This produces exactly the same results in the buffer and in the kill ring.  M-f M-f C-u M-DEL kills the same text, all going backward; once again, the result is the same.  The text in the kill ring entry always has the same order that it had in the buffer before you killed it.

If a kill command is separated from the last kill command by other commands (not just numeric arguments), it starts a new entry on the kill ring.  But you can force it to append by first typing the command C-M-w (append-next-kill) right before it.  The C-M-w tells the following command, if it is a kill command, to append the text it kills to the last killed text, instead of starting a new entry.  With C-M-w, you can kill several separated pieces of text and accumulate them to be yanked back in one place.

A kill command following M-w does not append to the text that M-w copied into the kill ring.

Yanking Earlier Kills
---------------------

To recover killed text that is no longer the most recent kill, use the M-y command (yank-pop).  It takes the text previously yanked and replaces it with the text from an earlier kill.  So, to recover the text of the next-to-the-last kill, first use C-y to yank the last kill, and then use M-y to replace it with the previous kill.  M-y is allowed only after a C-y or another M-y.

You can understand M-y in terms of a ``last yank'' pointer which points at an entry in the kill ring.  Each time you kill, the ``last yank'' pointer moves to the newly made entry at the front of the ring. C-y yanks the entry which the ``last yank'' pointer points to. M-y moves the ``last yank'' pointer to a different entry, and the text in the buffer changes to match.  Enough M-y commands can move the pointer to any entry in the ring, so you can get any entry into the buffer.  Eventually the pointer reaches the end of the ring; the next M-y loops back around to the first entry again.

M-y moves the ``last yank'' pointer around the ring, but it does not change the order of the entries in the ring, which always runs from the most recent kill at the front to the oldest one still remembered.

M-y can take a numeric argument, which tells it how many entries to advance the ``last yank'' pointer by.  A negative argument moves the pointer toward the front of the ring; from the front of the ring, it moves ``around'' to the last entry and continues forward from there.

Once the text you are looking for is brought into the buffer, you can stop doing M-y commands and it will stay there.  It's just a copy of the kill ring entry, so editing it in the buffer does not change what's in the ring.  As long as no new killing is done, the ``last yank'' pointer remains at the same place in the kill ring, so repeating C-y will yank another copy of the same previous kill.

If you know how many M-y commands it would take to find the text you want, you can yank that text in one step using C-y with a numeric argument.  C-y with an argument restores the text from the specified kill ring entry, counting back from the most recent as 1.  Thus, C-u 2 C-y gets the next-to-the-last block of killed text---it is equivalent to C-y M-y.  C-y with a numeric argument starts counting from the ``last yank'' pointer, and sets the ``last yank'' pointer to the entry that it yanks.

The length of the kill ring is controlled by the variable kill-ring-max; no more than that many blocks of killed text are saved.

The actual contents of the kill ring are stored in a variable named kill-ring; you can view the entire contents of the kill ring with the command C-h v kill-ring.

Accumulating Text
=================

Usually we copy or move text by killing it and yanking it, but there are other convenient methods for copying one block of text in many places, or for copying many scattered blocks of text into one place.  To copy one block to many places, store it in a register (see Registers).  Here we describe the commands to accumulate scattered pieces of text into a buffer or into a file.

M-x append-to-buffer
     Append region to the contents of a specified buffer.

M-x prepend-to-buffer
     Prepend region to the contents of a specified buffer.

M-x copy-to-buffer
     Copy region into a specified buffer, deleting that buffer's old contents.

M-x insert-buffer
     Insert the contents of a specified buffer into current buffer at point.

M-x append-to-file
     Append region to the contents of a specified file, at the end.

To accumulate text into a buffer, use M-x append-to-buffer. This reads a buffer name, then inserts a copy of the region into the buffer specified.  If you specify a nonexistent buffer, append-to-buffer creates the buffer.  The text is inserted wherever point is in that buffer.  If you have been using the buffer for editing, the copied text goes into the middle of the text of the buffer, starting from wherever point happens to be at that moment.

Point in that buffer is left at the end of the copied text, so successive uses of append-to-buffer accumulate the text in the specified buffer in the same order as they were copied.  Strictly speaking, append-to-buffer does not always append to the text already in the buffer---it appends only if point in that buffer is at the end. However, if append-to-buffer is the only command you use to alter a buffer, then point is always at the end.

M-x prepend-to-buffer is just like append-to-buffer except that point in the other buffer is left before the copied text, so successive prependings add text in reverse order.  M-x copy-to-buffer is similar, except that any existing text in the other buffer is deleted, so the buffer is left containing just the text newly copied into it.

To retrieve the accumulated text from another buffer, use the command M-x insert-buffer; this too takes BUFFERNAME as an argument.  It inserts a copy of the whole text in buffer BUFFERNAME into the current buffer at point, and sets the mark after the inserted text.  Alternatively, you can select the other buffer for editing, then copy text from it by killing. See Buffers, for background information on buffers.

Instead of accumulating text within Emacs, in a buffer, you can append text directly into a file with M-x append-to-file, which takes FILENAME as an argument.  It adds the text of the region to the end of the specified file.  The file is changed immediately on disk.

You should use append-to-file only with files that are not being visited in Emacs.  Using it on a file that you are editing in Emacs would change the file behind Emacs's back, which can lead to losing some of your editing.

Rectangles
==========

The rectangle commands operate on rectangular areas of the text: all the characters between a certain pair of columns, in a certain range of lines.  Commands are provided to kill rectangles, yank killed rectangles, clear them out, fill them with blanks or text, or delete them.  Rectangle commands are useful with text in multicolumn formats, and for changing text into or out of such formats.

When you must specify a rectangle for a command to work on, you do it by putting the mark at one corner and point at the opposite corner.  The rectangle thus specified is called the region-rectangle because you control it in much the same way as the region is controlled.  But remember that a given combination of point and mark values can be interpreted either as a region or as a rectangle, depending on the command that uses them.

If point and the mark are in the same column, the rectangle they delimit is empty.  If they are in the same line, the rectangle is one line high.  This asymmetry between lines and columns comes about because point (and likewise the mark) is between two columns, but within a line.

C-x r k
     Kill the text of the region-rectangle, saving its contents as the ``last killed rectangle'' (kill-rectangle).

C-x r d
     Delete the text of the region-rectangle (delete-rectangle).

C-x r y
     Yank the last killed rectangle with its upper left corner at point (yank-rectangle).

C-x r o
     Insert blank space to fill the space of the region-rectangle (open-rectangle).  This pushes the previous contents of the region-rectangle rightward.

C-x r c
     Clear the region-rectangle by replacing its contents with spaces (clear-rectangle).

M-x delete-whitespace-rectangle
     Delete whitespace in each of the lines on the specified rectangle, starting from the left edge column of the rectangle.

C-x r t STRING RET
     Replace rectangle contents with STRING on each line. (string-rectangle).

M-x string-insert-rectangle RET STRING RET
     Insert STRING on each line of the rectangle.

The rectangle operations fall into two classes: commands for deleting and inserting rectangles, and commands for blank rectangles.

There are two ways to get rid of the text in a rectangle: you can discard the text (delete it) or save it as the ``last killed'' rectangle.  The commands for these two ways are C-x r d (delete-rectangle) and C-x r k (kill-rectangle).  In either case, the portion of each line that falls inside the rectangle's boundaries is deleted, causing any following text on the line to move left into the gap.

Note that ``killing'' a rectangle is not killing in the usual sense; the rectangle is not stored in the kill ring, but in a special place that can only record the most recent rectangle killed.  This is because yanking a rectangle is so different from yanking linear text that different yank commands have to be used.  It is hard to define yank-popping for rectangles, so we do not try.

To yank the last killed rectangle, type C-x r y (yank-rectangle).  Yanking a rectangle is the opposite of killing one.  Point specifies where to put the rectangle's upper left corner. The rectangle's first line is inserted there, the rectangle's second line is inserted at the same horizontal position, but one line vertically down, and so on.  The number of lines affected is determined by the height of the saved rectangle.

You can convert single-column lists into double-column lists using rectangle killing and yanking; kill the second half of the list as a rectangle and then yank it beside the first line of the list. See Two-Column, for another way to edit multi-column text.

You can also copy rectangles into and out of registers with C-x r r R and C-x r i R.  See Rectangle Registers.

There are two commands you can use for making blank rectangles: C-x r c (clear-rectangle) which blanks out existing text, and C-x r o (open-rectangle) which inserts a blank rectangle.  Clearing a rectangle is equivalent to deleting it and then inserting a blank rectangle of the same size.

The command M-x delete-whitespace-rectangle deletes horizontal whitespace starting from a particular column.  This applies to each of the lines in the rectangle, and the column is specified by the left edge of the rectangle.  The right edge of the rectangle does not make any difference to this command.

The command C-x r t (string-rectangle) replaces the contents of a region-rectangle with a string on each line.  The string's width need not be the same as the width of the rectangle.  If the string's width is less, the text after the rectangle shifts left; if the string is wider than the rectangle, the text after the rectangle shifts right.

The command M-x string-insert-rectangle is similar to string-rectangle, but inserts the string on each line, shifting the original text to the right.

CUA Bindings
============

The command M-x cua-mode sets up key bindings that are compatible with the Common User Access (CUA) system used in many other applications.  C-x means cut (kill), C-c copy, C-v paste (yank), and C-z undo.  Standard Emacs commands like C-x C-c still work, because C-x and C-c only take effect when the mark is active (and the region is highlighted). However, if you don't want to override these bindings Emacs at all, set cua-enable-cua-keys to nil.

In CUA mode, using Shift together with the movement keys activates and highlights the region over which they move.  The standard (unshifted) movement keys deactivate the mark, and typed text replaces the active region as in Delete-Selection mode (see Graphical Kill).

To enter an Emacs command like C-x C-f while the mark is active, use one of the following methods: either hold Shift together with the prefix key, e.g. S-C-x C-f, or quickly type the prefix key twice, e.g. C-x C-x C-f.

CUA mode provides enhanced rectangle support with visible rectangle highlighting.  Use C-RET to start a rectangle, extend it using the movement commands, and cut or copy it using C-x or C-c.  RET moves the cursor to the next (clockwise) corner of the rectangle, so you can easily expand it in any direction.  Normal text you type is inserted to the left or right of each line in the rectangle (on the same side as the cursor).

With CUA you can easily copy text and rectangles into and out of registers by providing a one-digit numeric prefix the the kill, copy, and yank commands, e.g. C-1 C-c copies the region into register 1, and C-2 C-v yanks the contents of register 2.

CUA mode also has a global mark feature which allows easy moving and copying of text between buffers.  Use C-S-SPC to toggle the global mark on and off.  When the global mark is on, all text that you kill or copy is automatically inserted at the global mark, and text you type is inserted at the global mark rather than at the current position.

For example, to copy words from various buffers into a word list in a given buffer, set the global mark in the target buffer, then navigate to each of the words you want in the list, mark it (e.g. with S-M-f), copy it to the list with C-c or M-w, and insert a newline after the word in the target list by pressing RET.