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