how to find matching parenthesis or braces in emacs editor

In Emacs, there are quite a few packages that allow you to match braces and brackets in the buffer. Some of these packages provide more functionality than others, but almost all them provide the matching functionality.

Depending on the major mode of the buffer, different pairs of characters can be considered a matching parenthesis. Almost all modes support “()” as parenthesis along with “[]” and “{}”.

The keychord to navigate between matching or balanced parenthesis are:

  • C-M-f This moves forward to the matching parenthesis
  • C-M-b This moves backwards to the matching parenthesis

It is not necessary for the cursor to be on the parenthesis for this to work. If it is not on a bracket or brace then it moves across the word boundaries, but as soon as it finds a parenthesis it will jump to the matching parenthesis.

If you want jump only using the parenthesis, then the following keychord can be helpful.

  • C-M-n This moves forward over a parenthetical group
  • C-M-p This moves backwards over a parenthetical group

If you work extensively with programming languages then you can use C-M-u to easily jump to the starting parenthesis of the enclosing or surrounding block (or scope). This can be useful to move to the correct parenthesis you are trying to match.

One of the popular package that lets you deal with parenthesis in a reasonable way is SmartParens. It has several additional functionalities that aid in writing in addition to navigation.

Here is my “current” config to load it….You can possibly restrict it to programming modes by using a hook.

(use-package smartparens :diminish :ensure t :config (smartparens-mode))