Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 38 ยท Editing and Buffers

The Mark, the Region and Selection

Select text in Emacs with the mark and the region: C-SPC, C-x C-x, mark-word, mark-paragraph, mark-whole-buffer, the mark ring and transient mark mode.

Emacs selects text with two positions: point (where the cursor is) and the mark (a position you set earlier). The text between them is the region. Most commands that work on a chunk of text, such as C-w kill, M-w copy and M-x indent-region, act on the region. Understanding the mark is the key to selecting accurately and to jumping back and forth in a file.

Setting the mark

Keys Command Effect
C-SPC (or C-@) set-mark-command Set the mark at point and activate the region
C-x C-x exchange-point-and-mark Swap point and mark; the region stays
C-x h mark-whole-buffer Select everything
M-h mark-paragraph Select the current paragraph
C-M-h mark-defun Select the current function
M-@ mark-word Select the next word
C-M-@ mark-sexp Select the next balanced expression
Shift + movement key Select while moving (works with arrow keys too)

The typical sequence: press C-SPC where the text starts, move to where it ends with any motion (M-f, C-n, M->, a search), and apply a command such as C-w. The region is highlighted while the mark is active. Pressing C-g deactivates it without changing the text.

On some systems C-SPC is taken by an input method switcher. Use C-@ instead, or rebind the key in your init file.

Transient Mark mode

By default Emacs runs in Transient Mark mode, where the region is highlighted and typing over it with some commands deactivates it. Insertion does not replace the selected text unless you turn on delete-selection-mode (M-x delete-selection-mode), which gives the familiar behavior from other editors: typing replaces the selection and DEL deletes it. Most people enable that early.

JS Stand-in: point, mark and region

Press Run (or Ctrl + Enter) to see the console output here.

No output.

C-x C-x is useful for checking and adjusting a selection. Swapping point and mark moves the cursor to the other end of the region, so you can see where it starts, and a second press returns. You can also extend the region from either end.

The mark ring

Every time you set the mark, the old position is saved in the mark ring (and some commands such as M-<, M->, searches and M-g g set the mark for you and say "Mark set"). That makes the mark a built-in "go back" feature:

  • C-u C-SPC jumps to the previous mark position, then the one before it on repeated presses (after the first one you can keep pressing C-SPC with the repeat feature of newer Emacs, or C-u C-SPC again).
  • C-x C-SPC (pop-global-mark) does the same across buffers, using the global mark ring.

Typical habit: before any big jump (search, M->, M-g g), do not worry about it: Emacs marked your starting place. After you finish reading, C-u C-SPC takes you home. The mark-ring-max variable (16 by default) limits how many positions are remembered.

JS Stand-in: a mark ring

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Working with the region

Once a region is active, these commands apply to it:

Keys Effect
C-w / M-w Kill / copy
C-x C-u / C-x C-l Upcase / downcase (they are disabled by default; Emacs asks whether to enable them)
M-% Query replace limited to the region
C-M-\ Indent the region to match the code style
M-; Comment or uncomment the region
M-x sort-lines Sort the lines
M-x fill-region Reflow text
C-x TAB Indent the region rigidly with arrow keys
M-| Pipe through a shell command

The disabled-command prompt appears for a few commands that Emacs considers confusing to beginners (such as C-x C-u). Answer y to enable them permanently; Emacs writes a line in your init file or custom file.

Multiple selections and rectangles

The region is a contiguous stretch of text. For columns, use rectangle mark mode: C-x SPC activates a rectangular region. The rectangles, registers and macros lesson covers it. For editing many places at once, packages like multiple-cursors add multi-cursor behavior, but the built-in M-x occur with edit mode (the previous section) and keyboard macros take you a long way.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What defines the region?

    The region is the text between the mark and point, in either order.
  2. 2. Which key selects the whole buffer?

    C-x h is mark-whole-buffer.
  3. 3. How do you return to where you were before a long jump?

    C-u C-SPC pops the mark ring, which Emacs fills whenever a command sets the mark.
  4. 4. What does C-x C-x do?

    It exchanges point and mark, moving the cursor to the other end of the region.

Summary

  • The region is the text between point and the mark; C-SPC sets the mark, C-x C-x swaps the ends.
  • C-x h, M-h, C-M-h, M-@ select large units quickly.
  • The mark ring remembers earlier positions: C-u C-SPC goes back, C-x C-SPC works across buffers.
  • delete-selection-mode makes typing replace the selection; many commands act on the region.

Next: Undo and Redo