How do I indent all lines from the cursor to the matching closing bracket in Vim?
Answer
>%
Explanation
Vim's > operator (indent) works with any motion or text object — including %, which jumps to the bracket, parenthesis, or brace matching the one under the cursor. Combining them as >% indents every line from the current line to the matching closing delimiter, letting you re-indent entire code blocks without entering visual mode.
How it works
>is the indent operator; it shifts lines right by oneshiftwidth%moves to the matching bracket (works with(,),{,},[,])- When used as an operator+motion,
>%operates on all lines between the cursor and the matched bracket, inclusive
Place the cursor on an opening bracket and press >% to indent everything inside (and including) the surrounding delimiters.
Example
With the cursor on { at the start of a function body:
function greet() {
if (x) {
return x;
}
}
After pressing >%:
function greet() {
if (x) {
return x;
}
}
The entire body is shifted right by one indent level.
Tips
- Use
<%to un-indent from cursor to matching bracket - Use
=%to auto-indent the block according to your indentation rules (often more useful) - Add a count for multiple indent levels:
2>%indents by twoshiftwidths - Works on any matchable delimiter, not just
{}; useful with(for long function argument lists - The
%motion is powered bymatchpairs; see:help matchpairsto customize which brackets it matches