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

How do I jump to the enclosing curly brace or parenthesis in code?

Answer

[{ / ]}

Explanation

When editing code inside a deeply nested block, [{ jumps backward to the unmatched { that encloses the current position, and ]} jumps forward to its matching }. This lets you instantly navigate to the boundaries of the current code block without scrolling or searching.

How it works

  • [{ — move backward to the nearest unmatched { (the opening brace of the enclosing block)
  • ]} — move forward to the nearest unmatched } (the closing brace of the enclosing block)
  • [( / ]) — same behavior but for parentheses
  • These motions skip over balanced brace pairs, only stopping at the one that has no match on the side you came from

Example

func processItems(items []Item) {
    for _, item := range items {
        if item.IsValid() {
            // cursor is here
            doSomething(item)
        }
    }
}

With the cursor on the doSomething line:

  • [{ jumps to the { after if item.IsValid()
  • Pressing [{ again jumps to the { after for _, item := range items
  • ]} from the original position jumps to the } that closes the if block

Tips

  • These are motions, so they work with operators: d[{ deletes from cursor to the enclosing {, v]} visually selects to the closing }
  • Use % when you are already on a brace to jump to its match; use [{/]} when you are inside the block and want to reach its boundaries
  • Combine with [{V% to visually select the entire enclosing block (jump to opening brace, linewise visual, then % to its match)

Next

How do I ignore whitespace changes when using Vim's diff mode?