Starting with the point or active region, how do I quickly (in one keystroke / command) 'expand' to include the next set of enclosing parentheses?

If I start with: (| means point)

(defn foo [x] (+ x |42))

Or if I start with: (< and > bracket the selected region)

(defn foo [x] (+ x <42>|))

I want to end up with

(defn foo [x] <(+ x 42)>|)

Use expand-region.el:

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)

You can contract the region again with a negative prefix, if you expand too far.

  • 1
    For reference, the specific expand-region function to mark the list immediately is called er/mark-outside-pairs. – nanny Jan 19 '15 at 21:02

I agree you should try expand-region as suggested in another answer (that I'm voting for).

But just in case you aren't familiar with the relevant built-in commands:

C-M-u, backward-up-list
C-M-SPC, mark-sexp

In your first example where there is no region you could useC-M-u C-M-SPC to mark the enclosing list. If there is already a selected region these commands extend it. So if 42 is selected and you hit C-M-u it will extend the selection backward to the beginning of the list but would not include the closing paren.

So in the absence of something like expand-region I would hit C-g to cancel any current selection, then C-M-u to go back as many parens as you want, then C-M-SPC to mark


In the case of active region, h in lispy will do what you want. See my recent intro post.

In the first case, you need to M-m h.

update: all options that you have with lispy

Starting with:

(defn foo [x] (+ x ~42|))

Here, ~ denotes the mark (the start of the region), and | denotes the point (the end of the region).

  1. With h you get:

    (defn foo [x] |(+ x 42)~)
  2. With l you get:

    (defn foo [x] ~(+ x 42)|)
  3. With k you get (undoable with j):

    (defn foo [x] (+ ~x| 42))
  4. With 2k or kk you get :

    (defn foo [x] (~+| x 42))
  5. With d you get:

    (defn foo [x] (+ x |42~))
  6. With d> you get (undoable with <):

    (defn foo [x] (+ |x 42~))
  7. With d2> or d>> you get:

    (defn foo [x] (|+ x 42~))
  8. With m you get:

    (defn foo [x] (+ x 42|))

Just to summarize, when region is active, h, j, k, l are the arrows that move the region. > grows the region by one semantic unit in the direction of the point. < shrinks the region by one semantic unit in the direction of the point. m deactivates the mark. i selects the first element of the region, and d exchanges point and mark.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.