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

How do I paste text that automatically matches the indentation of the current line?

Answer

[p

Explanation

When you copy code from one indentation level and paste it at another, p preserves the original indentation, leaving your code misaligned. [p and ]p paste the text with its indentation adjusted to match the surrounding context — a major time-saver when restructuring code.

How it works

  • ]p — paste after the cursor, adjusting indented lines to match the current line's indent level
  • [p — paste before the cursor (like P), with the same indentation adjustment
  • The adjustment is relative: if the pasted block had 2 levels of indent and the destination has 3, every line in the block gains one additional level

Example

You yank this function body (4-space indent):

    result = compute(x)
    return result

With your cursor on an 8-space indented line:

        def inner():

Using ]p pastes the block re-indented to 8 spaces:

        def inner():
            result = compute(x)
            return result

Tips

  • Works with any register: "a]p pastes from register a with indentation adjustment
  • Pairs well with yap (yank-a-paragraph) when moving code between nested scopes
  • For manual indent correction after a regular paste, use ='] to re-indent the just-pasted text

Next

How do I run text I've yanked or typed as a Vim macro without recording it first?