vimtricks.wiki Concise Vim tricks, one at a time.

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 \zs and after \ze is 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

  • \zs alone (without \ze) inserts text after a prefix: :%s/prefix\zs/newtext/g appends newtext after every prefix.
  • You can use \zs and \ze in regular searches too — only the marked region is highlighted.
  • The pattern :%s/prefix\zstarget\ze suffix/replacement/g is equivalent to the more verbose :%s/prefix\(target\)suffix/prefix\1replacement/g\zs/\ze is usually cleaner.

Next

How do I change the working directory to the folder containing the current file without affecting other windows?