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
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}.
34 * Setting Mark:: Commands to set the mark.
35 * Transient Mark:: How to make Emacs highlight the region--
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.
44 @section Setting the Mark
46 Here are some commands for setting the mark:
51 Set the mark where point is (@code{set-mark-command}).
55 Interchange mark and point (@code{exchange-point-and-mark}).
57 Set point and the mark around the text you drag across.
59 Set the mark where point is, then move point to where you click
60 (@code{mouse-save-then-kill}).
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
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}.
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
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 For more facilities that allow you to go to previously set marks, see
109 There is no such character as @kbd{C-@key{SPC}} in ASCII; when you
110 type @key{SPC} while holding down @key{CTRL}, what you get on most
111 ordinary terminals is the character @kbd{C-@@}. This key is actually
112 bound to @code{set-mark-command}. But unless you are unlucky enough to
113 have a terminal where typing @kbd{C-@key{SPC}} does not produce
114 @kbd{C-@@}, you might as well think of this character as
115 @kbd{C-@key{SPC}}. Under X, @kbd{C-@key{SPC}} is actually a distinct
116 character, but its binding is still @code{set-mark-command}.
119 @section Transient Mark Mode
120 @cindex mode, Transient Mark
121 @cindex Transient Mark mode
122 @cindex highlighting region
123 @cindex region highlighting
125 On a terminal that supports colors, Emacs can highlight the current
126 region. But normally it does not. Why not?
128 Highlighting the region whenever it exists would not be desirable in
129 Emacs, because once you have set a mark, there is @emph{always} a
130 region (in that buffer). And highlighting the region all the time
131 would be a nuisance. So normally Emacs highlights the region only
132 immediately after you have selected one with the mouse.
134 You can turn on region highlighting by enabling Transient Mark mode.
135 This is a more rigid mode of operation in which the region ``lasts''
136 only temporarily, so you must set up a region for each command that uses
137 one. In Transient Mark mode, most of the time there is no region;
138 therefore, highlighting the region when it exists is useful and
141 @findex transient-mark-mode
142 To enable Transient Mark mode, type @kbd{M-x transient-mark-mode}.
143 This command toggles the mode, so you can repeat the command to turn off
146 Here are the details of Transient Mark mode:
150 To set the mark, type @kbd{C-@key{SPC}} (@code{set-mark-command}).
151 This makes the mark active; as you move point, you will see the region
152 highlighting grow and shrink.
155 The mouse commands for specifying the mark also make it active. So do
156 keyboard commands whose purpose is to specify a region, including
157 @kbd{M-@@}, @kbd{C-M-@@}, @kbd{M-h}, @kbd{C-M-h}, @kbd{C-x C-p}, and
161 When the mark is active, you can execute commands that operate on the
162 region, such as killing, indenting, or writing to a file.
165 Any change to the buffer, such as inserting or deleting a character,
166 deactivates the mark. This means any subsequent command that operates
167 on a region will get an error and refuse to operate. You can make the
168 region active again by typing @kbd{C-x C-x}.
171 Commands like @kbd{M->} and @kbd{C-s} that ``leave the mark behind'' in
172 addition to some other primary purpose do not activate the new mark.
173 You can activate the new region by executing @kbd{C-x C-x}
174 (@code{exchange-point-and-mark}).
177 @kbd{C-s} when the mark is active does not alter the mark.
180 Quitting with @kbd{C-g} deactivates the mark.
183 Some commands operate on the region whenever it is active. For
184 instance, @kbd{C-x u} in Transient Mark mode operates on the region
185 when there is a region. Outside Transient Mark mode, you must type
186 @kbd{C-u C-x u} if you want it to operate on the region.
187 @xref{Undo}. Other commands that act this way are identified
188 in their own documentation.
191 Highlighting of the region uses the @code{region} face; you can
192 customize how the region is highlighted by changing this face.
193 @xref{Face Customization}.
195 @vindex highlight-nonselected-windows
196 When multiple windows show the same buffer, they can have different
197 regions, because they can have different values of point (though they
198 all share one common mark position). Ordinarily, only the selected
199 window highlights its region (@pxref{Windows}). However, if the
200 variable @code{highlight-nonselected-windows} is non-@code{nil}, then
201 each window highlights its own region (provided that Transient Mark mode
202 is enabled and the window's buffer's mark is active).
204 When Transient Mark mode is not enabled, every command that sets the
205 mark also activates it, and nothing ever deactivates it.
207 @vindex mark-even-if-inactive
208 If the variable @code{mark-even-if-inactive} is non-@code{nil} in
209 Transient Mark mode, then commands can use the mark and the region
210 even when it is inactive. Region highlighting appears and disappears
211 just as it normally does in Transient Mark mode, but the mark doesn't
212 really go away when the highlighting disappears.
215 Transient Mark mode is also sometimes known as ``Zmacs mode''
216 because the Zmacs editor on the MIT Lisp Machine handled the mark in a
220 @section Operating on the Region
222 @cindex operations on a marked region
223 Once you have a region and the mark is active, here are some of the
224 ways you can operate on the region:
228 Kill it with @kbd{C-w} (@pxref{Killing}).
230 Save it in a register with @kbd{C-x r s} (@pxref{Registers}).
232 Save it in a buffer or a file (@pxref{Accumulating Text}).
234 Convert case with @kbd{C-x C-l} or @kbd{C-x C-u} (@pxref{Case}).
236 Indent it with @kbd{C-x @key{TAB}} or @kbd{C-M-\} (@pxref{Indentation}).
238 Fill it as text with @kbd{M-x fill-region} (@pxref{Filling}).
240 Print hardcopy with @kbd{M-x print-region} (@pxref{Hardcopy}).
242 Evaluate it as Lisp code with @kbd{M-x eval-region} (@pxref{Lisp Eval}).
245 Most commands that operate on the text in the
246 region have the word @code{region} in their names.
248 @node Marking Objects
249 @section Commands to Mark Textual Objects
251 @cindex marking sections of text
252 Here are the commands for placing point and the mark around a textual
253 object such as a word, list, paragraph or page.
257 Set mark after end of next word (@code{mark-word}). This command and
258 the following one do not move point.
260 Set mark after end of next Lisp expression (@code{mark-sexp}).
262 Put region around current paragraph (@code{mark-paragraph}).
264 Put region around current Lisp defun (@code{mark-defun}).
266 Put region around entire buffer (@code{mark-whole-buffer}).
268 Put region around current page (@code{mark-page}).
271 @kbd{M-@@} (@code{mark-word}) puts the mark at the end of the next word,
272 while @kbd{C-M-@@} (@code{mark-sexp}) puts it at the end of the next Lisp
273 expression. These commands handle arguments just like @kbd{M-f} and
277 @findex mark-whole-buffer
278 Other commands set both point and mark, to delimit an object in the
279 buffer. For example, @kbd{M-h} (@code{mark-paragraph}) moves point to
280 the beginning of the paragraph that surrounds or follows point, and puts
281 the mark at the end of that paragraph (@pxref{Paragraphs}). It prepares
282 the region so you can indent, case-convert, or kill a whole paragraph.
284 @kbd{C-M-h} (@code{mark-defun}) similarly puts point before and the
285 mark after the current or following defun (@pxref{Defuns}). @kbd{C-x
286 C-p} (@code{mark-page}) puts point before the current page, and mark at
287 the end (@pxref{Pages}). The mark goes after the terminating page
288 delimiter (to include it), while point goes after the preceding page
289 delimiter (to exclude it). A numeric argument specifies a later page
290 (if positive) or an earlier page (if negative) instead of the current
293 Finally, @kbd{C-x h} (@code{mark-whole-buffer}) sets up the entire
294 buffer as the region, by putting point at the beginning and the mark at
297 In Transient Mark mode, all of these commands activate the mark.
300 @section The Mark Ring
305 Aside from delimiting the region, the mark is also useful for
306 remembering a spot that you may want to go back to. To make this
307 feature more useful, each buffer remembers 16 previous locations of the
308 mark, in the @dfn{mark ring}. Commands that set the mark also push the
309 old mark onto this ring. To return to a marked location, use @kbd{C-u
310 C-@key{SPC}} (or @kbd{C-u C-@@}); this is the command
311 @code{set-mark-command} given a numeric argument. It moves point to
312 where the mark was, and restores the mark from the ring of former
313 marks. Thus, repeated use of this command moves point to all of the old
314 marks on the ring, one by one. The mark positions you move through in
315 this way are not lost; they go to the end of the ring.
317 Each buffer has its own mark ring. All editing commands use the current
318 buffer's mark ring. In particular, @kbd{C-u C-@key{SPC}} always stays in
321 Many commands that can move long distances, such as @kbd{M-<}
322 (@code{beginning-of-buffer}), start by setting the mark and saving the
323 old mark on the mark ring. This is to make it easier for you to move
324 back later. Searches set the mark if they move point. You can tell
325 when a command sets the mark because it displays @samp{Mark Set} in the
328 If you want to move back to the same place over and over, the mark
329 ring may not be convenient enough. If so, you can record the position
330 in a register for later retrieval (@pxref{RegPos}).
332 @vindex mark-ring-max
333 The variable @code{mark-ring-max} specifies the maximum number of
334 entries to keep in the mark ring. If that many entries exist and
335 another one is pushed, the last one in the list is discarded. Repeating
336 @kbd{C-u C-@key{SPC}} cycles through the positions currently in the
340 The variable @code{mark-ring} holds the mark ring itself, as a list of
341 marker objects, with the most recent first. This variable is local in
344 @node Global Mark Ring
345 @section The Global Mark Ring
346 @cindex global mark ring
348 In addition to the ordinary mark ring that belongs to each buffer,
349 Emacs has a single @dfn{global mark ring}. It records a sequence of
350 buffers in which you have recently set the mark, so you can go back
353 Setting the mark always makes an entry on the current buffer's mark
354 ring. If you have switched buffers since the previous mark setting, the
355 new mark position makes an entry on the global mark ring also. The
356 result is that the global mark ring records a sequence of buffers that
357 you have been in, and, for each buffer, a place where you set the mark.
359 @kindex C-x C-@key{SPC}
360 @findex pop-global-mark
361 The command @kbd{C-x C-@key{SPC}} (@code{pop-global-mark}) jumps to
362 the buffer and position of the latest entry in the global ring. It also
363 rotates the ring, so that successive uses of @kbd{C-x C-@key{SPC}} take
364 you to earlier and earlier buffers.