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
category: command-line tags: #command-line #marks #indentation #ex-commands #normal-mode
Next
How do I join a wrapped paragraph into one line without manual cursor moves?
Related Tricks
How do I keep an argument list change local to the current window?
:arglocal
How do I turn custom command output like file:line:col:message into a navigable quickfix list?
:setlocal errorformat=%f:%l:%c:%m | cexpr system('tool %') | copen
How do I define a custom Ex command that forwards arguments with file completion?
:command! -nargs=* -complete=file W w <args>
How do I run a substitution over every quickfix hit and save only changed files?
:cdo s/foo/bar/ge | update
How do I count substitution matches before changing anything?
:%s/pattern/replacement/gn
