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 lineP(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,
Ppastes before the cursor whileppastes after - Use
"+Pto paste from the system clipboard above the current line - Use
]pto paste and automatically adjust indentation to match the surrounding code - Combine with
ddandPto move a line upward