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

How do I re-indent all lines from the cursor to the end of the file in one keystroke?

Answer

=G

Explanation

The =G command applies Vim's auto-indent operator (=) from the current line to the last line of the file (G). This is the fastest way to fix indentation after pasting a large block of code, importing a snippet, or making structural edits that shift several levels of nesting.

How it works

  • = is Vim's indent operator — it applies indentation rules defined by indentexpr, cindent, lisp, or the built-in autoindent heuristics, depending on the filetype
  • G is the motion: "to the last line of the file"
  • Combined, =G processes every line from the cursor to EOF
  • If you are on line 1, the entire file is re-indented (same as gg=G)

Example

After pasting a Python snippet, your file looks like:

def greet(name):
x = f"Hello, {name}"
if x:
return x

With the cursor on x = f"Hello, pressing =G (with filetype=python) produces:

def greet(name):
    x = f"Hello, {name}"
    if x:
        return x

Tips

  • gg=G re-indents the entire file — go to line 1 first, then apply =G
  • =% re-indents the current block delimited by matching brackets
  • =i{ re-indents the contents of the current curly-brace block (without the braces)
  • =ip re-indents the current paragraph
  • Indentation quality depends on the filetype plugin; run :set filetype? to confirm the correct filetype is active before using =G

Next

How do I programmatically set a register's content in Vimscript to pre-load a macro?