]> code.delx.au - gnu-emacs/blob - man/mark.texi
*** empty log message ***
[gnu-emacs] / man / mark.texi
1 @c This is part of the Emacs manual.
2 @c Copyright (C) 1985,86,87,93,94,95,1997,2001 Free Software Foundation, Inc.
3 @c See file emacs.texi for copying conditions.
4 @node Mark, Killing, Help, Top
5 @chapter The Mark and the Region
6 @cindex mark
7 @cindex setting a mark
8 @cindex region
9
10 Many Emacs commands operate on an arbitrary contiguous part of the
11 current buffer. To specify the text for such a command to operate on,
12 you set @dfn{the mark} at one end of it, and move point to the other
13 end. The text between point and the mark is called @dfn{the region}.
14 Emacs highlights the region whenever there is one, if you enable
15 Transient Mark mode (@pxref{Transient Mark}).
16
17 Certain Emacs commands set the mark; other editing commands do not
18 affect it, so the mark remains where you set it last. Each Emacs
19 buffer has its own mark, and setting the mark in one buffer has no
20 effect on other buffers' marks. When you return to a buffer that was
21 current earlier, its mark is at the same place as before.
22
23 The ends of the region are always point and the mark. It doesn't
24 matter which of them was put in its current place first, or which one
25 comes earlier in the text---the region starts from point or the mark
26 (whichever comes first), and ends at point or the mark (whichever
27 comes last). Every time you move point, or set the mark in a new
28 place, the region changes.
29
30 Many commands that insert text, such as @kbd{C-y} (@code{yank}) and
31 @kbd{M-x insert-buffer}, position point and the mark at opposite ends
32 of the inserted text, so that the region consists of the text just
33 inserted.
34
35 Aside from delimiting the region, the mark is also useful for
36 remembering a spot that you may want to go back to. To make this
37 feature more useful, each buffer remembers 16 previous locations of the
38 mark in the @dfn{mark ring}.
39
40 @menu
41 * Setting Mark:: Commands to set the mark.
42 * Transient Mark:: How to make Emacs highlight the region--
43 when there is one.
44 * Using Region:: Summary of ways to operate on contents of the region.
45 * Marking Objects:: Commands to put region around textual units.
46 * Mark Ring:: Previous mark positions saved so you can go back there.
47 * Global Mark Ring:: Previous mark positions in various buffers.
48 @end menu
49
50 @node Setting Mark
51 @section Setting the Mark
52
53 Here are some commands for setting the mark:
54
55 @table @kbd
56 @item C-@key{SPC}
57 Set the mark where point is (@code{set-mark-command}).
58 @item C-@@
59 The same.
60 @item C-x C-x
61 Interchange mark and point (@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 where point is, then move point to where you click
66 (@code{mouse-save-then-kill}).
67 @end table
68
69 For example, suppose you wish to convert part of the buffer to
70 upper case, using the @kbd{C-x C-u} (@code{upcase-region}) command,
71 which operates on the text in the region. You can first go to the
72 beginning of the text to be capitalized, type @kbd{C-@key{SPC}} to put
73 the mark there, move to the end, and then type @kbd{C-x C-u}. Or, you
74 can set the mark at the end of the text, move to the beginning, and then
75 type @kbd{C-x C-u}.
76
77 @kindex C-SPC
78 @findex set-mark-command
79 The most common way to set the mark is with the @kbd{C-@key{SPC}} command
80 (@code{set-mark-command}). This sets the mark where point is. Then you
81 can move point away, leaving the mark behind.
82
83 There are two ways to set the mark with the mouse. You can drag mouse
84 button one across a range of text; that puts point where you release the
85 mouse button, and sets the mark at the other end of that range. Or you
86 can click mouse button three, which sets the mark at point (like
87 @kbd{C-@key{SPC}}) and then moves point (like @kbd{Mouse-1}). Both of
88 these methods copy the region into the kill ring in addition to setting
89 the mark; that gives behavior consistent with other window-driven
90 applications, but if you don't want to modify the kill ring, you must
91 use keyboard commands to set the mark. @xref{Mouse Commands}.
92
93 @kindex C-x C-x
94 @findex exchange-point-and-mark
95 Ordinary terminals have only one cursor, so there is no way for Emacs
96 to show you where the mark is located. You have to remember. The usual
97 solution to this problem is to set the mark and then use it soon, before
98 you forget where it is. Alternatively, you can see where the mark is
99 with the command @kbd{C-x C-x} (@code{exchange-point-and-mark}) which
100 puts the mark where point was and point where the mark was. The extent
101 of the region is unchanged, but the cursor and point are now at the
102 previous position of the mark. In Transient Mark mode, this command
103 reactivates the mark.
104
105 @kbd{C-x C-x} is also useful when you are satisfied with the position
106 of point but want to move the other end of the region (where the mark
107 is); do @kbd{C-x C-x} to put point at that end of the region, and then
108 move it. Using @kbd{C-x C-x} a second time, if necessary, puts the mark at
109 the new position with point back at its original position.
110
111 For more facilities that allow you to go to previously set marks, see
112 @ref{Mark Ring}.
113
114 @kindex C-@@
115 There is no such character as @kbd{C-@key{SPC}} in ASCII; when you
116 type @key{SPC} while holding down @key{CTRL}, what you get on most
117 ordinary terminals is the character @kbd{C-@@}. This key is actually
118 bound to @code{set-mark-command}. But unless you are unlucky enough to
119 have a terminal where typing @kbd{C-@key{SPC}} does not produce
120 @kbd{C-@@}, you might as well think of this character as
121 @kbd{C-@key{SPC}}. Under X, @kbd{C-@key{SPC}} is actually a distinct
122 character, but its binding is still @code{set-mark-command}.
123
124 @node Transient Mark
125 @section Transient Mark Mode
126 @cindex mode, Transient Mark
127 @cindex Transient Mark mode
128 @cindex highlighting region
129 @cindex region highlighting
130
131 On a terminal that supports colors, Emacs can highlight the current
132 region. But normally it does not. Why not?
133
134 Highlighting the region whenever it exists would not be desirable in
135 Emacs, because once you have set a mark, there is @emph{always} a
136 region (in that buffer). And highlighting the region all the time
137 would be a nuisance. So normally Emacs highlights the region only
138 immediately after you have selected one with the mouse.
139
140 You can turn on region highlighting by enabling Transient Mark mode.
141 This is a more rigid mode of operation in which the region ``lasts''
142 only temporarily, so you must set up a region for each command that uses
143 one. In Transient Mark mode, most of the time there is no region;
144 therefore, highlighting the region when it exists is useful and
145 not annoying.
146
147 @findex transient-mark-mode
148 To enable Transient Mark mode, type @kbd{M-x transient-mark-mode}.
149 This command toggles the mode, so you can repeat the command to turn off
150 the mode.
151
152 Here are the details of Transient Mark mode:
153
154 @itemize @bullet
155 @item
156 To set the mark, type @kbd{C-@key{SPC}} (@code{set-mark-command}).
157 This makes the mark active and thus begins highlighting of the region.
158 As you move point, you will see the highlighted region grow and
159 shrink.
160
161 @item
162 The mouse commands for specifying the mark also make it active. So do
163 keyboard commands whose purpose is to specify a region, including
164 @kbd{M-@@}, @kbd{C-M-@@}, @kbd{M-h}, @kbd{C-M-h}, @kbd{C-x C-p}, and
165 @kbd{C-x h}.
166
167 @item
168 When the mark is active, you can execute commands that operate on the
169 region, such as killing, indenting, or writing to a file.
170
171 @item
172 Any change to the buffer, such as inserting or deleting a character,
173 deactivates the mark. This means any subsequent command that operates
174 on a region will get an error and refuse to operate. You can make the
175 region active again by typing @kbd{C-x C-x}.
176
177 @item
178 Commands like @kbd{M->} and @kbd{C-s}, that ``leave the mark behind'' in
179 addition to some other primary purpose, do not activate the new mark.
180 You can activate the new region by executing @kbd{C-x C-x}
181 (@code{exchange-point-and-mark}).
182
183 @item
184 @kbd{C-s} when the mark is active does not alter the mark.
185
186 @item
187 Quitting with @kbd{C-g} deactivates the mark.
188
189 @item
190 Some commands operate on the region whenever it is active. For
191 instance, @kbd{C-x u} in Transient Mark mode operates on the region
192 when there is a region. Outside Transient Mark mode, you must type
193 @kbd{C-u C-x u} if you want it to operate on the region.
194 @xref{Undo}. Other commands that act this way are identified
195 in their own documentation.
196 @end itemize
197
198 The highlighting of the region uses the @code{region} face; you can
199 customize the appearance of the highlighted region by changing this
200 face. @xref{Face Customization}.
201
202 @vindex highlight-nonselected-windows
203 When multiple windows show the same buffer, they can have different
204 regions, because they can have different values of point (though they
205 all share one common mark position). Ordinarily, only the selected
206 window highlights its region (@pxref{Windows}). However, if the
207 variable @code{highlight-nonselected-windows} is non-@code{nil}, then
208 each window highlights its own region (provided that Transient Mark mode
209 is enabled and the mark in the window's buffer is active).
210
211 When Transient Mark mode is not enabled, every command that sets the
212 mark also activates it, and nothing ever deactivates it.
213
214 @vindex mark-even-if-inactive
215 If the variable @code{mark-even-if-inactive} is non-@code{nil} in
216 Transient Mark mode, then commands can use the mark and the region
217 even when it is inactive. Region highlighting appears and disappears
218 just as it normally does in Transient Mark mode, but the mark doesn't
219 really go away when the highlighting disappears.
220
221 @cindex Zmacs mode
222 Transient Mark mode is also sometimes known as ``Zmacs mode''
223 because the Zmacs editor on the MIT Lisp Machine handled the mark in a
224 similar way.
225
226 @node Using Region
227 @section Operating on the Region
228
229 @cindex operations on a marked region
230 Once you have a region and the mark is active, here are some of the
231 ways you can operate on the region:
232
233 @itemize @bullet
234 @item
235 Kill it with @kbd{C-w} (@pxref{Killing}).
236 @item
237 Save it in a register with @kbd{C-x r s} (@pxref{Registers}).
238 @item
239 Save it in a buffer or a file (@pxref{Accumulating Text}).
240 @item
241 Convert case with @kbd{C-x C-l} or @kbd{C-x C-u} (@pxref{Case}).
242 @item
243 Indent it with @kbd{C-x @key{TAB}} or @kbd{C-M-\} (@pxref{Indentation}).
244 @item
245 Fill it as text with @kbd{M-x fill-region} (@pxref{Filling}).
246 @item
247 Print hardcopy with @kbd{M-x print-region} (@pxref{Hardcopy}).
248 @item
249 Evaluate it as Lisp code with @kbd{M-x eval-region} (@pxref{Lisp Eval}).
250 @end itemize
251
252 Most commands that operate on the text in the region have the word
253 @code{region} in their names.
254
255 @node Marking Objects
256 @section Commands to Mark Textual Objects
257
258 @cindex marking sections of text
259 Here are the commands for placing point and the mark around a textual
260 object such as a word, list, paragraph or page.
261
262 @table @kbd
263 @item M-@@
264 Set mark after end of next word (@code{mark-word}). This command and
265 the following one do not move point.
266 @item C-M-@@
267 Set mark after end of following balanced expression (@code{mark-sexp}).
268 @item M-h
269 Put region around current paragraph (@code{mark-paragraph}).
270 @item C-M-h
271 Put region around current defun (@code{mark-defun}).
272 @item C-x h
273 Put region around the entire buffer (@code{mark-whole-buffer}).
274 @item C-x C-p
275 Put region around current page (@code{mark-page}).
276 @end table
277
278 @kbd{M-@@} (@code{mark-word}) puts the mark at the end of the next
279 word, while @kbd{C-M-@@} (@code{mark-sexp}) puts it at the end of the
280 next balanced expression (@pxref{Expressions}). These commands handle
281 arguments just like @kbd{M-f} and @kbd{C-M-f}.
282
283 @kindex C-x h
284 @findex mark-whole-buffer
285 Other commands set both point and mark, to delimit an object in the
286 buffer. For example, @kbd{M-h} (@code{mark-paragraph}) moves point to
287 the beginning of the paragraph that surrounds or follows point, and
288 puts the mark at the end of that paragraph (@pxref{Paragraphs}). It
289 prepares the region so you can indent, case-convert, or kill a whole
290 paragraph. With prefix argument, if the argument's value is positive,
291 @kbd{M-h} marks that many paragraphs starting with the one surrounding
292 point. If the prefix argument is @minus{}@var{n}, @kbd{M-h} also
293 marks @var{n} paragraphs, running back form the one surrounding point.
294 In that last case, point moves forward to the end of that paragraph,
295 and the mark goes at the start of the region.
296
297 @kbd{C-M-h} (@code{mark-defun}) similarly puts point before, and the
298 mark after, the current (or following) major top-level definition, or
299 defun (@pxref{Moving by Defuns}). (Currently it only marks one
300 defun.) @kbd{C-x C-p} (@code{mark-page}) puts point before the
301 current page, and mark at the end (@pxref{Pages}). The mark goes
302 after the terminating page delimiter (to include it in the region),
303 while point goes after the preceding page delimiter (to exclude it).
304 A numeric argument specifies a later page (if positive) or an earlier
305 page (if negative) instead of the current page.
306
307 Finally, @kbd{C-x h} (@code{mark-whole-buffer}) sets up the entire
308 buffer as the region, by putting point at the beginning and the mark at
309 the end.
310
311 In Transient Mark mode, all of these commands activate the mark.
312
313 @node Mark Ring
314 @section The Mark Ring
315
316 @kindex C-u C-SPC
317 @cindex mark ring
318 @kindex C-u C-@@
319 Aside from delimiting the region, the mark is also useful for
320 remembering a spot that you may want to go back to. To make this
321 feature more useful, each buffer remembers 16 previous locations of the
322 mark, in the @dfn{mark ring}. Commands that set the mark also push the
323 old mark onto this ring. To return to a marked location, use @kbd{C-u
324 C-@key{SPC}} (or @kbd{C-u C-@@}); this is the command
325 @code{set-mark-command} given a numeric argument. It moves point to
326 where the mark was, and restores the mark from the ring of former
327 marks. Thus, repeated use of this command moves point to all of the old
328 marks on the ring, one by one. The mark positions you move through in
329 this way are not lost; they go to the end of the ring.
330
331 Each buffer has its own mark ring. All editing commands use the current
332 buffer's mark ring. In particular, @kbd{C-u C-@key{SPC}} always stays in
333 the same buffer.
334
335 Many commands that can move long distances, such as @kbd{M-<}
336 (@code{beginning-of-buffer}), start by setting the mark and saving the
337 old mark on the mark ring. This is to make it easier for you to move
338 back later. Searches set the mark if they move point. You can tell
339 when a command sets the mark because it displays @samp{Mark set} in the
340 echo area.
341
342 If you want to move back to the same place over and over, the mark
343 ring may not be convenient enough. If so, you can record the position
344 in a register for later retrieval (@pxref{RegPos,, Saving Positions in
345 Registers}).
346
347 @vindex mark-ring-max
348 The variable @code{mark-ring-max} specifies the maximum number of
349 entries to keep in the mark ring. If that many entries exist and
350 another one is pushed, the earliest one in the list is discarded. Repeating
351 @kbd{C-u C-@key{SPC}} cycles through the positions currently in the
352 ring.
353
354 @vindex mark-ring
355 The variable @code{mark-ring} holds the mark ring itself, as a list of
356 marker objects, with the most recent first. This variable is local in
357 every buffer.
358
359 @node Global Mark Ring
360 @section The Global Mark Ring
361 @cindex global mark ring
362
363 In addition to the ordinary mark ring that belongs to each buffer,
364 Emacs has a single @dfn{global mark ring}. It records a sequence of
365 buffers in which you have recently set the mark, so you can go back
366 to those buffers.
367
368 Setting the mark always makes an entry on the current buffer's mark
369 ring. If you have switched buffers since the previous mark setting, the
370 new mark position makes an entry on the global mark ring also. The
371 result is that the global mark ring records a sequence of buffers that
372 you have been in, and, for each buffer, a place where you set the mark.
373
374 @kindex C-x C-@key{SPC}
375 @findex pop-global-mark
376 The command @kbd{C-x C-@key{SPC}} (@code{pop-global-mark}) jumps to
377 the buffer and position of the latest entry in the global ring. It also
378 rotates the ring, so that successive uses of @kbd{C-x C-@key{SPC}} take
379 you to earlier and earlier buffers.
380