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
]ppastes after the cursor with adjusted indent[ppastes 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
ppreserves original indentation (often wrong) ]pis 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
pto]pif you always want auto-indent on paste