How do I replace only part of a matched pattern using \zs and \ze in a substitution?
Answer
:%s/\zsparseData\ze(/processData/g
Explanation
Vim's \zs and \ze atoms let you include context in your search pattern without including that context in the replacement. \zs marks the start of the actual match, and \ze marks the end — everything outside these markers is used only for positional matching but is never replaced.
This solves a common problem: you want to replace part of a pattern (like a function name) but need surrounding context to avoid false positives. Without \zs/\ze, you'd need capture groups and \1 backreferences, which is more verbose.
How it works
\zs— set the start of the match (replacement begins here)\ze— set the end of the match (replacement ends here)- Text before
\zsand after\zeis matched for positioning but not included in the replacement
Example
Rename a function parseData to processData, but only where it's followed by ( — leaving variables also called parseData untouched:
:%s/\zsparseData\ze(/processData/g
Before:
result = parseData(input)
let parseData = "raw"
call parseData(items)
After:
result = processData(input)
let parseData = "raw"
call processData(items)
The assignment let parseData = "raw" is unchanged because it isn't followed by (.
Tips
\zsalone (without\ze) inserts text after a prefix::%s/prefix\zs/newtext/gappendsnewtextafter everyprefix.- You can use
\zsand\zein regular searches too — only the marked region is highlighted. - The pattern
:%s/prefix\zstarget\ze suffix/replacement/gis equivalent to the more verbose:%s/prefix\(target\)suffix/prefix\1replacement/g—\zs/\zeis usually cleaner.