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

How do I re-indent the entire paragraph under my cursor to match the surrounding code?

Answer

=ip

Explanation

The =ip command combines Vim's auto-indent operator (=) with the inner paragraph text object (ip) to re-indent every line in the current paragraph in a single keystroke. The cursor can be anywhere inside the paragraph — Vim finds the boundaries automatically.

This is one of the fastest ways to fix mangled indentation after pasting code from a different context or after a refactor that changed nesting depth.

How it works

  • = — the auto-indent operator; accepts any motion or text object
  • ip — "inner paragraph": the block of non-blank lines surrounding the cursor
  • Together, Vim re-indents the selected lines using the rules for the current filetype (indentexpr, cindent, smartindent, etc.)

Example

After pasting, a Python function ends up with wrong indentation:

def greet(name):
print(f"Hello, {name}")
return name

With the cursor anywhere in the block, pressing =ip:

def greet(name):
    print(f"Hello, {name}")
    return name

Tips

  • =ap uses the "around paragraph" text object, which also includes surrounding blank lines
  • == re-indents only the current line; =G re-indents from the cursor to end of file
  • For the best results, ensure filetype detection is on (:filetype plugin indent on) so Vim uses language-aware indentation rules
  • After =ip, use . to repeat the same re-indent operation on the next paragraph

Next

How do I make Vim automatically reformat paragraphs as I type so lines stay within the textwidth?