Emacs Advent Calendar 02: Mark and Region

Wed 2015-12-02

Many commands act on part of a buffer. To specify this part, one sets the mark at one end and positions point to the other end. The text now enclosed between point an mark is called the region.

Note that point does not necessarily has to come before mark, the region behaves the same either way.

When the mark is set, it becomes activated, as does the region that is defined by it. An active region is highlighted in the buffer. A region can be deactivated by the universal canceling command C-g.

To set the mark at point press C-SPC. Now the region can be extended by moving away point. To correct the position of mark use the command C-x C-x. This swaps the positions of point and mark and allows the start of the region to be moved around.

Several commands exist for selecting separate textual objects. M-@ sets the mark at the end of the next word, repeated presses move the mark forward one word at a time. Similarly M-h selects the current paragraph and C-x h the complete buffer.

When Emacs is operated under X, the text inside the region is saved to primary selection, i.e. it can be inserted into other applications by a click of middle mouse button.

Besides specifying the region, mark has another use: Each activation of mark pushes its position on the mark ring. To cycle through the earlier positions of mark, press C-u C-SPC repeatedly. Manually pushing a position on the ring is accomplished with the command C-SPC C-SPC (which in fact activates the region and then immediately deactivates it again).

Some commands push a mark position on the ring themselves and indicate it by showing "Mark set" in the echo area.

Full documentation of these commands can be found in sections 11.1 (Setting the mark), 11.2 (Commands to mark textual objects) and 11.4 (The Mark Ring) of the Emacs Reference Manual.

Tags:

This text by Ludger Sandig is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.