(diff-kill-junk): New interactive function.
[emacs.git] / man / mark.texi
blobba51aae3170f641295c94ed2422ab08d61f16381
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   Emacs can highlight the current region on a terminal which supports
123 colors.  But normally it does not.  Why not?
125   Highlighting the region doesn't work well ordinarily in Emacs, because
126 once you have set a mark, there is @emph{always} a region (in that
127 buffer).  And highlighting the region all the time would be a nuisance.
129   You can turn on region highlighting by enabling Transient Mark mode.
130 This is a more rigid mode of operation in which the region ``lasts''
131 only temporarily, so you must set up a region for each command that uses
132 one.  In Transient Mark mode, most of the time there is no region;
133 therefore, highlighting the region when it exists is convenient.
135 @findex transient-mark-mode
136   To enable Transient Mark mode, type @kbd{M-x transient-mark-mode}.
137 This command toggles the mode, so you can repeat the command to turn off
138 the mode.
140   Here are the details of Transient Mark mode:
142 @itemize @bullet
143 @item
144 To set the mark, type @kbd{C-@key{SPC}} (@code{set-mark-command}).
145 This makes the mark active; as you move point, you will see the region
146 highlighting grow and shrink.
148 @item 
149 The mouse commands for specifying the mark also make it active.  So do
150 keyboard commands whose purpose is to specify a region, including
151 @kbd{M-@@}, @kbd{C-M-@@}, @kbd{M-h}, @kbd{C-M-h}, @kbd{C-x C-p}, and
152 @kbd{C-x h}.
154 @item
155 When the mark is active, you can execute commands that operate on the
156 region, such as killing, indenting, or writing to a file.
158 @item
159 Any change to the buffer, such as inserting or deleting a character,
160 deactivates the mark.  This means any subsequent command that operates
161 on a region will get an error and refuse to operate.  You can make the
162 region active again by typing @kbd{C-x C-x}.
164 @item
165 Commands like @kbd{M->} and @kbd{C-s} that ``leave the mark behind'' in
166 addition to some other primary purpose do not activate the new mark.
167 You can activate the new region by executing @kbd{C-x C-x}
168 (@code{exchange-point-and-mark}).
170 @item
171 @kbd{C-s} when the mark is active does not alter the mark.
173 @item
174 Quitting with @kbd{C-g} deactivates the mark.
175 @end itemize
177   Highlighting of the region uses the @code{region} face; you can
178 customize how the region is highlighted by changing this face.
179 @xref{Face Customization}.
181 @vindex highlight-nonselected-windows
182   When multiple windows show the same buffer, they can have different
183 regions, because they can have different values of point (though they
184 all share one common mark position).  Ordinarily, only the selected
185 window highlights its region (@pxref{Windows}).  However, if the
186 variable @code{highlight-nonselected-windows} is non-@code{nil}, then
187 each window highlights its own region (provided that Transient Mark mode
188 is enabled and the window's buffer's mark is active).
190   When Transient Mark mode is not enabled, every command that sets the
191 mark also activates it, and nothing ever deactivates it.
193 @vindex mark-even-if-inactive
194   If the variable @code{mark-even-if-inactive} is non-@code{nil} in
195 Transient Mark mode, then commands can use the mark and the region
196 even when it is inactive.  Region highlighting appears and disappears 
197 just as it normally does in Transient Mark mode, but the mark doesn't
198 really go away when the highlighting disappears.
200 @cindex Zmacs mode
201   Transient Mark mode is also sometimes known as ``Zmacs mode''
202 because the Zmacs editor on the MIT Lisp Machine handled the mark in a
203 similar way.
205 @node Using Region
206 @section Operating on the Region
208 @cindex operations on a marked region
209   Once you have a region and the mark is active, here are some of the
210 ways you can operate on the region:
212 @itemize @bullet
213 @item
214 Kill it with @kbd{C-w} (@pxref{Killing}).
215 @item
216 Save it in a register with @kbd{C-x r s} (@pxref{Registers}).
217 @item
218 Save it in a buffer or a file (@pxref{Accumulating Text}).
219 @item
220 Convert case with @kbd{C-x C-l} or @kbd{C-x C-u} (@pxref{Case}).
221 @item
222 Indent it with @kbd{C-x @key{TAB}} or @kbd{C-M-\} (@pxref{Indentation}).
223 @item
224 Fill it as text with @kbd{M-x fill-region} (@pxref{Filling}).
225 @item
226 Print hardcopy with @kbd{M-x print-region} (@pxref{Hardcopy}).
227 @item
228 Evaluate it as Lisp code with @kbd{M-x eval-region} (@pxref{Lisp Eval}).
229 @end itemize
231   Most commands that operate on the text in the
232 region have the word @code{region} in their names.
234 @node Marking Objects
235 @section Commands to Mark Textual Objects
237 @cindex marking sections of text
238   Here are the commands for placing point and the mark around a textual
239 object such as a word, list, paragraph or page.
241 @table @kbd
242 @item M-@@
243 Set mark after end of next word (@code{mark-word}).  This command and
244 the following one do not move point.
245 @item C-M-@@
246 Set mark after end of next Lisp expression (@code{mark-sexp}).
247 @item M-h
248 Put region around current paragraph (@code{mark-paragraph}).
249 @item C-M-h
250 Put region around current Lisp defun (@code{mark-defun}).
251 @item C-x h
252 Put region around entire buffer (@code{mark-whole-buffer}).
253 @item C-x C-p
254 Put region around current page (@code{mark-page}).
255 @end table
257 @kbd{M-@@} (@code{mark-word}) puts the mark at the end of the next word,
258 while @kbd{C-M-@@} (@code{mark-sexp}) puts it at the end of the next Lisp
259 expression.  These commands handle arguments just like @kbd{M-f} and
260 @kbd{C-M-f}.
262 @kindex C-x h
263 @findex mark-whole-buffer
264    Other commands set both point and mark, to delimit an object in the
265 buffer.  For example, @kbd{M-h} (@code{mark-paragraph}) moves point to
266 the beginning of the paragraph that surrounds or follows point, and puts
267 the mark at the end of that paragraph (@pxref{Paragraphs}).  It prepares
268 the region so you can indent, case-convert, or kill a whole paragraph.
270   @kbd{C-M-h} (@code{mark-defun}) similarly puts point before and the
271 mark after the current or following defun (@pxref{Defuns}).  @kbd{C-x
272 C-p} (@code{mark-page}) puts point before the current page, and mark at
273 the end (@pxref{Pages}).  The mark goes after the terminating page
274 delimiter (to include it), while point goes after the preceding page
275 delimiter (to exclude it).  A numeric argument specifies a later page
276 (if positive) or an earlier page (if negative) instead of the current
277 page.
279   Finally, @kbd{C-x h} (@code{mark-whole-buffer}) sets up the entire
280 buffer as the region, by putting point at the beginning and the mark at
281 the end.
283   In Transient Mark mode, all of these commands activate the mark.
285 @node Mark Ring
286 @section The Mark Ring
288 @kindex C-u C-SPC
289 @cindex mark ring
290 @kindex C-u C-@@
291   Aside from delimiting the region, the mark is also useful for
292 remembering a spot that you may want to go back to.  To make this
293 feature more useful, each buffer remembers 16 previous locations of the
294 mark, in the @dfn{mark ring}.  Commands that set the mark also push the
295 old mark onto this ring.  To return to a marked location, use @kbd{C-u
296 C-@key{SPC}} (or @kbd{C-u C-@@}); this is the command
297 @code{set-mark-command} given a numeric argument.  It moves point to
298 where the mark was, and restores the mark from the ring of former
299 marks.  Thus, repeated use of this command moves point to all of the old
300 marks on the ring, one by one.  The mark positions you move through in
301 this way are not lost; they go to the end of the ring.
303   Each buffer has its own mark ring.  All editing commands use the current
304 buffer's mark ring.  In particular, @kbd{C-u C-@key{SPC}} always stays in
305 the same buffer.
307   Many commands that can move long distances, such as @kbd{M-<}
308 (@code{beginning-of-buffer}), start by setting the mark and saving the
309 old mark on the mark ring.  This is to make it easier for you to move
310 back later.  Searches set the mark if they move point.  You can tell
311 when a command sets the mark because it displays @samp{Mark Set} in the
312 echo area.
314   If you want to move back to the same place over and over, the mark
315 ring may not be convenient enough.  If so, you can record the position
316 in a register for later retrieval (@pxref{RegPos}).
318 @vindex mark-ring-max
319   The variable @code{mark-ring-max} specifies the maximum number of
320 entries to keep in the mark ring.  If that many entries exist and
321 another one is pushed, the last one in the list is discarded.  Repeating
322 @kbd{C-u C-@key{SPC}} cycles through the positions currently in the
323 ring.
325 @vindex mark-ring
326   The variable @code{mark-ring} holds the mark ring itself, as a list of
327 marker objects, with the most recent first.  This variable is local in
328 every buffer.
330 @node Global Mark Ring
331 @section The Global Mark Ring
332 @cindex global mark ring
334   In addition to the ordinary mark ring that belongs to each buffer,
335 Emacs has a single @dfn{global mark ring}.  It records a sequence of
336 buffers in which you have recently set the mark, so you can go back
337 to those buffers.
339   Setting the mark always makes an entry on the current buffer's mark
340 ring.  If you have switched buffers since the previous mark setting, the
341 new mark position makes an entry on the global mark ring also.  The
342 result is that the global mark ring records a sequence of buffers that
343 you have been in, and, for each buffer, a place where you set the mark.
345 @kindex C-x C-@key{SPC}
346 @findex pop-global-mark
347   The command @kbd{C-x C-@key{SPC}} (@code{pop-global-mark}) jumps to
348 the buffer and position of the latest entry in the global ring.  It also
349 rotates the ring, so that successive uses of @kbd{C-x C-@key{SPC}} take
350 you to earlier and earlier buffers.