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

How do I use \zs to set the start of a search match so only part of the pattern is highlighted or operated on?

Answer

/prefix\zsword

Explanation

Vim's \zs atom marks the start of the match within a longer pattern. Everything before \zs must be present in the text for the pattern to match, but it is excluded from the match itself — only the portion from \zs onwards is highlighted, substituted, or operated on.

How it works

  • \zs (zero-width start) sets the beginning of the captured match.
  • The counterpart \ze sets the end of the captured match.
  • Text before \zs acts as a required context (a zero-width look-behind equivalent), but is not consumed by the match.

Example

Suppose you want to highlight only the version number in lines like:

version: 1.2.3
version: 2.0.0

Search: /version: \zs\d\+\.\d\+\.\d\+

This matches the full version: 1.2.3 context, but only highlights and operates on 1.2.3.

For substitution:

:%s/version: \zs[0-9.]\+/3.0.0/g

Replaces only the version number, leaving version: untouched.

Tips

  • Combine with \ze to also limit where the match ends: /start\zsdata\ze end.
  • Useful with cgn for targeted change-and-repeat: search with \zs, then use cgn to change just the match portion, and . to repeat on each occurrence.
  • Works in :global, :vimgrep, gn, and any other context that uses a search pattern.

Next

How do I run the same Ex command in every open tab page at once?