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

How do I paste text above the current line?

Answer

P

Explanation

The P (uppercase) command pastes the contents of the default register before the cursor position. When the register contains a whole line (yanked with yy or deleted with dd), P pastes it on a new line above the current line.

How it works

  • p (lowercase) pastes after the cursor or below the current line
  • P (uppercase) pastes before the cursor or above the current line
  • The behavior depends on whether the register holds a linewise or characterwise yank

Example

Given the text:

first line
second line
third line

With the cursor on third line, yank first line with ggyy, then move back to third line with G. Pressing P results in:

first line
second line
first line
third line

The yanked line is inserted above the cursor line.

Tips

  • For characterwise text, P pastes before the cursor while p pastes after
  • Use "+P to paste from the system clipboard above the current line
  • Use ]p to paste and automatically adjust indentation to match the surrounding code
  • Combine with dd and P to move a line upward

Next

How do I edit multiple lines at once using multiple cursors in Vim?