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