How do I visually select an entire code block to the matching closing brace?
Answer
V]}
Explanation
Pressing V]} in normal mode enters visual line mode and immediately extends the selection to the next unmatched closing brace }. This is a fast way to select an entire function body or block from the current line without manually counting lines or using text objects that require the cursor to be in a specific position.
How it works
V— enters visual line mode, selecting the current line]}— Vim motion that jumps to the next unmatched}(a brace that has no matching{at the same nesting level)- Together, they extend the visual selection from the current line down to the line containing the closing brace
This works for any depth of nesting — Vim tracks bracket balance and finds the correct enclosing }.
Example
Given this code with the cursor on the func line:
func processItems(items []string) {
for _, item := range items { <- cursor here
fmt.Println(item)
}
}
With cursor on for _, item..., pressing V]} selects:
for _, item := range items {
fmt.Println(item)
}
To select the full function including its outer braces, first jump to the function opening with [{, then use V]} to grab everything through the closing }.
Tips
V[{selects from the current line up to the previous unmatched{- Combine with operators:
V]}ddeletes the block,V]}yyanks it,V]}=re-indents it - The complementary
[(and])motions work the same way for parentheses