How do I search and replace only within a visual block selection?
:'<,'>s/\%Vold/new/g
The \%V atom restricts a search pattern to match only within the visual selection area, including visual block selections.
:'<,'>s/\%Vold/new/g
The \%V atom restricts a search pattern to match only within the visual selection area, including visual block selections.
:%s/\<word\>/\u&/g
Vim's substitute command supports case conversion modifiers in the replacement string.
/pattern1\_.*pattern2
Vim's \ modifier makes any character class match newlines too.
:%s/\<old\>/new/g
Wrapping your search pattern in \ word boundary anchors ensures that Vim only matches the exact whole word, preventing accidental replacements inside longer wor
:%s/pattern/\=expression/g
Vim's substitute command supports expression replacements using \= in the replacement string.
/\v
Vim's default regex syntax requires backslashes before most special characters like +, (, ), {, and , which is the opposite of what most developers expect from
/foo\_.*bar
Vim's default .
/pattern/+3
Vim's search command accepts an offset after the pattern that shifts where the cursor lands relative to the match.
/\%Vpattern
The \%V atom restricts a search pattern to only match inside the most recent visual selection.
:%s//new/g
Leaving the search field empty in a :s command tells Vim to reuse the last search pattern from / or .
search #search #substitution #ex-commands #regex #productivity
:%s/\(pattern1\)\(pattern2\)/\2\1/g
Vim's substitute command supports capture groups that let you match parts of text, remember them, and rearrange or reuse them in the replacement.