How do I perform a substitute only within a visual block column?
Answer
:'<,'>s/\%Vpattern/replacement/g
Explanation
Using \%V in a substitute pattern restricts matching to within the visual block area only, rather than the full lines.
How it works
- Make a block selection with
<C-v> - Type
:(auto-fills'<,'>) - Use
\%Vin the pattern to restrict to the block - Only text within the block column is affected
Example
With a CSV file:
Alice,30,NYC
Bob,25,LA
Carol,35,SF
Select only the age column, then :'<,'>s/\%V\d\+/0/g replaces just the ages with 0.
Tips
- Without
\%V, the entire line is subject to substitution \%Vis the key atom for block-restricted operations- Works with any regex pattern
- Very powerful for editing tabular data or fixed-width formats