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