Commit | Line | Data |
---|---|---|
6bf7aab6 | 1 | @c This is part of the Emacs manual. |
73b0cd50 | 2 | @c Copyright (C) 1985-1987, 1993-1995, 1997, 2000-2011 |
8838673e | 3 | @c Free Software Foundation, Inc. |
6bf7aab6 | 4 | @c See file emacs.texi for copying conditions. |
6bf7aab6 | 5 | |
b103c904 | 6 | @node Killing, Registers, Mark, Top |
1f67b1dd | 7 | @chapter Killing and Moving Text |
6bf7aab6 | 8 | |
1f67b1dd RS |
9 | @dfn{Killing} means erasing text and copying it into the @dfn{kill |
10 | ring}, from which you can bring it back into the buffer by | |
7e9bf756 CY |
11 | @dfn{yanking} it. (Some applications use the terms ``cutting'' and |
12 | ``pasting'' for similar operations.) This is the most common way of | |
13 | moving or copying text within Emacs. It is very versatile, because | |
14 | there are commands for killing many different types of syntactic | |
15 | units. | |
1f67b1dd | 16 | |
b103c904 CY |
17 | @menu |
18 | * Deletion and Killing:: Commands that remove text. | |
19 | * Yanking:: Commands that insert text. | |
20 | * Accumulating Text:: Other methods to add text to the buffer. | |
21 | * Rectangles:: Operating on text in rectangular areas. | |
22 | * CUA Bindings:: Using C-x/C-c/C-v to kill and yank. | |
23 | @end menu | |
24 | ||
25 | @node Deletion and Killing | |
6bf7aab6 DL |
26 | @section Deletion and Killing |
27 | ||
28 | @cindex killing text | |
29 | @cindex cutting text | |
30 | @cindex deletion | |
1f67b1dd | 31 | Most commands which erase text from the buffer save it in the kill |
7e9bf756 CY |
32 | ring. These are known as @dfn{kill} commands. The kill ring stores |
33 | several recent kills, not just the last one, so killing is a very safe | |
34 | operation: when you make a new kill, you don't have to worry much | |
35 | about losing text that you previously killed. | |
36 | ||
37 | You can yank text from the kill ring into any position in a buffer, | |
38 | including a position in a different buffer; the kill ring is shared by | |
39 | all buffers. The @kbd{C-/} (@code{undo}) command can undo both kill | |
40 | and delete commands (@pxref{Undo}); the importance of the kill ring is | |
41 | that you can yank the text in a different place. | |
42 | ||
43 | Commands that erase text but do not save it in the kill ring are | |
44 | known as @dfn{delete} commands. These include @kbd{C-d} | |
45 | (@code{delete-char}) and @key{DEL} (@code{delete-backward-char}), | |
46 | which delete only one character at a time, and those commands that | |
47 | delete only spaces or newlines. Commands that can erase significant | |
48 | amounts of nontrivial data generally do a kill operation instead. The | |
49 | commands' names and individual descriptions use the words @samp{kill} | |
50 | and @samp{delete} to say which kind of operation they perform. | |
6bf7aab6 | 51 | |
a21cc7d1 RS |
52 | You can also use the mouse to kill and yank. @xref{Cut and Paste}. |
53 | ||
6bf7aab6 DL |
54 | @menu |
55 | * Deletion:: Commands for deleting small amounts of text and | |
56 | blank areas. | |
57 | * Killing by Lines:: How to kill entire lines of text at one time. | |
58 | * Other Kill Commands:: Commands to kill large regions of text and | |
177c0ea7 | 59 | syntactic units such as words and sentences. |
91ed7ea8 | 60 | * Kill Options:: Options that affect killing. |
6bf7aab6 DL |
61 | @end menu |
62 | ||
63 | @node Deletion | |
64 | @subsection Deletion | |
6bf7aab6 DL |
65 | @findex delete-backward-char |
66 | @findex delete-char | |
6bf7aab6 | 67 | |
ba5d9bfd RS |
68 | Deletion means erasing text and not saving it in the kill ring. For |
69 | the most part, the Emacs commands that delete text are those that | |
70 | erase just one character or only whitespace. | |
71 | ||
6bf7aab6 DL |
72 | @table @kbd |
73 | @item C-d | |
7e9bf756 CY |
74 | @itemx @key{Delete} |
75 | Delete next character (@code{delete-char}). | |
6bf7aab6 | 76 | @item @key{DEL} |
7e9bf756 | 77 | @itemx @key{Backspace} |
144e981a | 78 | Delete previous character (@code{delete-backward-char}). |
6bf7aab6 DL |
79 | @item M-\ |
80 | Delete spaces and tabs around point (@code{delete-horizontal-space}). | |
81 | @item M-@key{SPC} | |
82 | Delete spaces and tabs around point, leaving one space | |
83 | (@code{just-one-space}). | |
84 | @item C-x C-o | |
85 | Delete blank lines around the current line (@code{delete-blank-lines}). | |
86 | @item M-^ | |
87 | Join two lines by deleting the intervening newline, along with any | |
88 | indentation following it (@code{delete-indentation}). | |
89 | @end table | |
90 | ||
7e9bf756 CY |
91 | We have already described the basic deletion commands @kbd{C-d} |
92 | (@code{delete-char}) and @key{DEL} (@code{delete-backward-char}). | |
93 | @xref{Erasing}. | |
4933bc02 | 94 | |
6bf7aab6 DL |
95 | @kindex M-\ |
96 | @findex delete-horizontal-space | |
97 | @kindex M-SPC | |
98 | @findex just-one-space | |
7e9bf756 | 99 | The other delete commands are those that delete only whitespace |
6bf7aab6 DL |
100 | characters: spaces, tabs and newlines. @kbd{M-\} |
101 | (@code{delete-horizontal-space}) deletes all the spaces and tab | |
f111a3c6 CY |
102 | characters before and after point. With a prefix argument, this only |
103 | deletes spaces and tab characters before point. @kbd{M-@key{SPC}} | |
6bf7aab6 | 104 | (@code{just-one-space}) does likewise but leaves a single space after |
80b9df3a RS |
105 | point, regardless of the number of spaces that existed previously |
106 | (even if there were none before). With a numeric argument @var{n}, it | |
870f8c97 | 107 | leaves @var{n} spaces after point. |
6bf7aab6 DL |
108 | |
109 | @kbd{C-x C-o} (@code{delete-blank-lines}) deletes all blank lines | |
110 | after the current line. If the current line is blank, it deletes all | |
111 | blank lines preceding the current line as well (leaving one blank line, | |
fda95b3d | 112 | the current line). On a solitary blank line, it deletes that line. |
6bf7aab6 DL |
113 | |
114 | @kbd{M-^} (@code{delete-indentation}) joins the current line and the | |
115 | previous line, by deleting a newline and all surrounding spaces, usually | |
116 | leaving a single space. @xref{Indentation,M-^}. | |
117 | ||
118 | @node Killing by Lines | |
119 | @subsection Killing by Lines | |
120 | ||
121 | @table @kbd | |
122 | @item C-k | |
123 | Kill rest of line or one or more lines (@code{kill-line}). | |
533286e0 EZ |
124 | @item C-S-backspace |
125 | Kill an entire line at once (@code{kill-whole-line}) | |
6bf7aab6 DL |
126 | @end table |
127 | ||
128 | @kindex C-k | |
129 | @findex kill-line | |
7e9bf756 CY |
130 | The simplest kill command is @kbd{C-k}. If given at the beginning |
131 | of a line, it kills all the text on the line@footnote{Here, ``line'' | |
132 | means a logical text line, not a screen line. @xref{Continuation | |
133 | Lines}.}, leaving it blank. When used on a blank line, it kills the | |
134 | whole line including its newline. | |
135 | ||
136 | More precisely, @kbd{C-k} kills from point up to the end of the | |
137 | line, unless it is at the end of a line. In that case it kills the | |
138 | newline following point, thus merging the next line into the current | |
139 | one. Spaces and tabs at the end of the line are ignored when deciding | |
140 | which case applies, so as long as point is after the last visible | |
141 | character in the line, you can be sure that @kbd{C-k} will kill the | |
142 | newline. To kill an entire non-blank line, go to the beginning and | |
143 | type @kbd{C-k} twice. | |
144 | ||
145 | When @kbd{C-k} is given a positive argument @var{n}, it kills | |
146 | @var{n} lines and the newlines that follow them (text on the current | |
147 | line before point is not killed). With a negative argument | |
148 | @minus{}@var{n}, it kills @var{n} lines preceding the current line, | |
149 | together with the text on the current line before point. @kbd{C-k} | |
150 | with an argument of zero kills the text before point on the current | |
151 | line. | |
6bf7aab6 DL |
152 | |
153 | @vindex kill-whole-line | |
154 | If the variable @code{kill-whole-line} is non-@code{nil}, @kbd{C-k} at | |
155 | the very beginning of a line kills the entire line including the | |
156 | following newline. This variable is normally @code{nil}. | |
157 | ||
533286e0 EZ |
158 | @kindex C-S-backspace |
159 | @findex kill-whole-line | |
160 | @kbd{C-S-backspace} (@code{kill-whole-line}) will kill a whole line | |
161 | including its newline regardless of the position of point within the | |
162 | line. Note that many character terminals will prevent you from typing | |
163 | the key sequence @kbd{C-S-backspace}. | |
164 | ||
6bf7aab6 DL |
165 | @node Other Kill Commands |
166 | @subsection Other Kill Commands | |
167 | @findex kill-region | |
168 | @kindex C-w | |
169 | ||
6bf7aab6 DL |
170 | @table @kbd |
171 | @item C-w | |
4c987d71 | 172 | Kill region (@code{kill-region}). @xref{Mark}. |
7e9bf756 CY |
173 | @item M-w |
174 | Save region as last killed text without actually killing it | |
175 | (@code{kill-ring-save}). Some programs call this ``copying.'' | |
6bf7aab6 DL |
176 | @item M-d |
177 | Kill word (@code{kill-word}). @xref{Words}. | |
178 | @item M-@key{DEL} | |
179 | Kill word backwards (@code{backward-kill-word}). | |
180 | @item C-x @key{DEL} | |
181 | Kill back to beginning of sentence (@code{backward-kill-sentence}). | |
182 | @xref{Sentences}. | |
183 | @item M-k | |
184 | Kill to end of sentence (@code{kill-sentence}). | |
185 | @item C-M-k | |
46497336 | 186 | Kill the following balanced expression (@code{kill-sexp}). @xref{Expressions}. |
6bf7aab6 DL |
187 | @item M-z @var{char} |
188 | Kill through the next occurrence of @var{char} (@code{zap-to-char}). | |
189 | @end table | |
190 | ||
7e9bf756 CY |
191 | Apart from @kbd{C-k}, the most commonly-used kill command is |
192 | @kbd{C-w} (@code{kill-region}), which kills the text in the region | |
193 | (i.e., between point and mark). @xref{Mark}. If the mark is inactive | |
194 | when you type @kbd{C-w}, it first reactivates the mark where it was | |
195 | last set. The mark is deactivated at the end of the command. | |
196 | ||
197 | @kindex M-w | |
198 | @findex kill-ring-save | |
199 | The command @kbd{M-w} (@code{kill-ring-save}) copies the region into | |
200 | the kill ring without removing it from the buffer. This is | |
201 | approximately equivalent to @kbd{C-w} followed by @kbd{C-/}, except | |
202 | that @kbd{M-w} does not alter the undo history. | |
203 | ||
204 | Emacs also provides commands to kill specific syntactic units: | |
205 | words, with @kbd{M-@key{DEL}} and @kbd{M-d} (@pxref{Words}); balanced | |
206 | expressions, with @kbd{C-M-k} (@pxref{Expressions}); and sentences, | |
207 | with @kbd{C-x @key{DEL}} and @kbd{M-k} (@pxref{Sentences}). | |
6bf7aab6 DL |
208 | |
209 | @kindex M-z | |
210 | @findex zap-to-char | |
7e9bf756 CY |
211 | The command @kbd{M-z} (@code{zap-to-char}) combines killing with |
212 | searching: it reads a character and kills from point up to (and | |
6bf7aab6 | 213 | including) the next occurrence of that character in the buffer. A |
7e9bf756 | 214 | numeric argument acts as a repeat count; a negative argument means to |
6bf7aab6 DL |
215 | search backward and kill text before point. |
216 | ||
91ed7ea8 CY |
217 | @node Kill Options |
218 | @subsection Options for Killing | |
219 | ||
220 | @vindex kill-read-only-ok | |
221 | @cindex read-only text, killing | |
222 | Some specialized buffers contain @dfn{read-only text}, which cannot | |
223 | be modified and therefore cannot be killed. But some users like to | |
224 | use the kill commands to copy read-only text into the kill ring, | |
225 | without actually changing it. Therefore, the kill commands work | |
226 | specially in a read-only buffer: they move over text, and copy it to | |
227 | the kill ring, without actually deleting it from the buffer. | |
228 | Normally, kill commands beep and display an error message when this | |
229 | happens. But if you set the variable @code{kill-read-only-ok} to a | |
230 | non-@code{nil} value, they just print a message in the echo area to | |
231 | explain why the text has not been erased. | |
232 | ||
233 | @vindex kill-do-not-save-duplicates | |
234 | If you change the variable @code{kill-do-not-save-duplicates} to a | |
235 | non-@code{nil} value, identical subsequent kills yield a single | |
236 | kill-ring entry, without duplication. | |
237 | ||
b103c904 | 238 | @node Yanking |
6bf7aab6 DL |
239 | @section Yanking |
240 | @cindex moving text | |
241 | @cindex copying text | |
242 | @cindex kill ring | |
243 | @cindex yanking | |
244 | @cindex pasting | |
245 | ||
7e9bf756 CY |
246 | @dfn{Yanking} means reinserting text previously killed. The usual |
247 | way to move or copy text is to kill it and then yank it elsewhere one | |
248 | or more times. | |
6bf7aab6 DL |
249 | |
250 | @table @kbd | |
251 | @item C-y | |
252 | Yank last killed text (@code{yank}). | |
253 | @item M-y | |
254 | Replace text just yanked with an earlier batch of killed text | |
255 | (@code{yank-pop}). | |
6bf7aab6 DL |
256 | @item C-M-w |
257 | Append next kill to last batch of killed text (@code{append-next-kill}). | |
258 | @end table | |
259 | ||
144e981a RS |
260 | On graphical displays with window systems, if there is a current |
261 | selection in some other application, and you selected it more recently | |
262 | than you killed any text in Emacs, @kbd{C-y} copies the selection | |
263 | instead of text killed within Emacs. | |
7464a646 | 264 | |
6bf7aab6 | 265 | @menu |
8838673e GM |
266 | * Kill Ring:: Where killed text is stored. Basic yanking. |
267 | * Appending Kills:: Several kills in a row all yank together. | |
268 | * Earlier Kills:: Yanking something killed some time ago. | |
6bf7aab6 DL |
269 | @end menu |
270 | ||
271 | @node Kill Ring | |
272 | @subsection The Kill Ring | |
273 | ||
7e9bf756 CY |
274 | All killed text is recorded in the @dfn{kill ring}, a list of blocks |
275 | of text that have been killed. There is only one kill ring, shared by | |
276 | all buffers, so you can kill text in one buffer and yank it in another | |
277 | buffer. This is the usual way to move text from one file to another. | |
278 | (There are several other methods: for instance, you could store the | |
279 | text in a register. @xref{Registers}, for information about | |
280 | registers. @xref{Accumulating Text}, for some other ways to move text | |
281 | around.) | |
6bf7aab6 DL |
282 | |
283 | @kindex C-y | |
284 | @findex yank | |
4c987d71 CY |
285 | The command @kbd{C-y} (@code{yank}) reinserts the text of the most |
286 | recent kill, leaving the cursor at the end of the text. It also adds | |
287 | the position of the beginning of the text to the mark ring, without | |
288 | activating the mark; this allows you to jump easily to that position | |
7e9bf756 | 289 | with @kbd{C-x C-x} (@pxref{Setting Mark}). With a plain prefix |
4c987d71 CY |
290 | argument (@kbd{C-u C-y}), it instead leaves the cursor in front of the |
291 | text, and adds the position of the end of the text to the mark ring. | |
292 | Using other sort of prefix argument specifies an earlier kill; for | |
293 | example, @kbd{C-u 4 C-y} reinserts the fourth most recent kill. | |
2844d832 | 294 | @xref{Earlier Kills}. |
6bf7aab6 | 295 | |
c1a50bee RS |
296 | @cindex yanking and text properties |
297 | @vindex yank-excluded-properties | |
7e9bf756 CY |
298 | The yank commands discard certain properties from the yanked text. |
299 | These are properties that might lead to annoying results, such as | |
300 | causing the text to respond to the mouse or specifying key bindings. | |
301 | The list of properties to discard is stored in the variable | |
302 | @code{yank-excluded-properties}. Yanking of register contents and | |
303 | rectangles also discard these properties. @xref{Text Properties,,, | |
304 | elisp, the Emacs Lisp Reference Manual}, for more information about | |
305 | text properties. | |
6bf7aab6 DL |
306 | |
307 | @node Appending Kills | |
308 | @subsection Appending Kills | |
309 | ||
310 | @cindex appending kills in the ring | |
6bf7aab6 DL |
311 | Normally, each kill command pushes a new entry onto the kill ring. |
312 | However, two or more kill commands in a row combine their text into a | |
313 | single entry, so that a single @kbd{C-y} yanks all the text as a unit, | |
314 | just as it was before it was killed. | |
315 | ||
316 | Thus, if you want to yank text as a unit, you need not kill all of it | |
317 | with one command; you can keep killing line after line, or word after | |
318 | word, until you have killed it all, and you can still get it all back at | |
319 | once. | |
320 | ||
321 | Commands that kill forward from point add onto the end of the previous | |
322 | killed text. Commands that kill backward from point add text onto the | |
323 | beginning. This way, any sequence of mixed forward and backward kill | |
324 | commands puts all the killed text into one entry without rearrangement. | |
325 | Numeric arguments do not break the sequence of appending kills. For | |
326 | example, suppose the buffer contains this text: | |
327 | ||
328 | @example | |
329 | This is a line @point{}of sample text. | |
330 | @end example | |
331 | ||
332 | @noindent | |
333 | with point shown by @point{}. If you type @kbd{M-d M-@key{DEL} M-d | |
334 | M-@key{DEL}}, killing alternately forward and backward, you end up with | |
335 | @samp{a line of sample} as one entry in the kill ring, and @samp{This | |
58fa012d EZ |
336 | is@ @ text.} in the buffer. (Note the double space between @samp{is} |
337 | and @samp{text}, which you can clean up with @kbd{M-@key{SPC}} or | |
338 | @kbd{M-q}.) | |
6bf7aab6 DL |
339 | |
340 | Another way to kill the same text is to move back two words with | |
341 | @kbd{M-b M-b}, then kill all four words forward with @kbd{C-u M-d}. | |
342 | This produces exactly the same results in the buffer and in the kill | |
343 | ring. @kbd{M-f M-f C-u M-@key{DEL}} kills the same text, all going | |
344 | backward; once again, the result is the same. The text in the kill ring | |
345 | entry always has the same order that it had in the buffer before you | |
346 | killed it. | |
347 | ||
348 | @kindex C-M-w | |
349 | @findex append-next-kill | |
350 | If a kill command is separated from the last kill command by other | |
351 | commands (not just numeric arguments), it starts a new entry on the kill | |
352 | ring. But you can force it to append by first typing the command | |
353 | @kbd{C-M-w} (@code{append-next-kill}) right before it. The @kbd{C-M-w} | |
354 | tells the following command, if it is a kill command, to append the text | |
355 | it kills to the last killed text, instead of starting a new entry. With | |
356 | @kbd{C-M-w}, you can kill several separated pieces of text and | |
357 | accumulate them to be yanked back in one place.@refill | |
358 | ||
7e9bf756 CY |
359 | A kill command following @kbd{M-w} (@code{kill-ring-save}) does not |
360 | append to the text that @kbd{M-w} copied into the kill ring. | |
6bf7aab6 DL |
361 | |
362 | @node Earlier Kills | |
363 | @subsection Yanking Earlier Kills | |
364 | ||
365 | @cindex yanking previous kills | |
366 | @kindex M-y | |
367 | @findex yank-pop | |
368 | To recover killed text that is no longer the most recent kill, use the | |
369 | @kbd{M-y} command (@code{yank-pop}). It takes the text previously | |
370 | yanked and replaces it with the text from an earlier kill. So, to | |
371 | recover the text of the next-to-the-last kill, first use @kbd{C-y} to | |
372 | yank the last kill, and then use @kbd{M-y} to replace it with the | |
373 | previous kill. @kbd{M-y} is allowed only after a @kbd{C-y} or another | |
374 | @kbd{M-y}. | |
375 | ||
376 | You can understand @kbd{M-y} in terms of a ``last yank'' pointer which | |
377 | points at an entry in the kill ring. Each time you kill, the ``last | |
378 | yank'' pointer moves to the newly made entry at the front of the ring. | |
379 | @kbd{C-y} yanks the entry which the ``last yank'' pointer points to. | |
380 | @kbd{M-y} moves the ``last yank'' pointer to a different entry, and the | |
381 | text in the buffer changes to match. Enough @kbd{M-y} commands can move | |
382 | the pointer to any entry in the ring, so you can get any entry into the | |
383 | buffer. Eventually the pointer reaches the end of the ring; the next | |
58fa012d | 384 | @kbd{M-y} loops back around to the first entry again. |
6bf7aab6 DL |
385 | |
386 | @kbd{M-y} moves the ``last yank'' pointer around the ring, but it does | |
387 | not change the order of the entries in the ring, which always runs from | |
388 | the most recent kill at the front to the oldest one still remembered. | |
389 | ||
390 | @kbd{M-y} can take a numeric argument, which tells it how many entries | |
391 | to advance the ``last yank'' pointer by. A negative argument moves the | |
392 | pointer toward the front of the ring; from the front of the ring, it | |
393 | moves ``around'' to the last entry and continues forward from there. | |
394 | ||
395 | Once the text you are looking for is brought into the buffer, you can | |
396 | stop doing @kbd{M-y} commands and it will stay there. It's just a copy | |
397 | of the kill ring entry, so editing it in the buffer does not change | |
398 | what's in the ring. As long as no new killing is done, the ``last | |
399 | yank'' pointer remains at the same place in the kill ring, so repeating | |
400 | @kbd{C-y} will yank another copy of the same previous kill. | |
401 | ||
0ec1f115 RS |
402 | If you know how many @kbd{M-y} commands it would take to find the |
403 | text you want, you can yank that text in one step using @kbd{C-y} with | |
404 | a numeric argument. @kbd{C-y} with an argument restores the text from | |
405 | the specified kill ring entry, counting back from the most recent as | |
406 | 1. Thus, @kbd{C-u 2 C-y} gets the next-to-the-last block of killed | |
407 | text---it is equivalent to @kbd{C-y M-y}. @kbd{C-y} with a numeric | |
408 | argument starts counting from the ``last yank'' pointer, and sets the | |
409 | ``last yank'' pointer to the entry that it yanks. | |
6bf7aab6 DL |
410 | |
411 | @vindex kill-ring-max | |
412 | The length of the kill ring is controlled by the variable | |
413 | @code{kill-ring-max}; no more than that many blocks of killed text are | |
414 | saved. | |
415 | ||
416 | @vindex kill-ring | |
417 | The actual contents of the kill ring are stored in a variable named | |
418 | @code{kill-ring}; you can view the entire contents of the kill ring with | |
419 | the command @kbd{C-h v kill-ring}. | |
420 | ||
b103c904 | 421 | @node Accumulating Text |
6bf7aab6 DL |
422 | @section Accumulating Text |
423 | @findex append-to-buffer | |
424 | @findex prepend-to-buffer | |
425 | @findex copy-to-buffer | |
426 | @findex append-to-file | |
427 | ||
428 | @cindex accumulating scattered text | |
429 | Usually we copy or move text by killing it and yanking it, but there | |
3423ce02 | 430 | are other convenient methods for copying one block of text in many |
7e9bf756 CY |
431 | places, or for copying many scattered blocks of text into one place. |
432 | Here we describe the commands to accumulate scattered pieces of text | |
433 | into a buffer or into a file. | |
6bf7aab6 DL |
434 | |
435 | @table @kbd | |
436 | @item M-x append-to-buffer | |
0ec1f115 | 437 | Append region to the contents of a specified buffer. |
6bf7aab6 | 438 | @item M-x prepend-to-buffer |
0ec1f115 | 439 | Prepend region to the contents of a specified buffer. |
6bf7aab6 | 440 | @item M-x copy-to-buffer |
58fa012d | 441 | Copy region into a specified buffer, deleting that buffer's old contents. |
6bf7aab6 | 442 | @item M-x insert-buffer |
0ec1f115 | 443 | Insert the contents of a specified buffer into current buffer at point. |
6bf7aab6 | 444 | @item M-x append-to-file |
0ec1f115 | 445 | Append region to the contents of a specified file, at the end. |
6bf7aab6 DL |
446 | @end table |
447 | ||
448 | To accumulate text into a buffer, use @kbd{M-x append-to-buffer}. | |
449 | This reads a buffer name, then inserts a copy of the region into the | |
450 | buffer specified. If you specify a nonexistent buffer, | |
451 | @code{append-to-buffer} creates the buffer. The text is inserted | |
452 | wherever point is in that buffer. If you have been using the buffer for | |
453 | editing, the copied text goes into the middle of the text of the buffer, | |
58fa012d | 454 | starting from wherever point happens to be at that moment. |
6bf7aab6 DL |
455 | |
456 | Point in that buffer is left at the end of the copied text, so | |
457 | successive uses of @code{append-to-buffer} accumulate the text in the | |
458 | specified buffer in the same order as they were copied. Strictly | |
459 | speaking, @code{append-to-buffer} does not always append to the text | |
460 | already in the buffer---it appends only if point in that buffer is at the end. | |
461 | However, if @code{append-to-buffer} is the only command you use to alter | |
462 | a buffer, then point is always at the end. | |
463 | ||
464 | @kbd{M-x prepend-to-buffer} is just like @code{append-to-buffer} | |
465 | except that point in the other buffer is left before the copied text, so | |
466 | successive prependings add text in reverse order. @kbd{M-x | |
58fa012d | 467 | copy-to-buffer} is similar, except that any existing text in the other |
6bf7aab6 DL |
468 | buffer is deleted, so the buffer is left containing just the text newly |
469 | copied into it. | |
470 | ||
4c987d71 CY |
471 | The command @kbd{M-x insert-buffer} can be used to retrieve the |
472 | accumulated text from another buffer. This prompts for the name of a | |
473 | buffer, and inserts a copy of all the text in that buffer into the | |
474 | current buffer at point, leaving point at the beginning of the | |
475 | inserted text. It also adds the position of the end of the inserted | |
476 | text to the mark ring, without activating the mark. @xref{Buffers}, | |
477 | for background information on buffers. | |
6bf7aab6 | 478 | |
7e9bf756 CY |
479 | Instead of accumulating text in a buffer, you can append text |
480 | directly into a file with @kbd{M-x append-to-file}. This prompts for | |
481 | a filename, and adds the text of the region to the end of the | |
482 | specified file. The file is changed immediately on disk. | |
6bf7aab6 DL |
483 | |
484 | You should use @code{append-to-file} only with files that are | |
485 | @emph{not} being visited in Emacs. Using it on a file that you are | |
486 | editing in Emacs would change the file behind Emacs's back, which | |
487 | can lead to losing some of your editing. | |
488 | ||
7e9bf756 CY |
489 | Another way to move text around is to store it in a register. |
490 | @xref{Registers}. | |
491 | ||
b103c904 | 492 | @node Rectangles |
6bf7aab6 DL |
493 | @section Rectangles |
494 | @cindex rectangle | |
495 | @cindex columns (and rectangles) | |
496 | @cindex killing rectangular areas of text | |
497 | ||
7e9bf756 CY |
498 | @dfn{Rectangle} commands operate on rectangular areas of the text: |
499 | all the characters between a certain pair of columns, in a certain | |
500 | range of lines. Emacs has commands to kill rectangles, yank killed | |
501 | rectangles, clear them out, fill them with blanks or text, or delete | |
502 | them. Rectangle commands are useful with text in multicolumn formats, | |
503 | and for changing text into or out of such formats. | |
6bf7aab6 | 504 | |
077b72a4 | 505 | @cindex mark rectangle |
6bf7aab6 DL |
506 | When you must specify a rectangle for a command to work on, you do it |
507 | by putting the mark at one corner and point at the opposite corner. The | |
508 | rectangle thus specified is called the @dfn{region-rectangle} because | |
58fa012d | 509 | you control it in much the same way as the region is controlled. But |
6bf7aab6 DL |
510 | remember that a given combination of point and mark values can be |
511 | interpreted either as a region or as a rectangle, depending on the | |
512 | command that uses them. | |
513 | ||
514 | If point and the mark are in the same column, the rectangle they | |
515 | delimit is empty. If they are in the same line, the rectangle is one | |
516 | line high. This asymmetry between lines and columns comes about | |
517 | because point (and likewise the mark) is between two columns, but within | |
518 | a line. | |
519 | ||
520 | @table @kbd | |
521 | @item C-x r k | |
177c0ea7 | 522 | Kill the text of the region-rectangle, saving its contents as the |
6bf7aab6 DL |
523 | ``last killed rectangle'' (@code{kill-rectangle}). |
524 | @item C-x r d | |
525 | Delete the text of the region-rectangle (@code{delete-rectangle}). | |
526 | @item C-x r y | |
527 | Yank the last killed rectangle with its upper left corner at point | |
528 | (@code{yank-rectangle}). | |
529 | @item C-x r o | |
530 | Insert blank space to fill the space of the region-rectangle | |
531 | (@code{open-rectangle}). This pushes the previous contents of the | |
532 | region-rectangle rightward. | |
3b4d49d7 | 533 | @item C-x r c |
df7593dd | 534 | Clear the region-rectangle by replacing all of its contents with spaces |
3b4d49d7 | 535 | (@code{clear-rectangle}). |
6bf7aab6 DL |
536 | @item M-x delete-whitespace-rectangle |
537 | Delete whitespace in each of the lines on the specified rectangle, | |
538 | starting from the left edge column of the rectangle. | |
d621caf7 | 539 | @item C-x r t @var{string} @key{RET} |
df7593dd | 540 | Replace rectangle contents with @var{string} on each line |
6bf7aab6 | 541 | (@code{string-rectangle}). |
1e1e6d52 | 542 | @item M-x string-insert-rectangle @key{RET} @var{string} @key{RET} |
e9db3bf2 | 543 | Insert @var{string} on each line of the rectangle. |
6bf7aab6 DL |
544 | @end table |
545 | ||
58fa012d EZ |
546 | The rectangle operations fall into two classes: commands for |
547 | deleting and inserting rectangles, and commands for blank rectangles. | |
6bf7aab6 DL |
548 | |
549 | @kindex C-x r k | |
550 | @kindex C-x r d | |
551 | @findex kill-rectangle | |
552 | @findex delete-rectangle | |
553 | There are two ways to get rid of the text in a rectangle: you can | |
554 | discard the text (delete it) or save it as the ``last killed'' | |
555 | rectangle. The commands for these two ways are @kbd{C-x r d} | |
556 | (@code{delete-rectangle}) and @kbd{C-x r k} (@code{kill-rectangle}). In | |
557 | either case, the portion of each line that falls inside the rectangle's | |
58fa012d | 558 | boundaries is deleted, causing any following text on the line to |
6bf7aab6 DL |
559 | move left into the gap. |
560 | ||
561 | Note that ``killing'' a rectangle is not killing in the usual sense; the | |
562 | rectangle is not stored in the kill ring, but in a special place that | |
563 | can only record the most recent rectangle killed. This is because yanking | |
564 | a rectangle is so different from yanking linear text that different yank | |
144e981a RS |
565 | commands have to be used. It is hard to define yank-popping for rectangles, |
566 | so we do not try. | |
6bf7aab6 DL |
567 | |
568 | @kindex C-x r y | |
569 | @findex yank-rectangle | |
570 | To yank the last killed rectangle, type @kbd{C-x r y} | |
571 | (@code{yank-rectangle}). Yanking a rectangle is the opposite of killing | |
572 | one. Point specifies where to put the rectangle's upper left corner. | |
573 | The rectangle's first line is inserted there, the rectangle's second | |
d7d7da37 EZ |
574 | line is inserted at the same horizontal position, but one line |
575 | vertically down, and so on. The number of lines affected is determined | |
576 | by the height of the saved rectangle. | |
6bf7aab6 DL |
577 | |
578 | You can convert single-column lists into double-column lists using | |
579 | rectangle killing and yanking; kill the second half of the list as a | |
580 | rectangle and then yank it beside the first line of the list. | |
581 | @xref{Two-Column}, for another way to edit multi-column text. | |
582 | ||
583 | You can also copy rectangles into and out of registers with @kbd{C-x r | |
584 | r @var{r}} and @kbd{C-x r i @var{r}}. @xref{RegRect,,Rectangle | |
585 | Registers}. | |
586 | ||
587 | @kindex C-x r o | |
588 | @findex open-rectangle | |
3b4d49d7 | 589 | @kindex C-x r c |
6bf7aab6 DL |
590 | @findex clear-rectangle |
591 | There are two commands you can use for making blank rectangles: | |
3b4d49d7 RS |
592 | @kbd{C-x r c} (@code{clear-rectangle}) which blanks out existing text, |
593 | and @kbd{C-x r o} (@code{open-rectangle}) which inserts a blank | |
594 | rectangle. Clearing a rectangle is equivalent to deleting it and then | |
595 | inserting a blank rectangle of the same size. | |
6bf7aab6 DL |
596 | |
597 | @findex delete-whitespace-rectangle | |
598 | The command @kbd{M-x delete-whitespace-rectangle} deletes horizontal | |
599 | whitespace starting from a particular column. This applies to each of | |
600 | the lines in the rectangle, and the column is specified by the left | |
601 | edge of the rectangle. The right edge of the rectangle does not make | |
602 | any difference to this command. | |
603 | ||
604 | @kindex C-x r t | |
605 | @findex string-rectangle | |
d57211a3 | 606 | The command @kbd{C-x r t} (@code{string-rectangle}) replaces the |
1e1e6d52 GM |
607 | contents of a region-rectangle with a string on each line. The |
608 | string's width need not be the same as the width of the rectangle. If | |
609 | the string's width is less, the text after the rectangle shifts left; | |
610 | if the string is wider than the rectangle, the text after the | |
611 | rectangle shifts right. | |
612 | ||
613 | @findex string-insert-rectangle | |
177c0ea7 JB |
614 | The command @kbd{M-x string-insert-rectangle} is similar to |
615 | @code{string-rectangle}, but inserts the string on each line, | |
1e1e6d52 | 616 | shifting the original text to the right. |
ab5796a9 | 617 | |
b103c904 | 618 | @node CUA Bindings |
38fe0612 | 619 | @section CUA Bindings |
95a0306c KS |
620 | @findex cua-mode |
621 | @vindex cua-mode | |
622 | @cindex CUA key bindings | |
623 | @vindex cua-enable-cua-keys | |
624 | The command @kbd{M-x cua-mode} sets up key bindings that are | |
625 | compatible with the Common User Access (CUA) system used in many other | |
626 | applications. @kbd{C-x} means cut (kill), @kbd{C-c} copy, @kbd{C-v} | |
627 | paste (yank), and @kbd{C-z} undo. Standard Emacs commands like | |
628 | @kbd{C-x C-c} still work, because @kbd{C-x} and @kbd{C-c} only take | |
19126168 | 629 | effect when the mark is active (and the region is highlighted). |
25e5d36a KS |
630 | However, if you don't want to override these bindings in Emacs at all, |
631 | set @code{cua-enable-cua-keys} to @code{nil}. | |
95a0306c | 632 | |
144e981a RS |
633 | To enter an Emacs command like @kbd{C-x C-f} while the mark is |
634 | active, use one of the following methods: either hold @kbd{Shift} | |
635 | together with the prefix key, e.g. @kbd{S-C-x C-f}, or quickly type | |
636 | the prefix key twice, e.g. @kbd{C-x C-x C-f}. | |
95a0306c | 637 | |
7e9bf756 CY |
638 | In CUA mode, typed text replaces the active region as in |
639 | Delete-Selection mode (@pxref{Mouse Commands}). | |
640 | ||
95a0306c KS |
641 | @cindex rectangle highlighting |
642 | CUA mode provides enhanced rectangle support with visible | |
643 | rectangle highlighting. Use @kbd{C-RET} to start a rectangle, | |
644 | extend it using the movement commands, and cut or copy it using | |
19126168 KS |
645 | @kbd{C-x} or @kbd{C-c}. @kbd{RET} moves the cursor to the next |
646 | (clockwise) corner of the rectangle, so you can easily expand it in | |
647 | any direction. Normal text you type is inserted to the left or right | |
648 | of each line in the rectangle (on the same side as the cursor). | |
95a0306c KS |
649 | |
650 | With CUA you can easily copy text and rectangles into and out of | |
40b6cb79 | 651 | registers by providing a one-digit numeric prefix to the kill, copy, |
95a0306c KS |
652 | and yank commands, e.g. @kbd{C-1 C-c} copies the region into register |
653 | @code{1}, and @kbd{C-2 C-v} yanks the contents of register @code{2}. | |
654 | ||
655 | @cindex global mark | |
656 | CUA mode also has a global mark feature which allows easy moving and | |
657 | copying of text between buffers. Use @kbd{C-S-SPC} to toggle the | |
658 | global mark on and off. When the global mark is on, all text that you | |
659 | kill or copy is automatically inserted at the global mark, and text | |
660 | you type is inserted at the global mark rather than at the current | |
661 | position. | |
662 | ||
663 | For example, to copy words from various buffers into a word list in | |
664 | a given buffer, set the global mark in the target buffer, then | |
665 | navigate to each of the words you want in the list, mark it (e.g. with | |
666 | @kbd{S-M-f}), copy it to the list with @kbd{C-c} or @kbd{M-w}, and | |
667 | insert a newline after the word in the target list by pressing | |
668 | @key{RET}. |