7

Is there a shortcut for going to the trailing curly brace in a long piece of code?

In a few editors if I double click the { it takes me to the trailing brace }.

While writing large pieces of code I seem to forget to add this at times and also it would make debugging easy if such an implementation is available.

I know the shortcut for searching words, take the cursor on the word and press shift+8. Is there any similar shortcut for finding ending braces?

2 Answers 2

14

Are you looking for :h %?

%           Find the next item in this line after or under the
            cursor and jump to its match. |inclusive| motion.
            Items can be:
            ([{}])      parenthesis or (curly/square) brackets
                    (this can be changed with the
                    'matchpairs' option)

Pressing % in normal mode when the cursor is before or on an item like a parenthesis will jump to the matching item.

2
  • There is also ":set showmatch" or ":set sm" in vi, but I kind of hate it because it jumps around so much. But it is another option.
    – MJB
    Commented Jan 3, 2020 at 21:40
  • 2
    Yes that is exactly what I was looking for thank you sir.. you've just made reviews a lot easier for me :)
    – Sujay
    Commented Jan 4, 2020 at 5:02
2

If your {} is on first column:

  • ]] goto next { in first column, it's the open { of next function.
  • ][ goto next } in first column, it's the close } of current function.
  • [[ goto previous { in first column, it's the open { of current function.
  • [] goto previous } in first column, it's the close } of previous function.

It doesn't care how many nested {} there are, it only looks for {} in first column.

You can also use ]} and [{ to goto } and { of current {} block.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.