The Mark and the Region
***********************

Many Emacs commands operate on an arbitrary contiguous part of the current buffer.  To specify the text for such a command to operate on, you set the mark at one end of it, and move point to the other end.  The text between point and the mark is called the region.

The region always extends between point and the mark, no matter which one comes earlier in the text---the region starts from point or the mark (whichever comes first), and ends at point or the mark (whichever comes last).  Every time you move point, the region changes.  When a region is active, Emacs highlights it using the region face.  You can customize the appearance of the highlighting by changing this face (see Face Customization).

The region persists only until you use it.  The mark is automatically deactivated after certain non-motion commands, including any command that changes the text in the buffer.  You can also explicitly deactivate the mark at any time, by typing C-g (see Quitting).

Each Emacs buffer has its own mark, and setting the mark in one buffer has no effect on other buffers' marks.  When you return to a buffer with an active mark, the mark is at the same place as before. When multiple windows show the same buffer, they can have different values of point, and thus different regions (though they all share one common mark position).  See Windows.  Ordinarily, only the selected window highlights its region, if a region exists; however, if the variable highlight-nonselected-windows is non-nil, each window highlights its own region.

If Delete Selection mode is enabled, some commands delete the region when used while the mark is active.  See Mouse Commands.

Setting the Mark
================

Here are some commands for setting the mark:

C-SPC
     Set the mark where point is (set-mark-command).

C-@
     The same.

C-x C-x
     Set the mark where point was, and move point where the mark was (exchange-point-and-mark).

Drag-Mouse-1
     Set point and the mark around the text you drag across.

Mouse-3
     Set the mark where point is, then move point to where you click (mouse-save-then-kill).

The most common way to set the mark is with C-SPC (set-mark-command).  This sets the mark where point is.  You can then move point away, leaving the mark behind.

For example, suppose you wish to convert part of the buffer to upper case.  To accomplish this, first go to the beginning of the text to be capitalized, type C-SPC, and move point until the desired portion of text is highlighted.  You can now type C-x C-u (upcase-region), which converts the text in the region to upper case.  This also automatically deactivates the mark.  (Alternatively, you could set the mark at the end, move to the beginning, and do C-x C-u.)

The command C-x C-x (exchange-point-and-mark) puts the mark where point was and moves point where the mark was, keeping the region unchanged.  If no mark is active, Emacs reactivates the mark at the position where it was last set.  C-x C-x is useful when you are satisfied with the position of point but want to move the other end of the region (where the mark is); do C-x C-x to put point at that end of the region, and then move it.  Using C-x C-x a second time, if necessary, puts the mark at the new position with point back at its original position.

You can also set the mark with the mouse.  In fact, there are two ways to do this.  You can press the left mouse button (down-mouse-1) and drag the mouse across a range of text while holding down the button; this sets the mark where you first pressed the mouse button, and point where you release it.  Alternatively, you can click the right mouse button (mouse-3), which sets the mark at point and then moves point to where you clicked.

Using the mouse to mark a region also copies the region into the kill ring (see Kill Ring).  If you don't want to modify the kill ring, you must use keyboard commands to set the mark.  See Mouse Commands.

There is no such character as C-SPC in ASCII; when you type SPC while holding down CTRL on a text terminal, what you get is the character C-@.  This key is also bound to set-mark-command---so unless you are unlucky enough to have a text terminal where typing C-SPC does not produce C-@, you might as well think of this character as C-SPC.

Whenever the mark is active, you can deactivate it by typing C-g (see Quitting).  The mark is also automatically deactivated after certain non-motion commands.

Commands to Mark Textual Objects
================================

Here are the commands for placing point and the mark around a textual object such as a word, list, paragraph or page:

M-@
     Set mark after end of next word (mark-word).  This command and the following one do not move point.

C-M-@
     Set mark after end of following balanced expression (mark-sexp).

M-h
     Move point to the beginning of the current paragraph, and set mark at the end (mark-paragraph).

C-M-h
     Move point to the beginning of the current defun, and set mark at the end (mark-defun).

C-x h
     Move point to the beginning of the buffer, and set mark at the end (mark-whole-buffer).

C-x C-p
     Move point to the beginning of the current page, and set mark at the end (mark-page).

M-@ (mark-word) puts the mark at the end of the next word, while C-M-@ (mark-sexp) puts it at the end of the next balanced expression (see Expressions).  These commands handle arguments just like M-f and C-M-f.

Other commands set both point and mark, to delimit an object in the buffer.  For example, M-h (mark-paragraph) moves point to the beginning of the paragraph that surrounds or follows point, and puts the mark at the end of that paragraph (see Paragraphs).  It prepares the region so you can indent, case-convert, or kill a whole paragraph.  With a prefix argument, if the argument's value is positive, M-h marks that many paragraphs starting with the one surrounding point.  If the prefix argument is -N, M-h also marks N paragraphs, running back form the one surrounding point. In that last case, point moves forward to the end of that paragraph, and the mark goes at the start of the region.

C-M-h (mark-defun) similarly puts point before, and the mark after, the current (or following) major top-level definition, or defun (see Moving by Defuns).  Repeating C-M-h extends the region to subsequent defuns.

C-x C-p (mark-page) puts point before the current page, and mark at the end (see Pages).  The mark goes after the terminating page delimiter (to include it in the region), while point goes after the preceding page delimiter (to exclude it).  A numeric argument specifies a later page (if positive) or an earlier page (if negative) instead of the current page.

While the mark is active, repeating the above commands extends the region accordingly.  For example, you can type either C-u 2 M-@ or M-@ M-@ to mark the next two words.  Similarly, repeating the M-h command extends the region to subsequent paragraphs.

Finally, C-x h (mark-whole-buffer) sets up the entire buffer as the region, by putting point at the beginning and the mark at the end.  (In some programs this is called ``select all.'')

Operating on the Region
=======================

Once you have a region, here are some of the ways you can operate on it:

  * Kill it with C-w (see Killing).

  * Copy it to the kill ring with M-w (see Yanking).

  * Convert case with C-x C-l or C-x C-u (see Case).

  * Undo changes within it using C-u C-/ (see Undo).

  * Replace text within it using M-% (see Query Replace).

  * Indent it with C-x TAB or C-M-\ (see Indentation).

  * Fill it as text with M-x fill-region (see Filling).

  * Print hardcopy with M-x print-region (see Printing).

  * Evaluate it as Lisp code with M-x eval-region (see Lisp Eval).

  * Save it in a register with C-x r s (see Registers).

  * Save it in a buffer or a file (see Accumulating Text).

Most commands that operate on the text in the region have the word region in their names.

Persistent Marks
================

By default, the region is highlighted whenever it exists, and it disappears once you use it or explicitly deactivate the mark.  This behavior is called Transient Mark mode (It is also sometimes called Zmacs mode, because the Zmacs editor on the MIT Lisp Machine handled the mark in a similar way.).

Turning off Transient Mark mode switches Emacs to an alternative mode of operation, which was the default prior to Emacs 23.  When Transient Mark mode is off, the mark is persistent: it is never deactivated, but can be set to different locations using commands such as C-SPC.  After the first time you set the mark in a buffer, there is always a region in that buffer.  Emacs will not highlight the region, because that would be a nuisance.  As an exception, the region is temporarily highlighted after it is set with the mouse.

To turn off Transient Mark mode, type M-x transient-mark-mode. This command toggles the mode; you can use the same command to turn Transient Mark mode on again.  You can also toggle Transient Mark mode using the menu bar: in the Options menu, use the Active Region Highlighting menu item.

Here are the details of how Emacs behaves when Transient Mark mode is off:

  * Emacs does not show where the mark is located---you have to remember. The usual solution to this problem is to set the mark and then use it soon, before you forget where it is.  Alternatively, you can check the location of the mark by using C-x C-x, which exchanges the positions of the point and the mark (see Setting Mark).

  * Many commands that insert text, such as C-y (yank), position point and the mark at opposite ends of the inserted text, so that the region consists of the text just inserted.  You can tell when a command sets the mark because it displays Mark set in the echo area.

  * Many commands that move point long distances, like M-< and C-s, first set the mark where point was.

  * Some commands that ordinarily operate on the region, if one exists, act instead on the entire buffer.  For instance, C-x u normally reverses changes within the region if one exists; when Transient Mark mode is off, it acts on the entire buffer.  However, you can type C-u C-x u to make it operate on the region.  See Undo.  Other commands that act this way are identified in their own documentation.

While Transient Mark mode is off, you can activate it temporarily using C-SPC C-SPC or C-u C-x C-x.

C-SPC C-SPC
     Set the mark at point (like plain C-SPC) and enable Transient Mark mode just once, until the mark is deactivated.  (This is not really a separate command; you are using the C-SPC command twice.)

C-u C-x C-x
     Activate the mark without changing it; enable Transient Mark mode just once, until the mark is deactivated.  (This is the C-x C-x command, exchange-point-and-mark, with a prefix argument.)

These commands set or activate the mark, and enable Transient Mark mode only until the mark is deactivated.  One reason you may want to use them is that some commands operate on the entire buffer instead of the region when Transient Mark mode is off.  Enabling Transient Mark mode momentarily gives you a way to use these commands on the region.

The Mark Ring
=============

Aside from delimiting the region, the mark is also useful for remembering spots that you may want to go back to.  Each buffer remembers 16 previous locations of the mark, in the mark ring. Commands that set the mark also push the old mark onto this ring.

C-SPC C-SPC
     Set the mark, pushing it onto the mark ring, without activating it.

C-u C-SPC
     Move point to where the mark was, and restore the mark from the ring of former marks.

The command C-SPC C-SPC is handy when you want to use the mark to remember a position to which you may want to return.  It pushes the current point onto the mark ring, without activating the mark (which would cause Emacs to highlight the region).  This is actually two consecutive invocations of C-SPC (set-mark-command); the first C-SPC sets the mark, and the second C-SPC deactivates it.  (When Transient Mark mode is off, C-SPC C-SPC instead activates Transient Mark mode temporarily.  See Persistent Mark.)

To return to a marked position, use set-mark-command with a prefix argument: C-u C-SPC.  This moves point to where the mark was, and deactivates the mark if it was active.  Each subsequent C-u C-SPC jumps to a prior position stored in the mark ring.  The positions you move through in this way are not lost; they go to the end of the ring.

If you set set-mark-command-repeat-pop to non-nil, then immediately after you type C-u C-SPC, you can type C-SPC instead of C-u C-SPC to cycle through the mark ring.  By default, set-mark-command-repeat-pop is nil.

Each buffer has its own mark ring.  All editing commands use the current buffer's mark ring.  In particular, C-u C-SPC always stays in the same buffer.

The variable mark-ring-max specifies the maximum number of entries to keep in the mark ring.  If that many entries exist and another one is pushed, the earliest one in the list is discarded.  Repeating C-u C-SPC cycles through the positions currently in the ring.

If the variable mark-even-if-inactive is nil, commands can only use the mark and the region when it is active.  This variable is non-nil by default.

If you want to move back to the same place over and over, the mark ring may not be convenient enough.  If so, you can record the position in a register for later retrieval (see Saving Positions in Registers).

The Global Mark Ring
====================

In addition to the ordinary mark ring that belongs to each buffer, Emacs has a single global mark ring.  It records a sequence of buffers in which you have recently set the mark, so you can go back to those buffers.

Setting the mark always makes an entry on the current buffer's mark ring.  If you have switched buffers since the previous mark setting, the new mark position makes an entry on the global mark ring also.  The result is that the global mark ring records a sequence of buffers that you have been in, and, for each buffer, a place where you set the mark.

The command C-x C-SPC (pop-global-mark) jumps to the buffer and position of the latest entry in the global ring.  It also rotates the ring, so that successive uses of C-x C-SPC take you to earlier and earlier buffers.