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. Emacs highlights the region whenever there is one, if you enable Transient Mark mode (see Transient Mark).

Certain Emacs commands set the mark; other editing commands do not affect it, so the mark remains where you set it last.  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 that was current earlier, its mark is at the same place as before.

The ends of the region are always point and the mark.  It doesn't matter which of them was put in its current place first, or 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, or set the mark in a new place, the region changes.

Many commands that insert text, such as C-y (yank) and M-x insert-buffer, position point and the mark at opposite ends of the inserted text, so that the region consists of the text just inserted.

Aside from delimiting the region, the mark is also useful for remembering a spot that you may want to go back to.  To make this feature more useful, each buffer remembers 16 previous locations of the mark in the mark ring.

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
     Interchange mark and point (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).

For example, suppose you wish to convert part of the buffer to upper case, using the C-x C-u (upcase-region) command, which operates on the text in the region.  You can first go to the beginning of the text to be capitalized, type C-SPC to put the mark there, move to the end, and then type C-x C-u.  Or, you can set the mark at the end of the text, move to the beginning, and then type C-x C-u.

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

There are two ways to set the mark with the mouse.  You can drag mouse button one across a range of text; that puts point where you release the mouse button, and sets the mark at the other end of that range.  Or you can click mouse button three, which sets the mark at point (like C-SPC) and then moves point (like Mouse-1).  Both of these methods copy the region into the kill ring in addition to setting the mark; that gives behavior consistent with other window-driven applications, but if you don't want to modify the kill ring, you must use keyboard commands to set the mark.  See Mouse Commands.

Ordinary terminals have only one cursor, so there is no way for Emacs to show you 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 see where the mark is with the command C-x C-x (exchange-point-and-mark) which puts the mark where point was and point where the mark was.  The extent of the region is unchanged, but the cursor and point are now at the previous position of the mark.  In Transient Mark mode, this command also reactivates the mark.

C-x C-x is also 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.

For more facilities that allow you to go to previously set marks, see Mark Ring.

There is no such character as C-SPC in ASCII; when you type SPC while holding down CTRL, what you get on most ordinary terminals is the character C-@.  This key is actually bound to set-mark-command.  But unless you are unlucky enough to have a terminal where typing C-SPC does not produce C-@, you might as well think of this character as C-SPC.  Under X, C-SPC is actually a distinct character, but its binding is still set-mark-command.

Transient Mark Mode
===================

On a terminal that supports colors, Emacs has the ability to highlight the current region.  But normally it does not.  Why not?

Once you have set the mark in a buffer, there is always a region in that buffer.  This is because every command that sets the mark also activates it, and nothing ever deactivates it.  Highlighting the region all the time would be a nuisance.  So normally Emacs highlights the region only immediately after you have selected one with the mouse.

If you want region highlighting, you can use Transient Mark mode. This is a more rigid mode of operation in which the region always ``lasts'' only until you use it; you explicitly must set up a region for each command that uses one.  In Transient Mark mode, most of the time there is no region; therefore, highlighting the region when it exists is useful and not annoying.  When Transient Mark mode is enabled, Emacs always highlights the region whenever there is a region.

To enable Transient Mark mode, type M-x transient-mark-mode. This command toggles the mode; you can use the same command to turn the mode off again.

Here are the details of Transient Mark mode:

  * To set the mark, type C-SPC (set-mark-command). This makes the mark active and thus begins highlighting of the region. As you move point, you will see the highlighted region grow and shrink.

  * The mouse commands for specifying the mark also make it active.  So do keyboard commands whose purpose is to specify a region, including M-@, C-M-@, M-h, C-M-h, C-x C-p, and C-x h.

  * You can tell that the mark is active because the region is highlighted.

  * When the mark is active, you can execute commands that operate on the region, such as killing, indenting, or writing to a file.

  * Any change to the buffer, such as inserting or deleting a character, deactivates the mark.  This means any subsequent command that operates on a region will get an error and refuse to operate.  You can make the region active again by typing C-x C-x.

  * Quitting with C-g deactivates the mark.

  * Commands like M-> and C-s, that ``leave the mark behind'' in addition to some other primary purpose, do not activate the new mark. You can activate the new region by executing C-x C-x (exchange-point-and-mark).

  * C-s when the mark is active does not alter the mark.

  * Some commands operate on the region if a region is active.  For instance, C-x u in Transient Mark mode operates on the region, when there is a region.  (Outside Transient Mark mode, you must type C-u C-x u if you want it to operate on the region.) See Undo.  Other commands that act this way are identified in their own documentation.

The highlighting of the region uses the region face; you can customize the appearance of the highlighted region by changing this face.  See Face Customization.

When multiple windows show the same buffer, they can have different regions, because they can have different values of point (though they all share one common mark position).  Ordinarily, only the selected window highlights its region (see Windows).  However, if the variable highlight-nonselected-windows is non-nil, then each window highlights its own region (provided that Transient Mark mode is enabled and the mark in the window's buffer is active).

If the variable mark-even-if-inactive is non-nil in Transient Mark mode, then commands can use the mark and the region even when it is inactive.  Region highlighting appears and disappears just as it normally does in Transient Mark mode, but the mark doesn't really go away when the highlighting disappears, so you can still use region commands.

Transient Mark mode is also sometimes known as ``Zmacs mode'' because the Zmacs editor on the MIT Lisp Machine handled the mark in a similar way.

Using Transient Mark Mode Momentarily
=====================================

If you don't like Transient Mark mode in general, you might still want to use it once in a while.  To do this, type C-SPC C-SPC or C-u C-x C-x.  These commands set or activate the mark, and enable Transient Mark mode only until the mark is deactivated.

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.)

One of the secondary features of Transient Mark mode is that certain commands operate on the region when there is an active region.  If you don't use Transient Mark mode, the region once set never becomes inactive, so there is no way these commands to make such a distinction.  Enabling Transient Mark mode momentarily gives you a way to use these commands on the region.

The other way momentarily use of Transient Mark mode is useful is that it highlights the region for the time being.

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

Once you have a region and the mark is active, here are some of the ways you can operate on the region:

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

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

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

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

  * 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 Hardcopy).

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

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

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
     Put region around current paragraph (mark-paragraph).

C-M-h
     Put region around current defun (mark-defun).

C-x h
     Put region around the entire buffer (mark-whole-buffer).

C-x C-p
     Put region around current page (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.  If you repeat these commands, the region is extended.  For example, you can type either C-u 2 M-@ or M-@ M-@ to mark the next two words.

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 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.  The M-h command also supports the extension of the region, similar to M-@ and C-M-@.

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).  (Currently it only marks one defun, but repeating it marks more defuns, like for M-@.)  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.

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 Transient Mark mode, all of these commands activate the mark.

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

Aside from delimiting the region, the mark is also useful for remembering a spot that you may want to go back to.  To make this feature more useful, 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.  To return to a marked location, use C-u C-SPC (or C-u C-@); this is the command set-mark-command given a numeric argument.  It moves point to where the mark was, and restores the mark from the ring of former marks.

If you repeat the character C-SPC, after typing C-u C-SPC, each repetition moves point to a previous mark position from the ring.  The mark positions you move through in this way are not lost; they go to the end of the ring.

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.

Many commands that can move long distances, such as M-< (beginning-of-buffer), start by setting the mark and saving the old mark on the mark ring.  This is to make it easier for you to move back later.  Searches set the mark if they move point.  You can tell when a command sets the mark because it displays Mark set in the echo area.

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 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.

The variable mark-ring holds the mark ring itself, as a list of marker objects, with the most recent first.  This variable is local in every buffer.

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.