23

In GNU Emacs there is a feature to highlight matching brackets in code with the same colour.

However when the code which the brackets enclose is really long with several nested if's for's etc. then this feature is not really useful since one of the brackets will not be visible.

Say I have the following,

for(int i=0; i< N; ++i)
 {
  /*Long code*/

 } 

If my cursor is on the } brace I would like to have some feature which will enable me to jump / see the { brace, and then , if satisfied, come back to the } brace for any future coding.

Is this possible in Emacs?

sarnold
  • 102,305
  • 22
  • 181
  • 238
smilingbuddha
  • 14,334
  • 33
  • 112
  • 189

5 Answers5

35

This is actually a very standard binding: C-M-f and C-M-b to go back and forwards by default. In most modes C-M-f will take you forwards to the matching brace and C-M-b will take you backwards to the matching brace. This also works for things like quotes, pretty much the same way.

These bindings are easy to remember if you already use C-f and C-b for navigation. (If you don't, you should.) They're just like moving forward and backwards by a character lifted to moving by expression (which depends on mode).

Tikhon Jelvis
  • 67,485
  • 18
  • 177
  • 214
  • I think in case of sufficient distance between 2 braces, instead of just displaying the message, or the navigation, it 'should' also divide the screen in 2 parts (with duplicate buffer in 2nd part) and show both braces. – aartist Dec 25 '11 at 22:51
2

The first thing that might help is knowing about this option, if you don't already: blink-matching-paren-distance. If the sexp is very large then you need to increase the option value, or else paren matching gives up too soon and it shows a mismatch when there is no mismatch.

The second thing that can help is to be sure that blink-matching-paren and blink-matching-paren-on-screen are both non-nil. Then, to see the opening delimiter, just delete the closing delimiter and then type it again. When you insert it, the opening one will be made evident.

Drew
  • 29,895
  • 7
  • 74
  • 104
1
(defun px-match-paren (arg)
  "Go to the matching paren if on a paren; otherwise insert <key>."
  (interactive "p")
  (cond
   ((char-equal 41 (char-before)) (backward-list 1))
   ((char-equal 125 (char-before)) (backward-list 1))
   ((and
     (char-equal 123 (char-before))
     (char-equal 10 (char-after)))
    (backward-char 1) (forward-list 1))
   ((looking-at "\\s\(") (forward-list 1))
   ((looking-at "\\s\)") (backward-list 1))
   (t (self-insert-command (or arg 1)))))

The <key> to witch you bind this command will toggle opening and closing brace if point is before or after it, otherwise insert <key> (much like vi's "%")

I also bind C-<key> to forward-sexp and C-S-<key> to backward-sexp so I can really quickly navigate through functions in the file.

yPhil
  • 8,049
  • 4
  • 57
  • 83
1

backward-sexp is what I use. bound to ESC-left. Then ESC-right to get back to where you were

aaron
  • 1,746
  • 1
  • 13
  • 24
1

Try mic paren which shows matching parenthesis code even if found outside the present screen.

Tom
  • 7,515
  • 7
  • 38
  • 54