What is the command to match brackets in Emacs?
Asked Answered
S

6

47

What is the command to match brackets in Emacs (the equivalent of the % command in Vim)?

Swirly answered 4/11, 2008 at 10:59 Comment(0)
S
51

See show-paren-mode as described in 5.27 How do I show which parenthesis matches the one I'm looking at?

Sideward answered 4/11, 2008 at 11:10 Comment(1)
Also C-M-u and C-M-d. see Moving in the Parenthesis Structure...Uund
E
40

C-M-f, or M-x forward-sexp, goes forward to the closing brace, or to the opening brace in the next set of braces. C-M-b, or M-x backward-sexp, goes backward to the opening brace, or to the closing brace in the next set of braces. These commands will work for parentheses, square brackets, curly braces, angle brackets, etc., and can be customized to recognize other delimiters.

Eugeniaeugenics answered 4/11, 2008 at 12:22 Comment(0)
F
10

Just to complete the last response. I use C-M-n for forward match and C-M-u for backward match. C-M maps to Esc+Ctrl on windows keyboards.

Fango answered 16/5, 2011 at 8:11 Comment(3)
Strange combination of shortcuts, but I'm new to emacs and anyway here's the useful related link.Insured
@Insured Yes, it is strange. But then, Emacs was designed for Lisp which is all about parentheses, so it doesn't just match them, it navigates forward, backward, next, previous, and even up and down. Thanks for the link. It is now located at https://www.gnu.org/software/emacs/manual/html_node/emacs/Moving-by-Parens.html.Majestic
Side note for MS Windows and Apple MacOS, you don't need to use Esc for Meta. If your keyboard does not have a modifier key labelled Meta, you can usually use the one labelled Alt or Option. If it doesn't work, check if your terminal has a setting such as "Option sends Escape".Majestic
T
3

There is also a show-paren-mode. For navigation, I think it's better C-M-n and C-M-n

Traherne answered 9/11, 2008 at 6:20 Comment(1)
Yes, but sometimes the matching paren isn't currently on the screen. This is when forward/backward-sexp really come in handy.Eugeniaeugenics
E
1

https://github.com/redguardtoo/evil-matchit

I read vim matchit code before developing evil-matchit. evil-matchit is offering much more than original vim matchit now.

Besides, you can easily extend evil-matchit to support any new languages in your own ~/.emacs.

Eiffel answered 14/7, 2014 at 7:30 Comment(1)
Cool! It looks like this supports stuff like matching tags in XML also, including languages HTML Python Java C++/C Javascript, JSON Perl Latex CMake Org-mode Ruby Bash Lua PHP Laravel Blade Templating Vim script Emacs email (mesage-mode)Excitor
M
0

You can use %

Emacs offers a panoply of choices, but personally, I use %. Just add this to your .emacs file:

;; Emacs FAQ #5.25: By an unknown contributor, modified to only match braces.
(global-set-key "%" 'match-brace)
(defun match-brace (arg)
  "Go to the matching curly bracket if on one; otherwise insert %."
  (interactive "p")
  (cond ((looking-at "\{") (forward-list 1) (backward-char 1))
        ((looking-at "\}") (forward-char 1) (backward-list 1))
        (t (self-insert-command (or arg 1)))))

Discussion

If the cursor is on { or } and you hit %, then the cursor will move to the matching curly bracket. Otherwise, it just types a %. The original code from the Emacs FAQ actually works for (parentheses) and [square brackets] as well, but I changed it to only curly brackets {"braces"} for programming in C-like languages.

As with Vim, hitting % a second time will jump back to the original bracket. Also, this code positions the cursor directly on top of the closing bracket, not one character afterward (as seen in some alternatives below).

Alternatives

Emacs was designed for the Lisp programming language which is known for its heavy use of nested parentheses. Due to that heritage, Emacs has many ways to traverse matching brackets. Unlike hitting %, the cursor does not need to be on a bracket to use these.

C-M-n, C-M-p: move by group of balanced brackets

CtrlMetan and CtrlMetap moves the cursor across one balanced set of brackets. If the cursor is to the left of or on top of an opening bracket, then C-M-n will move to one character beyond the closing bracket. The reverse happens for C-M-p when the cursor is to the right of (but not on top of) a closing bracket.

Note the difference from Vim! Emacs makes more sense semantically — where you next type will always be at the same nesting level as before the key was hit — but Vim wins in terms of human visual cognition — the matching bracket is highlighted by the cursor. Also, Vim makes it easy to jump back by simply hitting % twice.

Emacs's choice isn't all bad, though. By staying at the same level in the parenthetical structure, it is easy to walk through nested code as a tree since hitting the same key repeatedly visits the next sibling node. (Try this with a large JSON file.)

C-M-f, C-M-b: move by balanced "sexp"

CtrlMetaf and CtrlMetab moves the cursor across a programming expression. This isn't just for brackets, it also leaps over other clumpable units, like numbers or identifiers. Unless you are programming in Lisp, there is little reason to use this instead of C-M-n and C-M-p.

C-M-d, C-M-u: move down and up in nesting depth

CtrlMetad moves the cursor forward, just after the next opening bracket (one level deeper). CtrlMetau moves the cursor backwards, landing on top of the first prior opening bracket (one level up in the tree structure). Note that when the cursor is on an opening bracket, it is technically outside of the nest as anything you type will be inserted before the bracket. Conversely, when the cursor is on a closing bracket, it is inside.

Again, this is more useful for navigating tree structured code and data than simply matching brackets. However, I do note that someone else's answer here actually recommends using C-M-u in combination with C-M-n, presumably because both of those can be used when the cursor is directly on top of a bracket, as in Vim. However, they are not inverses of each other and using one after the other will not return you to where you started.

Majestic answered 3/4 at 21:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.