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