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; each time you move point, the region changes.

Setting the mark at a position in the text also activates it. When the mark is active, Emacs indicates the extent of the region by highlighting the text within it, using the region face (see Face Customization).  After certain non-motion commands, including any command that changes the text in the buffer, Emacs automatically deactivates the mark; this turns off the highlighting.  You can also explicitly deactivate the mark at any time, by typing C-g (see Quitting).

This default behavior of the mark is known as Transient Mark mode. Disabling Transient Mark mode switches Emacs to an alternative behavior, in which the mark is always active, which was the default prior to Emacs 23.  See Persistent Mark.

Setting the mark in one buffer has no effect on the marks in other buffers.  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, but they all share one common mark position.  See Windows. Ordinarily, only the selected window highlights its region; however, if the variable highlight-nonselected-windows is non-nil, each window highlights its own region.

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

Here are some commands for setting the mark:

C-SPC
     Set the mark at point, and activate it (set-mark-command).

C-@
     The same.

C-x C-x
     Set the mark at point, 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 at point, then move point to where you click (mouse-save-then-kill).

Shifted motion keys
     Set the mark at point if the mark is inactive, then move point.

The most common way to set the mark is with C-SPC (set-mark-command) (There is no C-SPC character in ASCII; usually, typing C-SPC on a text terminal gives the character C-@.  This key is also bound to set-mark-command, so unless you are unlucky enough to have a text terminal that behaves differently, you might as well think of C-@ as C-SPC.).  This sets the mark where point is, and activates it.  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, go to the beginning of the desired text, type C-SPC, and move point until the desired portion of text is highlighted.  Now type C-x C-u (upcase-region). This converts the text in the region to upper case, and then deactivates the mark.

The command C-x C-x (exchange-point-and-mark) exchanges the positions of point and the mark, keeping the region unchanged.  If the mark is inactive, Emacs first reactivates the mark wherever 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).  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.  If you press the left mouse button (down-mouse-1) and drag the mouse across a range of text while holding down this button, this sets the mark where you first pressed the mouse button and puts point where you release it. Alternatively, clicking the right mouse button (mouse-3) 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).  See Mouse Commands, for a more detailed description of these mouse commands.

Finally, you can set the mark by holding down the shift key while typing certain cursor motion commands (such as S-right, S-C-f, S-C-n, etc.)  This is referred to as shift-selection.  This sets the mark at point before moving point, but only if there is no active mark set via shift-selection. The mark set by mouse commands and by shift-selection behaves slightly differently from the usual mark: any subsequent unshifted cursor motion command deactivates it automatically.  For details, see Shift Selection.

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 does not move point.

C-M-@
     Set mark after end of following balanced expression (mark-sexp).  This does not move point.

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 C-p
     Move point to the beginning of the current page, and set mark at the end (mark-page).

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

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.

The other commands in the above list set both point and mark, so as to delimit an object in the buffer.  M-h (mark-paragraph) moves point to the beginning of the paragraph that surrounds or follows point, and sets the mark at the end of that paragraph (see Paragraphs).  As a special exception, repeated invocations of M-h extend the region to subsequent paragraphs.  This is convenient for indenting, case-converting, or killing entire paragraphs.

The M-h command accepts prefix arguments.  If the argument's value is positive, M-h marks that many paragraphs starting with the one surrounding point; therefore, C-u M-h is equivalent to M-h M-h M-h M-h.  If the prefix argument is -N, M-h marks N paragraphs running back from the one surrounding point; in this case, point moves forward to the end of that paragraph, and the mark goes at the start of the region.

Similarly, C-M-h (mark-defun) sets mark and point around major top-level definitions (see Moving by Defuns), and C-x C-p (mark-page) does the same for pages (see Pages).  These treat repeated invocations and prefix arguments similarly to mark-paragraph.

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.

Some commands have a default behavior when the mark is inactive, but operate on the text in the region if the mark is active.  For example, M-$ (ispell-word) normally checks the spelling of the word at point, but it checks the text in the region if the region is active (see Spelling).  Normally, such commands use their default behavior if the region is empty (i.e., if mark and point are at the same position).  If you want them to operate on the empty region, change the variable use-empty-active-region to t.

If you enable Delete Selection mode, a minor mode, then inserting text while the mark is active causes the selected text to be deleted first.  This also deactivates the mark.  Many graphical applications follow this convention, but Emacs does not.  To toggle Delete Selection mode on or off, type M-x delete-selection-mode. Another effect of this mode is that some keys, such as DEL and C-d, always kill the region if one exists.

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 wish 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.  Each time you set a mark, in any buffer, this is recorded in the global mark ring in addition to the current buffer's own mark ring.

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 buffers and mark positions.

Shift Selection
===============

If you hold down the shift key while typing a cursor motion command, this sets the mark before moving point, so that the region extends from the original position of point to its new position.  This feature, newly introduced in Emacs 23, is referred to as shift-selection.  It is similar to the way text is selected in other editors.

The mark set via shift-selection behaves a little differently from what we have described above.  Firstly, in addition to the usual ways of deactivating the mark (such as changing the buffer text or typing C-g), the mark is deactivated by any unshifted cursor motion command.  Secondly, any subsequent shifted cursor motion command avoids setting the mark anew.  Therefore, a series of shifted cursor motion commands will continuously extend the region.

Shift-selection only works if the shifted cursor motion key is not already bound to a separate command (see Customization).  For example, if you bind S-C-f to another command, typing S-C-f runs that command instead of performing a shift-selected version of C-f (forward-char).

A mark set via mouse commands behaves the same as a mark set via shift-selection (see Setting Mark).  For example, if you specify a region by dragging the mouse, you can continue to extend the region using shifted cursor motion commands.  In either case, any unshifted cursor motion command deactivates the mark.

To turn off shift-selection, set shift-select-mode to nil.  Doing this does not disable setting the mark via mouse commands.

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

By default, the mark is activated by setting it, and deactivated by most non-motion commands (including all commands that change the text in the buffer).  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 never deactivated, but it can be set to different locations using commands such as C-SPC.  Emacs does not highlight the region, because that would be a nuisance.  As a special exception, the region is temporarily highlighted if you set it with the mouse (see Setting Mark), or with shift-selection (see Shift Selection).

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 it on again.  You can also toggle Transient Mark mode using the Active Region Highlighting menu item in the Options menu.

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, which ordinarily act on the region when the mark is active, no longer do so.  For example, normally M-% (query-replace) performs replacements within the region, if the mark is active.  When Transient Mark mode is off, it always operates from point to the end of the buffer.  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 and enable Transient Mark mode temporarily, until the mark is next 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.