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

How do I paste text and automatically adjust its indentation?

Answer

]p

Explanation

The ]p command pastes text and adjusts its indentation to match the current line. This is perfect for pasting code at a different indentation level.

How it works

  • ]p pastes after the cursor with adjusted indent
  • [p pastes before the cursor with adjusted indent
  • The indentation of the pasted text matches the target context

Example

You yank a function body at one indentation level and paste it inside a nested block. ]p automatically adjusts the indentation.

Tips

  • Regular p preserves original indentation (often wrong)
  • ]p is especially useful when moving code between indentation levels
  • After regular p, you can fix indent with `[v`]=
  • Works with both linewise and characterwise pastes
  • Map p to ]p if you always want auto-indent on paste

Next

How do you yank a single word into a named register?