11.3 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
orC-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
orC-M-\
(see Indentation). - Fill it as text with
M-x fill-region
(see Filling). - Check the spelling of words within it with
M-$
(see Spelling). - 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).
Some commands have a default behavior when the mark is inactive, but operate on 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 mark 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
.
As described in Erasing, the DEL
(backward-delete-char
) and Delete
(delete-forward-char
) commands also act this way. If the mark is active, they delete the text in the region. (As an exception, if you supply a numeric argument n
, where n
is not one, these commands delete n
characters regardless of whether the mark is active). If you change the variable delete-active-region
to nil
, then these commands don’t act differently when the mark is active. If you change the value to kill
, these commands kill the region instead of deleting it (see Killing).
Other commands always operate on the region, and have no default behavior. Such commands usually have the word region
in their names, like C-w
(kill-region
) and C-x C-u
(upcase-region
). If the mark is inactive, they operate on the inactive region—that is, on the text between point and the position at which the mark was last set (see Mark Ring). To disable this behavior, change the variable mark-even-if-inactive
to nil
. Then these commands will instead signal an error if the mark is inactive.
By default, text insertion occurs normally even if the mark is active—for example, typing a
inserts the character ‘a
’, then deactivates the mark. Delete Selection mode, a minor mode, modifies this behavior: if you enable that mode, then inserting text while the mark is active causes the text in the region to be deleted first. To toggle Delete Selection mode on or off, type M-x delete-selection-mode
.