How do I visually re-select the exact region I just changed or yanked?
Answer
`[v`]
Explanation
After any change (c, d, s, y, p, etc.) or paste operation, Vim automatically sets two special marks: `[ at the first character and `] at the last character of the affected text. Using `[v`] visually re-selects that exact region without having to re-count or re-specify a motion.
How it works
`[— jump to the position of the first character of the last change/yankv— start character-wise visual selection at that position`]— extend the selection to the last character of the last change/yank
Together they reactivate a visual selection covering exactly the previously affected text.
Example
You yank a word with yiw, move elsewhere, then realize you need to format the same original word differently. Return to normal mode and press `[v`] to re-select it exactly, then press = to reindent, u to lowercase, or any other operator.
Or: you paste a block with p, then immediately `[v`] to select the pasted region and run :'<,'>!fmt to reformat just that block.
" After yanking 3 lines with 3yy, re-select them:
`[V`]
" After changing a word with ciw and escaping, re-select what was typed:
`[v`]
Tips
'[and'](line-level marks) jump to the line of the first/last changed character- These marks persist until the next change/yank operation overwrites them
- In Vimscript,
getpos("'[")andgetpos("']")let you query the exact position programmatically - The marks also work cross-file: if you yanked from another buffer, they still point to the right location in that buffer
:[range]automatically uses'<,'>when you press:from visual mode, but`[v`]is what you need when you've already left visual mode