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

How do I reindent the whole file without adding extra jump-list entries?

Answer

:keepjumps normal! gg=G

Explanation

Bulk formatting commands are common in cleanup sessions, but they often leave side effects in your navigation history. Wrapping a full-buffer reindent in :keepjumps avoids polluting the jump list, so your usual <C-o>/<C-i> workflow remains meaningful after formatting. This is especially useful when you need to reindent quickly in the middle of investigation or review.

How it works

  • :keepjumps executes the following command without recording jump-list entries caused by cursor movement
  • normal! runs raw Normal-mode keys (ignores custom mappings for safety and predictability)
  • gg=G jumps to first line and applies = indentation operator through end of file
  • Combined, you get whole-buffer indentation with minimal navigation side effects

Example

Before:

if ready:
print("start")
  if debug:
      log("x")

Run:

:keepjumps normal! gg=G

After (indentation normalized to filetype rules):

if ready:
  print("start")
  if debug:
    log("x")

Tips

  • Use gg=G only when filetype indent settings are correct (:set filetype?)
  • If you want to preserve marks as well, combine with :keepmarks in scripted workflows
  • For a smaller scope, replace gg=G with a motion like =ap or =}

Next

How do I keep a utility split from resizing when other windows open or close?