Emacs: What's Region, Active Region, transient-mark-mode?

Perm url with updates: http://xahlee.org/emacs/emacs_region.html

Emacs: What's Region, Active Region, transient-mark-mode?

Xah Lee, 2008-06, ..., 2010-03-25

This page is a tutorial on emacs's concept of region, active region, transient-mark-mode, for those who want to write emacs lisp commands.


The last Mark position to the current cursor position is called a Region.

Once a user sets a mark in a buffer (“Ctrl+Enter” or “Alt+x set-mark-command”), a region exists. So, almost always, there exists a region in a buffer.

By convention, commands ending in the word “-region” acts on the region, regardless whether the region is active. For example: kill-region, comment-region, fill-region, indent-region. This system worked well before emacs introduced the region highlighting feature around late 1990s.

You can get the position of region beginning by the function “region-beginning”, similarly “region-end”.

transient-mark-mode, Active Region, Highlighting of Region


Emacs has a minor mode called transient-mark-mode, introduced since emacs 19.x or earlier. It is on by default since Emacs 23. When on, it will highlight the Active Region.

Active Region

Because a region exists once a user set a mark, and always having a section of text highlighted to the cursor position is annoying, so there's a new concept of Active Region.

The active/inactive status of a region is stored the variable mark-active, and this variable can be set/unset by commands.

When is a region Active?

Typically, when set-mark-command is called, the region becomes active (highlighted). When a command is called, typically the command will set the region status to inactive. This means, when you set mark using the keyboard or the mouse, text selection become highlighted, then after you called some command that do something, the region returns inactive again (and the highlighting goes away).

What's Text Selection?

Emacs's concept of “active region” is very much similar to the modern term “Text Selection”. When a region is active, there is a text selection.

Typically, when you want your command to act on active region, do this check:

(and transient-mark-mode mark-active)

Or, use use a higher level function “region-active-p”, which does exactly the above. (lookup its inline doc)

Emacs 23 Changes

Starting with Emacs 23, transient-mark-mode is on by default, and some command behavior changed. If there is a text selection, the command act on it, else it acts on the current word, line, paragraph, buffer (or whatever is its default input).

This change is good, because users don't need to think about whether he should choose the region or non-region version of the command. The command simply act on a text selection if there is one.

Commands with this new behavior includes: fill-paragraph, ispell-word, indent-for-tab-command, comment-dwim. The number of command that are sensitive to existence of text selection will probably increase.

(info "(elisp) The Mark")

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs