Match context, replace only the value
:s/id=\zs\d\+\ze;/42/
Example
Normal mode. Line: id=17; other=99;.
id=17; other=99;- :s/id=\zs\d\+\ze;/42/<CR> Replace digits between id= and ; · id=42; other=99;
id=42; other=99;Details & Vim help
Use \zs and \ze to set match boundaries inside a larger pattern.
Change a value while leaving its identifying prefix and suffix intact.
The surrounding text must still match. The example uses Vim’s default magic syntax, where \d matches a digit and \+ means one or more.
Try it in Vim
- Run :s/id=\zs\d\+\ze;/42/<CR>.
- The pattern requires id= before the digits and ; afterward.
- Only the digits between \zs and \ze are replaced.