Emacs extension to increase selected region by semantic units.
Clone or download
andrel and magnars Rephrase how contract-region is used. (#193)
Attempt to clarify contract-region. I misunderstood this at first and wasted time attempting to define another `global-set-key` for contracting.
Latest commit ed32924 Aug 17, 2018
Failed to load latest commit information.
features fix #163 - prefer current pair over previous pair (#204) Aug 17, 2018
.gitignore Use Carton for dev-dependencies and CI Mar 11, 2013
.travis.yml Remove tests for emacs-24.3 Jun 28, 2018
Cask Added: test coverage with undercover.el Oct 15, 2014
README.md Rephrase how contract-region is used. (#193) Aug 17, 2018
cc-mode-expansions.el Fix c-mode expansions. Nov 21, 2016
clojure-mode-expansions.el fix byte compile warning for clojure-mode-expansions Jan 1, 2014
cperl-mode-expansions.el copy-pasta Nov 12, 2014
css-mode-expansions.el Turn er/enable-mode-expansions into a function. Oct 25, 2012
enh-ruby-mode-expansions.el Cleanup per magnar's feedback. Apr 6, 2014
er-basic-expansions.el fix #163 - prefer current pair over previous pair (#204) Aug 17, 2018
erlang-mode-expansions.el Turn er/enable-mode-expansions into a function. Oct 25, 2012
expand-region-core.el transient-mark-mode 'only should be buffer-local (#244) Jun 25, 2018
expand-region-custom.el Smart cursor. Jul 18, 2016
expand-region-pkg.el Release 0.11.0 Oct 20, 2016
expand-region.el Load text-mode as a string. Nov 21, 2016
feature-mode-expansions.el Turn er/enable-mode-expansions into a function. Oct 25, 2012
html-mode-expansions.el fix html-mode byte compiler warnings Jan 4, 2014
js-mode-expansions.el Properly mark JavaScript invocations and property lookups Nov 26, 2014
js2-mode-expansions.el fix js2-mode byte compiler warnings Jan 4, 2014
jsp-expansions.el Correct use of eval-after-load Nov 20, 2016
latex-mode-expansions.el fix latex-mode byte compiler warnings Jan 4, 2014
nxml-mode-expansions.el Use cl-find-if, require cl-lib Jul 31, 2015
octave-expansions.el add function declaration for octave-mod function Jan 6, 2014
python-el-expansions.el fix python byte-compile warnings Jun 10, 2014
python-el-fgallina-expansions.el remove reference to obsolete var in python-el-fgallina-expansions.el Jun 10, 2014
python-mode-expansions.el fix python byte-compile warnings Jun 10, 2014
ruby-mode-expansions.el remove unnecessary local file variable Jan 26, 2014
run-tests.sh Move from Carton to Cask Aug 11, 2013
run-travis-ci.sh Travis: Disable fgallina tests below Emacs 24.3 Apr 8, 2013
sml-mode-expansions.el add function declaration for sml-mode byte compilation warning Jan 6, 2014
subword-mode-expansions.el Require library subword for subword-left and subword-right Jul 31, 2015
text-mode-expansions.el Turn er/enable-mode-expansions into a function. Oct 25, 2012
the-org-mode-expansions.el Fix org-mode tests. Nov 21, 2016
watch-tests.watchr Use Carton for dev-dependencies and CI Mar 11, 2013
web-mode-expansions.el Better web-mode expand behavior (#221) Aug 17, 2018


expand-region.el Build Status Coverage Status

Expand region increases the selected region by semantic units. Just keep pressing the key until it selects what you want.

An example:

(setq alphabet-start "abc def")

With the cursor at the c, it starts by marking the entire word abc, then expand to the contents of the quotes abc def, then to the entire quote "abc def", then to the contents of the sexp setq alphabet-start "abc def" and finally to the entire sexp.

You can set it up like this:

(require 'expand-region)
(global-set-key (kbd "C-=") 'er/expand-region)

If you expand too far, you can contract the region by pressing - (minus key).


You can watch an intro to expand-region at Emacs Rocks.


I highly recommend installing expand-region through elpa.

It's available on marmalade and melpa:

M-x package-install expand-region

Language support

Expand region works fairly well with most languages, due to the general nature of the basic expansions:


However, most languages also will benefit from some specially crafted expansions. For instance, expand-region comes with these extra expansions for html-mode:


You can add your own expansions to the languages of your choice simply by creating a function that looks around point to see if it's inside or looking at the construct you want to mark, and if so - mark it.

There's plenty of examples to look at in these files.

After you make your function, add it to a buffer-local version of the er/try-expand-list.


Let's say you want expand-region to also mark paragraphs and pages in text-mode. Incidentally Emacs already comes with mark-paragraph and mark-page. To add it to the try-list, do this:

(defun er/add-text-mode-expansions ()
  (make-variable-buffer-local 'er/try-expand-list)
  (setq er/try-expand-list (append

(add-hook 'text-mode-hook 'er/add-text-mode-expansions)

Add that to its own file, and add it to the expand-region.el-file, where it says "Mode-specific expansions"

Warning: Badly written expansions might slow down expand-region dramatically. Remember to exit quickly before you start traversing the entire document looking for constructs to mark.


If you make some nice expansions for your favorite mode, it would be great if you opened a pull-request. The repo is at:


All changes must be accompanied by feature tests. They are written in Ecukes, a Cucumber for Emacs.

To fetch the test dependencies, install cask if you haven't already, then:

$ cd /path/to/expand-region
$ cask

Run the tests with:

$ ./run-tests.sh

If feature tests are missing for the mode you are changing, please make sure to add a set of basic tests around the functionality you're changing.


  • Josh Johnston contributed er/contract-region
  • Le Wang contributed consistent handling of the mark ring, expanding into pairs/quotes just left of the cursor, and general code clean-up.
  • Raimon Grau added support for when transient-mark-mode is off.
  • Roland Walker added option to copy the contents of the most recent action to a register, and some fixes.
  • Damien Cassou added option to continue expanding/contracting with fast keys after initial expand.
  • Sylvain Rousseau fixed loads of little annoyances.
  • Ryan Mulligan cleaned up a lot of byte compilation warnings.
  • Lefteris Karapetsas added subword-mode expansions.

Language specific contributions



  • Option expand-region-smart-cursor to keep cursor at beginning of region if it is there (Andrea Orru)

From 0.9 to 0.10

  • Smarter expansion of ruby heredoc contents (Steve Purcell)
  • Add enh-ruby-mode expansions (Bradley Wright)
  • Add basic expansion er/mark-defun
  • Big cleanup of byte compilation warnings (Ryan Mulligan)
  • Better performance
  • Lots of bugfixes

From 0.8 to 0.9

  • Improve org-, clojure-, python-, latex-, cc- and ruby-modes
  • Add basic expansions: email and url
  • Add sml-mode expansions (Alexis Gallagher)
  • Add cperl-mode expansions (Kang-min Liu)
  • Add octave-mode expansions (Mark Hepburn)
  • Add web-mode expansions (Rotem Yaari)
  • Use Carton for dev-dependencies
  • Fix bad behavior in minibuffer (Sylvain Rousseau)
  • More robust comment expansions
  • Improve loading of expansions for all major modes

From 0.7 to 0.8

  • Improve js-, ruby-, python- and latex-modes
  • Support built-in javascript-mode
  • Handle narrowed buffers correctly
  • Include mode-specific expansions when autoloading
  • Provide option to copy the contents of the most recent action to a register
  • Add cc-mode specific expansions
  • Add customization to turn off skipping whitespace when expanding
  • Continue expanding/contracting with one key press (optional)


Copyright (C) 2011-2016 Magnar Sveen

Author: Magnar Sveen magnars@gmail.com Keywords: marking region

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.