]> code.delx.au - gnu-emacs/blob - doc/emacs/mark.texi
Convert consecutive FSF copyright years to ranges.
[gnu-emacs] / doc / emacs / mark.texi
1 @c This is part of the Emacs manual.
2 @c Copyright (C) 1985-1987, 1993-1995, 1997, 2001-2011
3 @c Free Software Foundation, Inc.
4 @c See file emacs.texi for copying conditions.
5 @node Mark, Killing, Help, Top
6 @chapter The Mark and the Region
7 @cindex mark
8 @cindex setting a mark
9 @cindex region
10
11 Many Emacs commands operate on an arbitrary contiguous part of the
12 current buffer. To specify the text for such a command to operate on,
13 you set @dfn{the mark} at one end of it, and move point to the other
14 end. The text between point and the mark is called @dfn{the region}.
15 The region always extends between point and the mark, no matter which
16 one comes earlier in the text; each time you move point, the region
17 changes.
18
19 Setting the mark at a position in the text also @dfn{activates} it.
20 When the mark is active, Emacs indicates the extent of the region by
21 highlighting the text within it, using the @code{region} face
22 (@pxref{Face Customization}). After certain non-motion commands,
23 including any command that changes the text in the buffer, Emacs
24 automatically @dfn{deactivates} the mark; this turns off the
25 highlighting. You can also explicitly deactivate the mark at any
26 time, by typing @kbd{C-g} (@pxref{Quitting}).
27
28 This default behavior of the mark is known as Transient Mark mode.
29 Disabling Transient Mark mode switches Emacs to an alternative
30 behavior, in which the mark is always active, which was the default
31 prior to Emacs 23. @xref{Persistent Mark}.
32
33 @vindex highlight-nonselected-windows
34 Setting the mark in one buffer has no effect on the marks in other
35 buffers. When you return to a buffer with an active mark, the mark is
36 at the same place as before. When multiple windows show the same
37 buffer, they can have different values of point, and thus different
38 regions, but they all share one common mark position. @xref{Windows}.
39 Ordinarily, only the selected window highlights its region; however,
40 if the variable @code{highlight-nonselected-windows} is
41 non-@code{nil}, each window highlights its own region.
42
43 @menu
44 * Setting Mark:: Commands to set the mark.
45 * Marking Objects:: Commands to put region around textual units.
46 * Using Region:: Summary of ways to operate on contents of the region.
47 * Mark Ring:: Previous mark positions saved so you can go back there.
48 * Global Mark Ring:: Previous mark positions in various buffers.
49 * Shift Selection:: Using shifted cursor motion keys.
50 * Persistent Mark:: Keeping the mark active all the time.
51 @end menu
52
53 @node Setting Mark
54 @section Setting the Mark
55
56 Here are some commands for setting the mark:
57
58 @table @kbd
59 @item C-@key{SPC}
60 Set the mark at point, and activate it (@code{set-mark-command}).
61 @item C-@@
62 The same.
63 @item C-x C-x
64 Set the mark at point, and move point where the mark was
65 (@code{exchange-point-and-mark}).
66 @item Drag-Mouse-1
67 Set point and the mark around the text you drag across.
68 @item Mouse-3
69 Set the mark at point, then move point to where you click
70 (@code{mouse-save-then-kill}).
71 @item @samp{Shifted motion keys}
72 Set the mark at point if the mark is inactive, then move point.
73 @end table
74
75 @kindex C-SPC
76 @kindex C-@@
77 @findex set-mark-command
78 The most common way to set the mark is with @kbd{C-@key{SPC}}
79 (@code{set-mark-command})@footnote{There is no @kbd{C-@key{SPC}}
80 character in @acronym{ASCII}; usually, typing @kbd{C-@key{SPC}} on a
81 text terminal gives the character @kbd{C-@@}. This key is also bound
82 to @code{set-mark-command}, so unless you are unlucky enough to have
83 a text terminal that behaves differently, you might as well think of
84 @kbd{C-@@} as @kbd{C-@key{SPC}}.}. This sets the mark where point is,
85 and activates it. You can then move point away, leaving the mark
86 behind.
87
88 For example, suppose you wish to convert part of the buffer to upper
89 case. To accomplish this, go to the beginning of the desired text,
90 type @kbd{C-@key{SPC}}, and move point until the desired portion of
91 text is highlighted. Now type @kbd{C-x C-u} (@code{upcase-region}).
92 This converts the text in the region to upper case, and then
93 deactivates the mark.
94
95 @kindex C-x C-x
96 @findex exchange-point-and-mark
97 The command @kbd{C-x C-x} (@code{exchange-point-and-mark}) exchanges
98 the positions of point and the mark, keeping the region unchanged. If
99 the mark is inactive, Emacs first reactivates the mark wherever it was
100 last set. @kbd{C-x C-x} is useful when you are satisfied with the
101 position of point but want to move the other end of the region (where
102 the mark is). Using @kbd{C-x C-x} a second time, if necessary, puts
103 the mark at the new position with point back at its original position.
104
105 You can also set the mark with the mouse. If you press the left
106 mouse button (@kbd{down-mouse-1}) and drag the mouse across a range of
107 text while holding down this button, this sets the mark where you
108 first pressed the mouse button and puts point where you release it.
109 Alternatively, clicking the right mouse button (@kbd{mouse-3}) sets
110 the mark at point and then moves point to where you clicked. Using
111 the mouse to mark a region also copies the region into the kill ring
112 (@pxref{Kill Ring}). @xref{Mouse Commands}, for a more detailed
113 description of these mouse commands.
114
115 @cindex shift-selection
116 Finally, you can set the mark by holding down the shift key while
117 typing certain cursor motion commands (such as @kbd{S-@key{right}},
118 @kbd{S-C-f}, @kbd{S-C-n}, etc.) This is referred to as
119 @dfn{shift-selection}. This sets the mark at point before moving
120 point, but only if there is no active mark set via shift-selection.
121 The mark set by mouse commands and by shift-selection behaves slightly
122 differently from the usual mark: any subsequent unshifted cursor
123 motion command deactivates it automatically. For details, @xref{Shift
124 Selection}.
125
126 Whenever the mark is active, you can deactivate it by typing
127 @kbd{C-g} (@pxref{Quitting}). The mark is also automatically
128 deactivated after certain non-motion commands.
129
130 @node Marking Objects
131 @section Commands to Mark Textual Objects
132
133 @cindex marking sections of text
134 Here are the commands for placing point and the mark around a textual
135 object such as a word, list, paragraph or page:
136
137 @table @kbd
138 @item M-@@
139 Set mark after end of next word (@code{mark-word}). This does not
140 move point.
141 @item C-M-@@
142 Set mark after end of following balanced expression
143 (@code{mark-sexp}). This does not move point.
144 @item M-h
145 Move point to the beginning of the current paragraph, and set mark at
146 the end (@code{mark-paragraph}).
147 @item C-M-h
148 Move point to the beginning of the current defun, and set mark at the
149 end (@code{mark-defun}).
150 @item C-x C-p
151 Move point to the beginning of the current page, and set mark at the
152 end (@code{mark-page}).
153 @item C-x h
154 Move point to the beginning of the buffer, and set mark at the end
155 (@code{mark-whole-buffer}).
156 @end table
157
158 @findex mark-word
159 @findex mark-sexp
160 @kbd{M-@@} (@code{mark-word}) puts the mark at the end of the next
161 word, while @kbd{C-M-@@} (@code{mark-sexp}) puts it at the end of the
162 next balanced expression (@pxref{Expressions}). These commands handle
163 arguments just like @kbd{M-f} and @kbd{C-M-f}.
164
165 @kindex C-x h
166 @findex mark-whole-buffer
167 The other commands in the above list set both point and mark, so as
168 to delimit an object in the buffer. @kbd{M-h} (@code{mark-paragraph})
169 moves point to the beginning of the paragraph that surrounds or
170 follows point, and sets the mark at the end of that paragraph
171 (@pxref{Paragraphs}). As a special exception, repeated invocations of
172 @kbd{M-h} extend the region to subsequent paragraphs. This is
173 convenient for indenting, case-converting, or killing entire
174 paragraphs.
175
176 The @kbd{M-h} command accepts prefix arguments. If the argument's
177 value is positive, @kbd{M-h} marks that many paragraphs starting with
178 the one surrounding point; therefore, @kbd{C-u M-h} is equivalent to
179 @kbd{M-h M-h M-h M-h}. If the prefix argument is @minus{}@var{n},
180 @kbd{M-h} marks @var{n} paragraphs running back from the one
181 surrounding point; in this case, point moves forward to the end of
182 that paragraph, and the mark goes at the start of the region.
183
184 Similarly, @kbd{C-M-h} (@code{mark-defun}) sets mark and point
185 around major top-level definitions (@pxref{Moving by Defuns}), and
186 @kbd{C-x C-p} (@code{mark-page}) does the same for pages
187 (@pxref{Pages}). These treat repeated invocations and prefix
188 arguments similarly to @code{mark-paragraph}.
189
190 Finally, @kbd{C-x h} (@code{mark-whole-buffer}) sets up the entire
191 buffer as the region, by putting point at the beginning and the mark
192 at the end. (In some programs this is called ``select all.'')
193
194 @node Using Region
195 @section Operating on the Region
196
197 @cindex operations on a marked region
198 Once you have a region, here are some of the ways you can operate on
199 it:
200
201 @itemize @bullet
202 @item
203 Kill it with @kbd{C-w} (@pxref{Killing}).
204 @item
205 Copy it to the kill ring with @kbd{M-w} (@pxref{Yanking}).
206 @item
207 Convert case with @kbd{C-x C-l} or @kbd{C-x C-u} (@pxref{Case}).
208 @item
209 Undo changes within it using @kbd{C-u C-/} (@pxref{Undo}).
210 @item
211 Replace text within it using @kbd{M-%} (@pxref{Query Replace}).
212 @item
213 Indent it with @kbd{C-x @key{TAB}} or @kbd{C-M-\} (@pxref{Indentation}).
214 @item
215 Fill it as text with @kbd{M-x fill-region} (@pxref{Filling}).
216 @item
217 Print hardcopy with @kbd{M-x print-region} (@pxref{Printing}).
218 @item
219 Evaluate it as Lisp code with @kbd{M-x eval-region} (@pxref{Lisp Eval}).
220 @item
221 Save it in a register with @kbd{C-x r s} (@pxref{Registers}).
222 @item
223 Save it in a buffer or a file (@pxref{Accumulating Text}).
224 @end itemize
225
226 Most commands that operate on the text in the region have the word
227 @code{region} in their names.
228
229 Some commands have a default behavior when the mark is inactive, but
230 operate on the text in the region if the mark is active. For example,
231 @kbd{M-$} (@code{ispell-word}) normally checks the spelling of the
232 word at point, but it checks the text in the region if the region is
233 active (@pxref{Spelling}). Normally, such commands use their default
234 behavior if the region is empty (i.e., if mark and point are at the
235 same position). If you want them to operate on the empty region,
236 change the variable @code{use-empty-active-region} to @code{t}.
237
238 @cindex Delete Selection mode
239 @cindex mode, Delete Selection
240 @findex delete-selection-mode
241 If you enable Delete Selection mode, a minor mode, then inserting
242 text while the mark is active causes the selected text to be deleted
243 first. This also deactivates the mark. Many graphical applications
244 follow this convention, but Emacs does not. To toggle Delete
245 Selection mode on or off, type @kbd{M-x delete-selection-mode}.
246 Another effect of this mode is that some keys, such as @key{DEL} and
247 @kbd{C-d}, always kill the region if one exists.
248
249 @node Mark Ring
250 @section The Mark Ring
251
252 @cindex mark ring
253 Aside from delimiting the region, the mark is also useful for
254 remembering spots that you may want to go back to. Each buffer
255 remembers 16 previous locations of the mark, in the @dfn{mark ring}.
256 Commands that set the mark also push the old mark onto this ring.
257
258 @table @kbd
259 @item C-@key{SPC} C-@key{SPC}
260 Set the mark, pushing it onto the mark ring, without activating it.
261 @item C-u C-@key{SPC}
262 Move point to where the mark was, and restore the mark from the ring
263 of former marks.
264 @end table
265
266 @kindex C-SPC C-SPC
267 The command C-@key{SPC} C-@key{SPC} is handy when you want to use
268 the mark to remember a position to which you may wish to return. It
269 pushes the current point onto the mark ring, without activating the
270 mark (which would cause Emacs to highlight the region). This is
271 actually two consecutive invocations of @kbd{C-@key{SPC}}
272 (@code{set-mark-command}); the first @kbd{C-@key{SPC}} sets the mark,
273 and the second @kbd{C-@key{SPC}} deactivates it. (When Transient Mark
274 mode is off, C-@key{SPC} C-@key{SPC} instead activates Transient Mark
275 mode temporarily. @xref{Persistent Mark}.)
276
277 @kindex C-u C-SPC
278 To return to a marked position, use @code{set-mark-command} with a
279 prefix argument: @kbd{C-u C-@key{SPC}}. This moves point to where the
280 mark was, and deactivates the mark if it was active. Each subsequent
281 @kbd{C-u C-@key{SPC}} jumps to a prior position stored in the mark
282 ring. The positions you move through in this way are not lost; they
283 go to the end of the ring.
284
285 @vindex set-mark-command-repeat-pop
286 If you set @code{set-mark-command-repeat-pop} to non-@code{nil},
287 then immediately after you type @kbd{C-u C-@key{SPC}}, you can type
288 @kbd{C-@key{SPC}} instead of @kbd{C-u C-@key{SPC}} to cycle through
289 the mark ring. By default, @code{set-mark-command-repeat-pop} is
290 @code{nil}.
291
292 Each buffer has its own mark ring. All editing commands use the
293 current buffer's mark ring. In particular, @kbd{C-u C-@key{SPC}}
294 always stays in the same buffer.
295
296 @vindex mark-ring-max
297 The variable @code{mark-ring-max} specifies the maximum number of
298 entries to keep in the mark ring. If that many entries exist and
299 another one is pushed, the earliest one in the list is discarded. Repeating
300 @kbd{C-u C-@key{SPC}} cycles through the positions currently in the
301 ring.
302
303 @vindex mark-even-if-inactive
304 If the variable @code{mark-even-if-inactive} is @code{nil}, commands
305 can only use the mark and the region when it is active. This variable
306 is non-@code{nil} by default.
307
308 If you want to move back to the same place over and over, the mark
309 ring may not be convenient enough. If so, you can record the position
310 in a register for later retrieval (@pxref{RegPos,, Saving Positions in
311 Registers}).
312
313 @node Global Mark Ring
314 @section The Global Mark Ring
315 @cindex global mark ring
316
317 In addition to the ordinary mark ring that belongs to each buffer,
318 Emacs has a single @dfn{global mark ring}. Each time you set a mark,
319 in any buffer, this is recorded in the global mark ring in addition to
320 the current buffer's own mark ring.
321
322 @kindex C-x C-@key{SPC}
323 @findex pop-global-mark
324 The command @kbd{C-x C-@key{SPC}} (@code{pop-global-mark}) jumps to
325 the buffer and position of the latest entry in the global ring. It also
326 rotates the ring, so that successive uses of @kbd{C-x C-@key{SPC}} take
327 you to earlier buffers and mark positions.
328
329 @node Shift Selection
330 @section Shift Selection
331 @cindex shift-selection
332
333 If you hold down the shift key while typing a cursor motion command,
334 this sets the mark before moving point, so that the region extends
335 from the original position of point to its new position. This
336 feature, newly introduced in Emacs 23, is referred to as
337 @dfn{shift-selection}. It is similar to the way text is selected in
338 other editors.
339
340 The mark set via shift-selection behaves a little differently from
341 what we have described above. Firstly, in addition to the usual ways
342 of deactivating the mark (such as changing the buffer text or typing
343 @kbd{C-g}), the mark is deactivated by any @emph{unshifted} cursor
344 motion command. Secondly, any subsequent @emph{shifted} cursor motion
345 command avoids setting the mark anew. Therefore, a series of shifted
346 cursor motion commands will continuously extend the region.
347
348 Shift-selection only works if the shifted cursor motion key is not
349 already bound to a separate command (@pxref{Customization}). For
350 example, if you bind @kbd{S-C-f} to another command, typing
351 @kbd{S-C-f} runs that command instead of performing a shift-selected
352 version of @kbd{C-f} (@code{forward-char}).
353
354 A mark set via mouse commands behaves the same as a mark set via
355 shift-selection (@pxref{Setting Mark}). For example, if you specify a
356 region by dragging the mouse, you can continue to extend the region
357 using shifted cursor motion commands. In either case, any unshifted
358 cursor motion command deactivates the mark.
359
360 To turn off shift-selection, set @code{shift-select-mode} to
361 @code{nil}. Doing this does not disable setting the mark via mouse
362 commands.
363
364 @node Persistent Mark
365 @section Persistent Marks
366 @cindex mode, Transient Mark
367 @cindex Transient Mark mode
368 @cindex highlighting region
369 @cindex region highlighting
370 @cindex Zmacs mode
371
372 By default, the mark is activated by setting it, and deactivated by
373 most non-motion commands (including all commands that change the text
374 in the buffer). This behavior is called Transient Mark
375 mode@footnote{It is also sometimes called @dfn{Zmacs mode}, because
376 the Zmacs editor on the MIT Lisp Machine handled the mark in a similar
377 way.}.
378
379 Turning off Transient Mark mode switches Emacs to an alternative
380 mode of operation, which was the default prior to Emacs 23. When
381 Transient Mark mode is off, the mark is @emph{never} deactivated, but
382 it can be set to different locations using commands such as
383 @kbd{C-@key{SPC}}. Emacs does not highlight the region, because that
384 would be a nuisance. As a special exception, the region is
385 temporarily highlighted if you set it with the mouse (@pxref{Setting
386 Mark}), or with shift-selection (@pxref{Shift Selection}).
387
388 @findex transient-mark-mode
389 To turn off Transient Mark mode, type @kbd{M-x transient-mark-mode}.
390 This command toggles the mode; you can use the same command to turn it
391 on again. You can also toggle Transient Mark mode using the
392 @samp{Active Region Highlighting} menu item in the @samp{Options}
393 menu.
394
395 Here are the details of how Emacs behaves when Transient Mark mode
396 is off:
397
398 @itemize @bullet
399 @item
400 Emacs does not show where the mark is located---you have to remember.
401 The usual solution to this problem is to set the mark and then use it
402 soon, before you forget where it is. Alternatively, you can check the
403 location of the mark by using @kbd{C-x C-x}, which exchanges the
404 positions of the point and the mark (@pxref{Setting Mark}).
405
406 @item
407 Many commands that insert text, such as @kbd{C-y} (@code{yank}),
408 position point and the mark at opposite ends of the inserted text, so
409 that the region consists of the text just inserted. You can tell when
410 a command sets the mark because it displays @samp{Mark set} in the
411 echo area.
412
413 @item
414 Many commands that move point long distances, like @kbd{M-<} and
415 @kbd{C-s}, first set the mark where point was.
416
417 @item
418 Some commands, which ordinarily act on the region when the mark is
419 active, no longer do so. For example, normally @kbd{M-%}
420 (@code{query-replace}) performs replacements within the region, if the
421 mark is active. When Transient Mark mode is off, it always operates
422 from point to the end of the buffer. Commands that act this way are
423 identified in their own documentation.
424 @end itemize
425
426 While Transient Mark mode is off, you can activate it temporarily
427 using @kbd{C-@key{SPC} C-@key{SPC}} or @kbd{C-u C-x C-x}.
428
429 @table @kbd
430 @item C-@key{SPC} C-@key{SPC}
431 @kindex C-@key{SPC} C-@key{SPC}
432 Set the mark at point (like plain @kbd{C-@key{SPC}}) and enable
433 Transient Mark mode just once, until the mark is deactivated. (This
434 is not really a separate command; you are using the @kbd{C-@key{SPC}}
435 command twice.)
436
437 @item C-u C-x C-x
438 @kindex C-u C-x C-x
439 Activate the mark and enable Transient Mark mode temporarily, until
440 the mark is next deactivated. (This is the @kbd{C-x C-x} command,
441 @code{exchange-point-and-mark}, with a prefix argument.)
442 @end table
443
444 These commands set or activate the mark, and enable Transient Mark
445 mode only until the mark is deactivated. One reason you may want to
446 use them is that some commands operate on the entire buffer instead of
447 the region when Transient Mark mode is off. Enabling Transient Mark
448 mode momentarily gives you a way to use these commands on the region.