]> code.delx.au - gnu-emacs/blob - doc/emacs/killing.texi
Merge from trunk.
[gnu-emacs] / doc / emacs / killing.texi
1 @c This is part of the Emacs manual.
2 @c Copyright (C) 1985-1987, 1993-1995, 1997, 2000-2011
3 @c Free Software Foundation, Inc.
4 @c See file emacs.texi for copying conditions.
5
6 @node Killing, Registers, Mark, Top
7 @chapter Killing and Moving Text
8
9 In Emacs, @dfn{killing} means erasing text and copying it into the
10 @dfn{kill ring}. @dfn{Yanking} means bringing text from the kill ring
11 back into the buffer. (Some applications use the terms ``cutting''
12 and ``pasting'' for similar operations.) The kill ring is so-named
13 because it can be visualized as a set of blocks of text arranged in a
14 ring, which you can access in cyclic order. @xref{Kill Ring}.
15
16 Killing and yanking are the most common way to move or copy text
17 within Emacs. It is very versatile, because there are commands for
18 killing many different types of syntactic units.
19
20 @menu
21 * Deletion and Killing:: Commands that remove text.
22 * Yanking:: Commands that insert text.
23 * Cut and Paste:: Clipboard and selections on graphical displays.
24 * Accumulating Text:: Other methods to add text to the buffer.
25 * Rectangles:: Operating on text in rectangular areas.
26 * CUA Bindings:: Using @kbd{C-x}/@kbd{C-c}/@kbd{C-v} to kill and yank.
27 @end menu
28
29 @node Deletion and Killing
30 @section Deletion and Killing
31
32 @cindex killing text
33 @cindex cutting text
34 @cindex deletion
35 Most commands which erase text from the buffer save it in the kill
36 ring. These are known as @dfn{kill} commands, and their names
37 normally contain the word @samp{kill} (e.g. @code{kill-line}). The
38 kill ring stores several recent kills, not just the last one, so
39 killing is a very safe operation: you don't have to worry much about
40 losing text that you previously killed. The kill ring is shared by
41 all buffers, so text that is killed in one buffer can be yanked into
42 another buffer.
43
44 When you use @kbd{C-/} (@code{undo}) to undo a kill command
45 (@pxref{Undo}), that brings the killed text back into the buffer, but
46 does not remove it from the kill ring.
47
48 On graphical displays, killing text also copies it to the system
49 clipboard. @xref{Cut and Paste}.
50
51 Commands that erase text but do not save it in the kill ring are
52 known as @dfn{delete} commands; their names usually contain the word
53 @samp{delete}. These include @kbd{C-d} (@code{delete-char}) and
54 @key{DEL} (@code{delete-backward-char}), which delete only one
55 character at a time, and those commands that delete only spaces or
56 newlines. Commands that can erase significant amounts of nontrivial
57 data generally do a kill operation instead.
58
59 You can also use the mouse to kill and yank. @xref{Cut and Paste}.
60
61 @menu
62 * Deletion:: Commands for deleting small amounts of text and
63 blank areas.
64 * Killing by Lines:: How to kill entire lines of text at one time.
65 * Other Kill Commands:: Commands to kill large regions of text and
66 syntactic units such as words and sentences.
67 * Kill Options:: Options that affect killing.
68 @end menu
69
70 @node Deletion
71 @subsection Deletion
72 @findex delete-backward-char
73 @findex delete-char
74
75 Deletion means erasing text and not saving it in the kill ring. For
76 the most part, the Emacs commands that delete text are those that
77 erase just one character or only whitespace.
78
79 @table @kbd
80 @item C-d
81 @itemx @key{Delete}
82 Delete next character (@code{delete-char}).
83 @item @key{DEL}
84 @itemx @key{Backspace}
85 Delete previous character (@code{delete-backward-char}).
86 @item M-\
87 Delete spaces and tabs around point (@code{delete-horizontal-space}).
88 @item M-@key{SPC}
89 Delete spaces and tabs around point, leaving one space
90 (@code{just-one-space}).
91 @item C-x C-o
92 Delete blank lines around the current line (@code{delete-blank-lines}).
93 @item M-^
94 Join two lines by deleting the intervening newline, along with any
95 indentation following it (@code{delete-indentation}).
96 @end table
97
98 We have already described the basic deletion commands @kbd{C-d}
99 (@code{delete-char}) and @key{DEL} (@code{delete-backward-char}).
100 @xref{Erasing}.
101
102 @kindex M-\
103 @findex delete-horizontal-space
104 @kindex M-SPC
105 @findex just-one-space
106 The other delete commands are those that delete only whitespace
107 characters: spaces, tabs and newlines. @kbd{M-\}
108 (@code{delete-horizontal-space}) deletes all the spaces and tab
109 characters before and after point. With a prefix argument, this only
110 deletes spaces and tab characters before point. @kbd{M-@key{SPC}}
111 (@code{just-one-space}) does likewise but leaves a single space after
112 point, regardless of the number of spaces that existed previously
113 (even if there were none before). With a numeric argument @var{n}, it
114 leaves @var{n} spaces after point.
115
116 @kbd{C-x C-o} (@code{delete-blank-lines}) deletes all blank lines
117 after the current line. If the current line is blank, it deletes all
118 blank lines preceding the current line as well (leaving one blank line,
119 the current line). On a solitary blank line, it deletes that line.
120
121 @kbd{M-^} (@code{delete-indentation}) joins the current line and the
122 previous line, by deleting a newline and all surrounding spaces, usually
123 leaving a single space. @xref{Indentation,M-^}.
124
125 @node Killing by Lines
126 @subsection Killing by Lines
127
128 @table @kbd
129 @item C-k
130 Kill rest of line or one or more lines (@code{kill-line}).
131 @item C-S-backspace
132 Kill an entire line at once (@code{kill-whole-line})
133 @end table
134
135 @kindex C-k
136 @findex kill-line
137 The simplest kill command is @kbd{C-k} (@code{kill-line}). If used
138 at the end of a line, it kills the line-ending newline character,
139 merging the next line into the current one (thus, a blank line is
140 entirely removed). Otherwise, @kbd{C-k} kills all the text from point
141 up to the end of the line; if point was originally at the beginning of
142 the line, this leaves the line blank.
143
144 Spaces and tabs at the end of the line are ignored when deciding
145 which case applies. As long as point is after the last visible
146 character in the line, you can be sure that @kbd{C-k} will kill the
147 newline. To kill an entire non-blank line, go to the beginning and
148 type @kbd{C-k} twice.
149
150 In this context, ``line'' means a logical text line, not a screen
151 line (@pxref{Continuation Lines}).
152
153 When @kbd{C-k} is given a positive argument @var{n}, it kills
154 @var{n} lines and the newlines that follow them (text on the current
155 line before point is not killed). With a negative argument
156 @minus{}@var{n}, it kills @var{n} lines preceding the current line,
157 together with the text on the current line before point. @kbd{C-k}
158 with an argument of zero kills the text before point on the current
159 line.
160
161 @vindex kill-whole-line
162 If the variable @code{kill-whole-line} is non-@code{nil}, @kbd{C-k} at
163 the very beginning of a line kills the entire line including the
164 following newline. This variable is normally @code{nil}.
165
166 @kindex C-S-backspace
167 @findex kill-whole-line
168 @kbd{C-S-backspace} (@code{kill-whole-line}) kills a whole line
169 including its newline, regardless of the position of point within the
170 line. Note that many text terminals will prevent you from typing the
171 key sequence @kbd{C-S-backspace}.
172
173 @node Other Kill Commands
174 @subsection Other Kill Commands
175 @findex kill-region
176 @kindex C-w
177
178 @table @kbd
179 @item C-w
180 Kill region (@code{kill-region}). @xref{Mark}.
181 @item M-w
182 Save region as last killed text without actually killing it
183 (@code{kill-ring-save}). Some programs call this ``copying.''
184 @item M-d
185 Kill word (@code{kill-word}). @xref{Words}.
186 @item M-@key{DEL}
187 Kill word backwards (@code{backward-kill-word}).
188 @item C-x @key{DEL}
189 Kill back to beginning of sentence (@code{backward-kill-sentence}).
190 @xref{Sentences}.
191 @item M-k
192 Kill to end of sentence (@code{kill-sentence}).
193 @item C-M-k
194 Kill the following balanced expression (@code{kill-sexp}). @xref{Expressions}.
195 @item M-z @var{char}
196 Kill through the next occurrence of @var{char} (@code{zap-to-char}).
197 @end table
198
199 Apart from @kbd{C-k}, the most commonly-used kill command is
200 @kbd{C-w} (@code{kill-region}), which kills the text in the region
201 (i.e., between point and mark). @xref{Mark}. If the mark is inactive
202 when you type @kbd{C-w}, it first reactivates the mark where it was
203 last set. The mark is deactivated at the end of the command.
204
205 @kindex M-w
206 @findex kill-ring-save
207 @kbd{M-w} (@code{kill-ring-save}) copies the region into the kill
208 ring without removing it from the buffer.
209
210 Emacs also provides commands to kill specific syntactic units:
211 words, with @kbd{M-@key{DEL}} and @kbd{M-d} (@pxref{Words}); balanced
212 expressions, with @kbd{C-M-k} (@pxref{Expressions}); and sentences,
213 with @kbd{C-x @key{DEL}} and @kbd{M-k} (@pxref{Sentences}).
214
215 @kindex M-z
216 @findex zap-to-char
217 The command @kbd{M-z} (@code{zap-to-char}) combines killing with
218 searching: it reads a character and kills from point up to (and
219 including) the next occurrence of that character in the buffer. A
220 numeric argument acts as a repeat count; a negative argument means to
221 search backward and kill text before point.
222
223 @node Kill Options
224 @subsection Options for Killing
225
226 @vindex kill-read-only-ok
227 @cindex read-only text, killing
228 Some specialized buffers contain @dfn{read-only text}, which cannot
229 be modified and therefore cannot be killed. The kill commands work
230 specially in a read-only buffer: they move over text and copy it to
231 the kill ring, without actually deleting it from the buffer.
232 Normally, they also beep and display an error message when this
233 happens. But if you set the variable @code{kill-read-only-ok} to a
234 non-@code{nil} value, they just print a message in the echo area to
235 explain why the text has not been erased.
236
237 @vindex kill-do-not-save-duplicates
238 If you change the variable @code{kill-do-not-save-duplicates} to a
239 non-@code{nil} value, identical subsequent kills yield a single
240 kill-ring entry, without duplication.
241
242 @node Yanking
243 @section Yanking
244 @cindex moving text
245 @cindex copying text
246 @cindex kill ring
247 @cindex yanking
248 @cindex pasting
249
250 @dfn{Yanking} means reinserting text previously killed. The usual
251 way to move or copy text is to kill it and then yank it elsewhere one
252 or more times.
253
254 @table @kbd
255 @item C-y
256 Yank last killed text (@code{yank}).
257 @item M-y
258 Replace text just yanked with an earlier batch of killed text
259 (@code{yank-pop}).
260 @item C-M-w
261 Append next kill to last batch of killed text (@code{append-next-kill}).
262 @end table
263
264 @cindex yanking and text properties
265 @vindex yank-excluded-properties
266 The yank commands discard certain properties from the yanked text.
267 These are properties that might lead to annoying results, such as
268 causing the text to respond to the mouse or specifying key bindings.
269 The list of properties to discard is stored in the variable
270 @code{yank-excluded-properties}. These properties are also discarded
271 when yanking register contents and rectangles. @xref{Text
272 Properties,,, elisp, the Emacs Lisp Reference Manual}, for more
273 information about text properties.
274
275 @menu
276 * Kill Ring:: Where killed text is stored. Basic yanking.
277 * Appending Kills:: Several kills in a row all yank together.
278 * Earlier Kills:: Yanking something killed some time ago.
279 @end menu
280
281 @node Kill Ring
282 @subsection The Kill Ring
283
284 The @dfn{kill ring} is a list of blocks of text that were previously
285 killed. There is only one kill ring, shared by all buffers, so you
286 can kill text in one buffer and yank it in another buffer. This is
287 the usual way to move text from one file to another. (There are
288 several other methods: for instance, you could store the text in a
289 register; see @ref{Registers}. @xref{Accumulating Text}, for some
290 other ways to move text around.)
291
292 @kindex C-y
293 @findex yank
294 @kbd{C-y} (@code{yank}) reinserts the text of the most recent kill,
295 leaving the cursor at the end of the text. It also adds the position
296 of the beginning of the text to the mark ring, without activating the
297 mark; this allows you to jump easily to that position with @kbd{C-x
298 C-x} (@pxref{Setting Mark}).
299
300 On graphical displays, @kbd{C-y} first checks if another application
301 has placed any text in the system clipboard more recently than the
302 last Emacs kill. If so, it inserts from the clipboard instead of the
303 kill ring. Conceptually, you can think of the clipboard as an
304 ``extra'' entry in the kill ring, which is present if you recently cut
305 or copied some text in another application. @xref{Cut and Paste}.
306
307 With a plain prefix argument (@kbd{C-u C-y}), the @code{yank}
308 command instead leaves the cursor in front of the text, and adds the
309 position of the end of the text to the mark ring. Using any other
310 prefix argument specifies an earlier kill; for example, @kbd{C-u 4
311 C-y} reinserts the fourth most recent kill. @xref{Earlier Kills}.
312
313 @node Appending Kills
314 @subsection Appending Kills
315
316 @cindex appending kills in the ring
317 Normally, each kill command pushes a new entry onto the kill ring.
318 However, two or more kill commands in a row combine their text into a
319 single entry, so that a single @kbd{C-y} yanks all the text as a unit,
320 just as it was before it was killed.
321
322 Thus, if you want to yank text as a unit, you need not kill all of it
323 with one command; you can keep killing line after line, or word after
324 word, until you have killed it all, and you can still get it all back at
325 once.
326
327 Commands that kill forward from point add onto the end of the previous
328 killed text. Commands that kill backward from point add text onto the
329 beginning. This way, any sequence of mixed forward and backward kill
330 commands puts all the killed text into one entry without rearrangement.
331 Numeric arguments do not break the sequence of appending kills. For
332 example, suppose the buffer contains this text:
333
334 @example
335 This is a line @point{}of sample text.
336 @end example
337
338 @noindent
339 with point shown by @point{}. If you type @kbd{M-d M-@key{DEL} M-d
340 M-@key{DEL}}, killing alternately forward and backward, you end up with
341 @samp{a line of sample} as one entry in the kill ring, and @samp{This
342 is@ @ text.} in the buffer. (Note the double space between @samp{is}
343 and @samp{text}, which you can clean up with @kbd{M-@key{SPC}} or
344 @kbd{M-q}.)
345
346 Another way to kill the same text is to move back two words with
347 @kbd{M-b M-b}, then kill all four words forward with @kbd{C-u M-d}.
348 This produces exactly the same results in the buffer and in the kill
349 ring. @kbd{M-f M-f C-u M-@key{DEL}} kills the same text, all going
350 backward; once again, the result is the same. The text in the kill ring
351 entry always has the same order that it had in the buffer before you
352 killed it.
353
354 @kindex C-M-w
355 @findex append-next-kill
356 If a kill command is separated from the last kill command by other
357 commands (not just numeric arguments), it starts a new entry on the kill
358 ring. But you can force it to append by first typing the command
359 @kbd{C-M-w} (@code{append-next-kill}) right before it. The @kbd{C-M-w}
360 tells the following command, if it is a kill command, to append the text
361 it kills to the last killed text, instead of starting a new entry. With
362 @kbd{C-M-w}, you can kill several separated pieces of text and
363 accumulate them to be yanked back in one place.@refill
364
365 A kill command following @kbd{M-w} (@code{kill-ring-save}) does not
366 append to the text that @kbd{M-w} copied into the kill ring.
367
368 @node Earlier Kills
369 @subsection Yanking Earlier Kills
370
371 @cindex yanking previous kills
372 @kindex M-y
373 @findex yank-pop
374 To recover killed text that is no longer the most recent kill, use the
375 @kbd{M-y} command (@code{yank-pop}). It takes the text previously
376 yanked and replaces it with the text from an earlier kill. So, to
377 recover the text of the next-to-the-last kill, first use @kbd{C-y} to
378 yank the last kill, and then use @kbd{M-y} to replace it with the
379 previous kill. @kbd{M-y} is allowed only after a @kbd{C-y} or another
380 @kbd{M-y}.
381
382 You can understand @kbd{M-y} in terms of a ``last yank'' pointer which
383 points at an entry in the kill ring. Each time you kill, the ``last
384 yank'' pointer moves to the newly made entry at the front of the ring.
385 @kbd{C-y} yanks the entry which the ``last yank'' pointer points to.
386 @kbd{M-y} moves the ``last yank'' pointer to a different entry, and the
387 text in the buffer changes to match. Enough @kbd{M-y} commands can move
388 the pointer to any entry in the ring, so you can get any entry into the
389 buffer. Eventually the pointer reaches the end of the ring; the next
390 @kbd{M-y} loops back around to the first entry again.
391
392 @kbd{M-y} moves the ``last yank'' pointer around the ring, but it does
393 not change the order of the entries in the ring, which always runs from
394 the most recent kill at the front to the oldest one still remembered.
395
396 @kbd{M-y} can take a numeric argument, which tells it how many entries
397 to advance the ``last yank'' pointer by. A negative argument moves the
398 pointer toward the front of the ring; from the front of the ring, it
399 moves ``around'' to the last entry and continues forward from there.
400
401 Once the text you are looking for is brought into the buffer, you can
402 stop doing @kbd{M-y} commands and it will stay there. It's just a copy
403 of the kill ring entry, so editing it in the buffer does not change
404 what's in the ring. As long as no new killing is done, the ``last
405 yank'' pointer remains at the same place in the kill ring, so repeating
406 @kbd{C-y} will yank another copy of the same previous kill.
407
408 If you know how many @kbd{M-y} commands it would take to find the
409 text you want, you can yank that text in one step using @kbd{C-y} with
410 a numeric argument. @kbd{C-y} with an argument restores the text from
411 the specified kill ring entry, counting back from the most recent as
412 1. Thus, @kbd{C-u 2 C-y} gets the next-to-the-last block of killed
413 text---it is equivalent to @kbd{C-y M-y}. @kbd{C-y} with a numeric
414 argument starts counting from the ``last yank'' pointer, and sets the
415 ``last yank'' pointer to the entry that it yanks.
416
417 @vindex kill-ring-max
418 The length of the kill ring is controlled by the variable
419 @code{kill-ring-max}; no more than that many blocks of killed text are
420 saved.
421
422 @vindex kill-ring
423 The actual contents of the kill ring are stored in a variable named
424 @code{kill-ring}; you can view the entire contents of the kill ring with
425 the command @kbd{C-h v kill-ring}.
426
427 @node Cut and Paste
428 @section ``Cut and Paste'' Operations on Graphical Displays
429 @cindex cut
430 @cindex copy
431 @cindex paste
432
433 In most graphical desktop environments, you can transfer data
434 (usually text) between different applications using a system facility
435 called the @dfn{clipboard}. On X, two other similar facilities are
436 available: the primary selection and the secondary selection. When
437 Emacs is run on a graphical display, its kill and yank commands
438 integrate with these facilities, so that you can easily transfer text
439 between Emacs and other graphical applications.
440
441 By default, Emacs uses UTF-8 as the coding system for inter-program
442 text transfers. If you find that the pasted text is not what you
443 expected, you can specify another coding system by typing @kbd{C-x
444 @key{RET} x} or @kbd{C-x @key{RET} X}. You can also request a
445 different data type by customizing @code{x-select-request-type}.
446 @xref{Communication Coding}.
447
448 @menu
449 * Clipboard:: How Emacs uses the system clipboard.
450 * Primary Selection:: The temporarily selected text selection.
451 * Secondary Selection:: Cutting without altering point and mark.
452 @end menu
453
454 @node Clipboard
455 @subsection Using the Clipboard
456 @cindex clipboard
457
458 The @dfn{clipboard} is the facility that most graphical applications
459 use for ``cutting and pasting''. When the clipboard exists, the kill
460 and yank commands in Emacs make use of it.
461
462 When you kill some text with a command such as @kbd{C-w}
463 (@code{kill-region}), or copy it to the kill ring with a command such
464 as @kbd{M-w} (@code{kill-ring-save}), that text is also put in the
465 clipboard. @xref{Killing}.
466
467 @vindex save-interprogram-paste-before-kill
468 When an Emacs kill command puts text in the clipboard, the existing
469 clipboard contents are normally lost. Optionally, you can change
470 @code{save-interprogram-paste-before-kill} to @code{t}. Then Emacs
471 will first save the clipboard to its kill ring, preventing you from
472 losing the old clipboard data---at the risk of high memory consumption
473 if that data turns out to be large.
474
475 The usual yank commands, such as @kbd{C-y} (@code{yank}), also use
476 the clipboard. If another application ``owns'' the clipboard---i.e.,
477 if you cut or copied text there more recently than your last kill
478 command in Emacs---then Emacs yanks from the clipboard instead of the
479 kill ring. Otherwise, it yanks from the kill ring, as described in
480 @ref{Yanking}.
481
482 @vindex yank-pop-change-selection
483 Normally, rotating the kill ring with @kbd{M-y} (@code{yank-pop})
484 does not alter the clipboard. However, if you change
485 @code{yank-pop-change-selection} to @code{t}, then @kbd{M-y} saves the
486 new yank to the clipboard.
487
488 @vindex x-select-enable-clipboard
489 To prevent kill and yank commands from accessing the clipboard,
490 change the variable @code{x-select-enable-clipboard} to @code{nil}.
491
492 @vindex x-select-enable-primary
493 @findex clipboard-kill-region
494 @findex clipboard-kill-ring-save
495 @findex clipboard-yank
496 Prior to Emacs 24, the kill and yank commands used the primary
497 selection (@pxref{Primary Selection}), not the clipboard. If you
498 prefer this behavior, change @code{x-select-enable-clipboard} to
499 @code{nil}, @code{x-select-enable-primary} to @code{t}, and
500 @code{mouse-drag-copy-region} to @code{t}. In this case, you can use
501 the following commands to act explicitly on the clipboard:
502 @code{clipboard-kill-region} kills the region and saves it to the
503 clipboard; @code{clipboard-kill-ring-save} copies the region to the
504 kill ring and saves it to the clipboard; and @code{clipboard-yank}
505 yanks the contents of the clipboard at point.
506
507 @node Primary Selection
508 @subsection Cut and Paste with Other Window Applications
509 @cindex X cutting and pasting
510 @cindex X selection
511 @cindex primary selection
512 @cindex selection, primary
513
514 Under the X window system, there exists a @dfn{primary selection}
515 containing the last stretch of text selected in an X application
516 (usually by dragging the mouse). Typically, this text can be inserted
517 into other X applications by @kbd{mouse-2} clicks. The primary
518 selection is separate from the clipboard (@pxref{Clipboard}). Its
519 contents are more ``fragile''; they are overwritten by any mouse
520 selection, whereas the clipboard is only overwritten by explicit
521 ``cut'' or ``copy'' commands.
522
523 Under X, whenever you set an active region (@pxref{Mark}), Emacs
524 saves the text in the active region to the primary selection. This
525 applies to active regions made by dragging or clicking the mouse
526 (@pxref{Mouse Commands}), and those made by keyboard commands (e.g. by
527 typing @kbd{C-@key{SPC}} and moving point; see @ref{Setting Mark}).
528
529 @vindex select-active-regions
530 If you change the variable @code{select-active-regions} to
531 @code{only}, Emacs saves only temporarily active regions to the
532 primary selection, i.e. those made with the mouse or with shift
533 selection (@pxref{Shift Selection}). If you change
534 @code{select-active-regions} to @code{nil}, Emacs avoids saving active
535 regions to the primary selection entirely.
536
537 To insert the primary selection into an Emacs buffer, click
538 @kbd{mouse-2} (@code{mouse-yank-primary}) where you want to insert it.
539 @xref{Mouse Commands}.
540
541 @cindex MS-Windows, and primary selection
542 MS-Windows provides no primary selection, but Emacs emulates it
543 within a single Emacs session by storing the selected text internally.
544 Therefore, all the features and commands related to the primary
545 selection work on Windows as they do on X, for cutting and pasting
546 within the same session, but not across Emacs sessions or with other
547 applications.
548
549 @node Secondary Selection
550 @subsection Secondary Selection
551 @cindex secondary selection
552
553 In addition to the primary selection, the X Window System provides a
554 second similar facility known as the @dfn{secondary selection}.
555 Nowadays, few X applications make use of the secondary selection, but
556 you can access it using the following Emacs commands:
557
558 @table @kbd
559 @findex mouse-set-secondary
560 @kindex M-Drag-Mouse-1
561 @item M-Drag-Mouse-1
562 Set the secondary selection, with one end at the place where you press
563 down the button, and the other end at the place where you release it
564 (@code{mouse-set-secondary}). The selected text is highlighted, using
565 the @code{secondary-selection} face, as you drag. The window scrolls
566 automatically if you drag the mouse off the top or bottom of the
567 window, just like @code{mouse-set-region} (@pxref{Mouse Commands}).
568
569 This command does not alter the kill ring.
570
571 @findex mouse-start-secondary
572 @kindex M-Mouse-1
573 @item M-Mouse-1
574 Set one endpoint for the @dfn{secondary selection}
575 (@code{mouse-start-secondary}).
576
577 @findex mouse-secondary-save-then-kill
578 @kindex M-Mouse-3
579 @item M-Mouse-3
580 Set the secondary selection, with one end at the position clicked and
581 the other at the position specified with @kbd{M-Mouse-1}
582 (@code{mouse-secondary-save-then-kill}). This also puts the selected
583 text in the kill ring. A second @kbd{M-Mouse-3} at the same place
584 kills the secondary selection just made.
585
586 @findex mouse-yank-secondary
587 @kindex M-Mouse-2
588 @item M-Mouse-2
589 Insert the secondary selection where you click, placing point at the
590 end of the yanked text (@code{mouse-yank-secondary}).
591 @end table
592
593 Double or triple clicking of @kbd{M-Mouse-1} operates on words and
594 lines, much like @kbd{Mouse-1}.
595
596 If @code{mouse-yank-at-point} is non-@code{nil}, @kbd{M-Mouse-2} yanks
597 at point. Then it does not matter precisely where you click, or even
598 which of the frame's windows you click on. @xref{Mouse Commands}.
599
600 @node Accumulating Text
601 @section Accumulating Text
602 @findex append-to-buffer
603 @findex prepend-to-buffer
604 @findex copy-to-buffer
605 @findex append-to-file
606
607 @cindex accumulating scattered text
608 Usually we copy or move text by killing it and yanking it, but there
609 are other convenient methods for copying one block of text in many
610 places, or for copying many scattered blocks of text into one place.
611 Here we describe the commands to accumulate scattered pieces of text
612 into a buffer or into a file.
613
614 @table @kbd
615 @item M-x append-to-buffer
616 Append region to the contents of a specified buffer.
617 @item M-x prepend-to-buffer
618 Prepend region to the contents of a specified buffer.
619 @item M-x copy-to-buffer
620 Copy region into a specified buffer, deleting that buffer's old contents.
621 @item M-x insert-buffer
622 Insert the contents of a specified buffer into current buffer at point.
623 @item M-x append-to-file
624 Append region to the contents of a specified file, at the end.
625 @end table
626
627 To accumulate text into a buffer, use @kbd{M-x append-to-buffer}.
628 This reads a buffer name, then inserts a copy of the region into the
629 buffer specified. If you specify a nonexistent buffer,
630 @code{append-to-buffer} creates the buffer. The text is inserted
631 wherever point is in that buffer. If you have been using the buffer for
632 editing, the copied text goes into the middle of the text of the buffer,
633 starting from wherever point happens to be at that moment.
634
635 Point in that buffer is left at the end of the copied text, so
636 successive uses of @code{append-to-buffer} accumulate the text in the
637 specified buffer in the same order as they were copied. Strictly
638 speaking, @code{append-to-buffer} does not always append to the text
639 already in the buffer---it appends only if point in that buffer is at the end.
640 However, if @code{append-to-buffer} is the only command you use to alter
641 a buffer, then point is always at the end.
642
643 @kbd{M-x prepend-to-buffer} is just like @code{append-to-buffer}
644 except that point in the other buffer is left before the copied text, so
645 successive prependings add text in reverse order. @kbd{M-x
646 copy-to-buffer} is similar, except that any existing text in the other
647 buffer is deleted, so the buffer is left containing just the text newly
648 copied into it.
649
650 The command @kbd{M-x insert-buffer} can be used to retrieve the
651 accumulated text from another buffer. This prompts for the name of a
652 buffer, and inserts a copy of all the text in that buffer into the
653 current buffer at point, leaving point at the beginning of the
654 inserted text. It also adds the position of the end of the inserted
655 text to the mark ring, without activating the mark. @xref{Buffers},
656 for background information on buffers.
657
658 Instead of accumulating text in a buffer, you can append text
659 directly into a file with @kbd{M-x append-to-file}. This prompts for
660 a filename, and adds the text of the region to the end of the
661 specified file. The file is changed immediately on disk.
662
663 You should use @code{append-to-file} only with files that are
664 @emph{not} being visited in Emacs. Using it on a file that you are
665 editing in Emacs would change the file behind Emacs's back, which
666 can lead to losing some of your editing.
667
668 Another way to move text around is to store it in a register.
669 @xref{Registers}.
670
671 @node Rectangles
672 @section Rectangles
673 @cindex rectangle
674 @cindex columns (and rectangles)
675 @cindex killing rectangular areas of text
676
677 @dfn{Rectangle} commands operate on rectangular areas of the text:
678 all the characters between a certain pair of columns, in a certain
679 range of lines. Emacs has commands to kill rectangles, yank killed
680 rectangles, clear them out, fill them with blanks or text, or delete
681 them. Rectangle commands are useful with text in multicolumn formats,
682 and for changing text into or out of such formats.
683
684 @cindex mark rectangle
685 When you must specify a rectangle for a command to work on, you do it
686 by putting the mark at one corner and point at the opposite corner. The
687 rectangle thus specified is called the @dfn{region-rectangle} because
688 you control it in much the same way as the region is controlled. But
689 remember that a given combination of point and mark values can be
690 interpreted either as a region or as a rectangle, depending on the
691 command that uses them.
692
693 If point and the mark are in the same column, the rectangle they
694 delimit is empty. If they are in the same line, the rectangle is one
695 line high. This asymmetry between lines and columns comes about
696 because point (and likewise the mark) is between two columns, but within
697 a line.
698
699 @table @kbd
700 @item C-x r k
701 Kill the text of the region-rectangle, saving its contents as the
702 ``last killed rectangle'' (@code{kill-rectangle}).
703 @item C-x r d
704 Delete the text of the region-rectangle (@code{delete-rectangle}).
705 @item C-x r y
706 Yank the last killed rectangle with its upper left corner at point
707 (@code{yank-rectangle}).
708 @item C-x r o
709 Insert blank space to fill the space of the region-rectangle
710 (@code{open-rectangle}). This pushes the previous contents of the
711 region-rectangle rightward.
712 @item C-x r c
713 Clear the region-rectangle by replacing all of its contents with spaces
714 (@code{clear-rectangle}).
715 @item M-x delete-whitespace-rectangle
716 Delete whitespace in each of the lines on the specified rectangle,
717 starting from the left edge column of the rectangle.
718 @item C-x r t @var{string} @key{RET}
719 Replace rectangle contents with @var{string} on each line
720 (@code{string-rectangle}).
721 @item M-x string-insert-rectangle @key{RET} @var{string} @key{RET}
722 Insert @var{string} on each line of the rectangle.
723 @end table
724
725 The rectangle operations fall into two classes: commands for
726 deleting and inserting rectangles, and commands for blank rectangles.
727
728 @kindex C-x r k
729 @kindex C-x r d
730 @findex kill-rectangle
731 @findex delete-rectangle
732 There are two ways to get rid of the text in a rectangle: you can
733 discard the text (delete it) or save it as the ``last killed''
734 rectangle. The commands for these two ways are @kbd{C-x r d}
735 (@code{delete-rectangle}) and @kbd{C-x r k} (@code{kill-rectangle}). In
736 either case, the portion of each line that falls inside the rectangle's
737 boundaries is deleted, causing any following text on the line to
738 move left into the gap.
739
740 Note that ``killing'' a rectangle is not killing in the usual sense; the
741 rectangle is not stored in the kill ring, but in a special place that
742 can only record the most recent rectangle killed. This is because yanking
743 a rectangle is so different from yanking linear text that different yank
744 commands have to be used. It is hard to define yank-popping for rectangles,
745 so we do not try.
746
747 @kindex C-x r y
748 @findex yank-rectangle
749 To yank the last killed rectangle, type @kbd{C-x r y}
750 (@code{yank-rectangle}). Yanking a rectangle is the opposite of killing
751 one. Point specifies where to put the rectangle's upper left corner.
752 The rectangle's first line is inserted there, the rectangle's second
753 line is inserted at the same horizontal position, but one line
754 vertically down, and so on. The number of lines affected is determined
755 by the height of the saved rectangle.
756
757 You can convert single-column lists into double-column lists using
758 rectangle killing and yanking; kill the second half of the list as a
759 rectangle and then yank it beside the first line of the list.
760 @xref{Two-Column}, for another way to edit multi-column text.
761
762 You can also copy rectangles into and out of registers with @kbd{C-x r
763 r @var{r}} and @kbd{C-x r i @var{r}}. @xref{Rectangle Registers}.
764
765 @kindex C-x r o
766 @findex open-rectangle
767 @kindex C-x r c
768 @findex clear-rectangle
769 There are two commands you can use for making blank rectangles:
770 @kbd{C-x r c} (@code{clear-rectangle}) which blanks out existing text,
771 and @kbd{C-x r o} (@code{open-rectangle}) which inserts a blank
772 rectangle. Clearing a rectangle is equivalent to deleting it and then
773 inserting a blank rectangle of the same size.
774
775 @findex delete-whitespace-rectangle
776 The command @kbd{M-x delete-whitespace-rectangle} deletes horizontal
777 whitespace starting from a particular column. This applies to each of
778 the lines in the rectangle, and the column is specified by the left
779 edge of the rectangle. The right edge of the rectangle does not make
780 any difference to this command.
781
782 @kindex C-x r t
783 @findex string-rectangle
784 The command @kbd{C-x r t} (@code{string-rectangle}) replaces the
785 contents of a region-rectangle with a string on each line. The
786 string's width need not be the same as the width of the rectangle. If
787 the string's width is less, the text after the rectangle shifts left;
788 if the string is wider than the rectangle, the text after the
789 rectangle shifts right.
790
791 @findex string-insert-rectangle
792 The command @kbd{M-x string-insert-rectangle} is similar to
793 @code{string-rectangle}, but inserts the string on each line,
794 shifting the original text to the right.
795
796 @node CUA Bindings
797 @section CUA Bindings
798 @findex cua-mode
799 @vindex cua-mode
800 @cindex CUA key bindings
801 @vindex cua-enable-cua-keys
802 The command @kbd{M-x cua-mode} sets up key bindings that are
803 compatible with the Common User Access (CUA) system used in many other
804 applications. @kbd{C-x} means cut (kill), @kbd{C-c} copy, @kbd{C-v}
805 paste (yank), and @kbd{C-z} undo. Standard Emacs commands like
806 @kbd{C-x C-c} still work, because @kbd{C-x} and @kbd{C-c} only take
807 effect when the mark is active (and the region is highlighted).
808 However, if you don't want to override these bindings in Emacs at all,
809 set @code{cua-enable-cua-keys} to @code{nil}.
810
811 To enter an Emacs command like @kbd{C-x C-f} while the mark is
812 active, use one of the following methods: either hold @kbd{Shift}
813 together with the prefix key, e.g. @kbd{S-C-x C-f}, or quickly type
814 the prefix key twice, e.g. @kbd{C-x C-x C-f}.
815
816 In CUA mode, typed text replaces the active region as in
817 Delete-Selection mode (@pxref{Mouse Commands}).
818
819 @cindex rectangle highlighting
820 CUA mode provides enhanced rectangle support with visible
821 rectangle highlighting. Use @kbd{C-RET} to start a rectangle,
822 extend it using the movement commands, and cut or copy it using
823 @kbd{C-x} or @kbd{C-c}. @kbd{RET} moves the cursor to the next
824 (clockwise) corner of the rectangle, so you can easily expand it in
825 any direction. Normal text you type is inserted to the left or right
826 of each line in the rectangle (on the same side as the cursor).
827
828 With CUA you can easily copy text and rectangles into and out of
829 registers by providing a one-digit numeric prefix to the kill, copy,
830 and yank commands, e.g. @kbd{C-1 C-c} copies the region into register
831 @code{1}, and @kbd{C-2 C-v} yanks the contents of register @code{2}.
832
833 @cindex global mark
834 CUA mode also has a global mark feature which allows easy moving and
835 copying of text between buffers. Use @kbd{C-S-SPC} to toggle the
836 global mark on and off. When the global mark is on, all text that you
837 kill or copy is automatically inserted at the global mark, and text
838 you type is inserted at the global mark rather than at the current
839 position.
840
841 For example, to copy words from various buffers into a word list in
842 a given buffer, set the global mark in the target buffer, then
843 navigate to each of the words you want in the list, mark it (e.g. with
844 @kbd{S-M-f}), copy it to the list with @kbd{C-c} or @kbd{M-w}, and
845 insert a newline after the word in the target list by pressing
846 @key{RET}.