With emacs, how to go to the pairing (balancing) parentheses

Emacs

Emacs Problem Overview


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)

Emacs Solutions


Solution 1 - Emacs

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.

Solution 2 - Emacs

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

Solution 3 - Emacs

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

Solution 4 - Emacs

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).

Solution 5 - Emacs

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)))))

Solution 6 - Emacs

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrew_1510View Question on Stackoverflow
Solution 1 - EmacsLindydancerView Answer on Stackoverflow
Solution 2 - EmacsBuzzView Answer on Stackoverflow
Solution 3 - EmacsJorge ZapataView Answer on Stackoverflow
Solution 4 - EmacsychaoucheView Answer on Stackoverflow
Solution 5 - EmacsMoritz BunkusView Answer on Stackoverflow
Solution 6 - EmacsocodoView Answer on Stackoverflow