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