How do I apply an Ex command only to the exact range of my last change or yank?
Answer
:'[,']normal! =
Explanation
When you need to run a command on exactly the text you just changed, yanked, or pasted, Vim's automatic marks are faster and safer than reselecting manually. The marks '[ and '] track the start and end of the last changed or yanked region, so you can target that precise range immediately. This is especially useful in repetitive refactors where precision matters.
How it works
:'[,']normal! =
:'[,']defines an Ex range from the start mark to the end mark of the last changed/yanked textnormal!executes a Normal-mode command on each line in that range without custom mappings=runs the built-in indent operator on each targeted line
You can swap = for other linewise Normal commands. For example, :'[,']normal! gUU uppercases the same last-changed range.
Example
After pasting a misindented block, run:
:'[,']normal! =
Before:
if ready:
print("start")
process()
After:
if ready:
print("start")
process()
Tips
- This pattern works best right after paste/change/yank, before another edit updates the marks
- Use
:keepjumps '[,']normal! ...if you also want to avoid polluting jump history