80

When cursor on one parentheses, how to jump to the pairing parentheses. Good to work in emacs -nw .

Just like % in Vim.

;;After got hint from @Lindy, @Francesco, I found more:

  C-M-f     Move forward over a balanced expression
  C-M-b     Move backward over a balanced expression
  C-M-k     Kill balanced expression forward
  C-M-SPC   put the mark at the end of the sexp.
  C-M-n  Move forward over a parenthetical group 
  C-M-p  Move backward over a parenthetical group 
  ;; C-M key binding can also be done by --> ESC Control-key

  ;;And put this to .emacs, it will highlight opening/closing parens:
  (show-paren-mode 1)
Andrew_1510
  • 12,258
  • 9
  • 51
  • 52
  • Possible duplicate of [Matching braces in Emacs](http://stackoverflow.com/q/8627725/1225607) – François Févotte Mar 23 '12 at 22:56
  • 1
    there is also great `paredit` mode that keeps parentheses balanced, and allows to manipulate... – Alex Ott Mar 24 '12 at 09:04
  • 7
    Hint: try `C-M-u` to go up to the surrounding paren before `C-M-f`, so you don't have to be on it already. – Tom Oct 30 '13 at 03:36
  • @AlexOtt hope you've become acquainted with SmartParens now, it's an improved/extended Paredit. – ocodo May 01 '14 at 01:10
  • 1
    Regarding parenthetical movement broadly, check out [evil-lisp-state (Github)](https://github.com/syl20bnr/evil-lisp-state) if you're evil. – trailing slash Apr 06 '17 at 16:30

6 Answers6

75

Use C-M-right and C-M-left (respectively backward-sexp and forward-sexp) to go to the beginning or the end of the current expression. This works for parenthesis pairs but also for plain words.

TacticalCoder
  • 6,275
  • 3
  • 31
  • 39
Lindydancer
  • 25,428
  • 4
  • 49
  • 68
  • 7
    Thanks, I found it map to **switch desktop right/left** by my desktop. – Andrew_1510 Mar 23 '12 at 20:03
  • 5
    Who can tell the command name of **C-M-right**? It's now bring me to other workspace by **gnome desktop** – Andrew_1510 Mar 23 '12 at 20:25
  • 12
    @Andrew_1510 It's `backward-sexp`/`forward-sexp`. Instead of holding simultaneously Ctrl+Alt+, you can produce these key bindings using `Esc` then `C-`. This way, the key combination won't be captured by gnome and will correctly be forwarded to emacs. – François Févotte Mar 23 '12 at 20:36
  • 8
    You can also use `C-M-f` and `C-M-b` for forward and backward. (This corresponds to the `C-f` and `C-b` key mappings for plain forward and backward movement that predates keyboards with arrow keys.) – Lindydancer Mar 23 '12 at 22:38
  • You should probably unbind them from Gnome if you don't use that functionality that often. – Noufal Ibrahim Mar 24 '12 at 13:55
  • 1
    @Francesco: thanks a *lot* for this ESC thinggy to prevent conflicts, I never knew about it (despite being a Gnome / Emacs user since, well, Gnome exist ; ) Going to +1 one your answer so that you gain some SO rep ! – TacticalCoder Mar 24 '12 at 14:13
  • 1
    Most DE already bind C-M-arrow keys for window or desktop movement. I recommend C-M-f and C-M-b which do the same trick and are likely to be DE-bound free. – ychaouche Oct 30 '12 at 14:07
32

As mentioned in emacs wiki (http://www.emacswiki.org/emacs/NavigatingParentheses):

  • C-M-n forward-list Move forward over a parenthetical group

  • C-M-p backward-list Move backward over a parenthetical group

  • C-M-f forward-sexp Move forward over a balanced expression

  • C-M-b backward-sexp Move backward over a balanced expression

  • C-M-k kill-sexp Kill balanced expression forward

  • C-M-SPC mark-sexp Put the mark at the end of the sexp.

https://superuser.com/questions/677516/how-do-i-jump-to-the-opening-or-closing-paren-brace-in-emacs

Community
  • 1
  • 1
Buzz
  • 1,549
  • 3
  • 16
  • 31
23

For parentheses, braces and brackets just double clicking on them does the trick.

Asrail
  • 144
  • 1
  • 7
Jorge Zapata
  • 2,316
  • 1
  • 30
  • 57
  • 47
    clicking? with your mouse? in emacs? – Alexander Corwin Mar 23 '12 at 19:59
  • 3
    Clicking? In emacs? Bah, I say! :) -- *(I'm assuming the OP's not using xemacs?)* – Rob Hruska Mar 23 '12 at 19:59
  • 2
    Sometimes it is faster just to click something than to search for it or move the cursor position. FWIW, *double clicking* on some parens, bracket or curly braces will move the cursor to the its pair and select the inner contents. As of Emacs 24 on X, at least. – Asrail Apr 30 '14 at 22:20
  • 1
    Nice! It works but only if you double click the open brace. It doesn't work if you click on the close to find the open (`backward-sexp` equivalent). – Felipe Aug 17 '16 at 20:05
  • 1
    Why would anyone double-click when they can just do **C-s ( Left C-Space C-M-Right**? – Martin Jambon Jul 13 '18 at 23:43
  • @Felipe in Emacs26 you can double click on the close bracket to select the inner contents. Although I find clicking in Emacs blasphemy – Swedgin Nov 04 '19 at 16:12
8

I suggest C-M-f and C-M-b, as C-M-right/left are already bound to my DE (switch to desktop on the right / left).

ychaouche
  • 4,922
  • 2
  • 44
  • 52
5

I would highly recommend SmartParens it has extensive navigation and manipulation of parenthetical structures (ie. wrapping, quotes, tags, brackets, braces, regular parentheses, sexp, etc.) With support for many languages, and structures, with easy customization.

It also supports fairly complex structures, which are referred to as hybrid-s-expressions in it's documentation. Which makes it extremely powerful for manipulating code in languages such as C/C++, Java, JS etc.

For navigation the following are used.

sp-forward-sexp (&optional arg)                 ;; C-M-f
sp-backward-sexp (&optional arg)                ;; C-M-b
sp-down-sexp (&optional arg)                    ;; C-M-d
sp-backward-down-sexp (&optional arg)           ;; C-M-a
sp-up-sexp (&optional arg)                      ;; C-M-e
sp-backward-up-sexp (&optional arg)             ;; C-M-u
sp-next-sexp (&optional arg)                    ;; C-M-n
sp-previous-sexp (&optional arg)                ;; C-M-p
sp-beginning-of-sexp (&optional arg)            ;; C-S-d
sp-end-of-sexp (&optional arg)                  ;; C-S-a
sp-beginning-of-next-sexp (&optional arg)       ;; none
sp-beginning-of-previous-sexp (&optional arg)   ;; none
sp-end-of-next-sexp (&optional arg)             ;; none
sp-end-of-previous-sexp (&optional arg)         ;; none

Note that it maps many to commands to the Emacs default equivalent. When it's installed, just browse it's functions (they're all prefixed with sp-) to get a good feeling for it's scale.

There's a lot more to it, I'd recommend you have a look at the wiki

ocodo
  • 29,401
  • 18
  • 105
  • 117
5

I use the following small function for exactly that (though I don't know whether or not it matches vim's behavior; I'm no vim user myself):

(defun mo-match-paren (arg)
  "Go to the matching parenthesis."
  (interactive "p")
  (cond ((looking-at "\\s\(") (forward-list 1) (backward-char 1))
        ((looking-at "\\s\)") (forward-char 1) (backward-list 1))
        (t (self-insert-command (or arg 1)))))
Moritz Bunkus
  • 11,592
  • 3
  • 37
  • 49
  • 1
    This looks like it wants to be bound to '%' as it will insert the key pressed if it is not on a paren – M Smith Sep 09 '16 at 23:00