emacs: Using Region
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’ (Killing).
• Copy it to the kill ring with ‘M-w’ (Yanking).
• Convert case with ‘C-x C-l’ or ‘C-x C-u’ (Case).
• Undo changes within it using ‘C-u C-/’ (Undo).
• Replace text within it using ‘M-%’ (Query Replace).
• Indent it with ‘C-x <TAB>’ or ‘C-M-\’ (Indentation).
• Fill it as text with ‘M-x fill-region’ (Filling).
• Check the spelling of words within it with ‘M-$’ (
Spelling).
• Evaluate it as Lisp code with ‘M-x eval-region’ (Lisp
Eval).
• Save it in a register with ‘C-x r s’ (Registers).
• Save it in a buffer or a file (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 (
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
(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 (
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. Also,
commands that normally delete just one character, such as ‘C-d’ or
‘<DEL>’, will delete the entire region instead. To toggle Delete
Selection mode on or off, type ‘M-x delete-selection-mode’.