vimtricks.wiki Concise Vim tricks, one at a time.

How do I jump to the nearest unmatched opening parenthesis or bracket above the cursor?

Answer

[( / [{ / [{

Explanation

The [( and [{ motions jump backward to the nearest unmatched opening parenthesis or curly brace. Their counterparts ]) and ]} jump forward to the nearest unmatched closing delimiter. These are invaluable for navigating code where you need to find which block or function call you are currently inside.

The motions

Motion Jumps to
[( Previous unmatched (
[{ Previous unmatched {
]) Next unmatched )
]} Next unmatched }

"Unmatched" means the delimiter does not have a matching pair between it and the cursor.

Example

if (x > 0) {
    while (y < 10) {
        doSomething(x, y);
        // cursor is here |
    }
}

With cursor on the comment line:

  • [( jumps to the ( of while (y < 10)
  • [{ jumps to the { of while
  • 2[{ jumps to the { of if
  • 2[( jumps to the ( of if (x > 0)

Tips

  • Works as a motion with operators: d[( deletes from cursor back to the unmatched (; y[{ yanks from cursor back to the unmatched {
  • Accepts a count: 3[( finds the third unmatched ( outward
  • % jumps between matched pairs; [( and ]) find UNmatched pairs — they solve different problems
  • For C/C++, [m and ]m jump to method/function boundaries (similar idea but language-aware)
  • These motions work in any language — anywhere that uses () or {} as delimiters

Next

How do I run a search and replace only within a visually selected region?