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