March 22, 2026

Vim: replace only part of a matched regex

There is a number 2 here and a dot there:. 
1. but we only want to match 1. not 2


:%s/[[:digit:]]\zs\./:/gc 

matches 1. (but 2) and replaces . with : (but the dot after there:.).

  • % search the whole file
  • s/{pattern}/{string}/ to substitute a pattern by a string.
  • :digit: matches a digit.
  • backslash the dot to match the dot instead of matching any character
  • \zs marks the start of string that should be substituted
  • g replaces all occurrences in a line
  • c confirms each occurrence

Try:

  • \ze to set the end

personal computing command-line interface (cli) gnu linux trisquel shell literacy wiki vim

No affiliate links, no analytics, no tracking, no cookies. This work © 2016-2025 by yctct is licensed under CC BY-SA 4.0 .   about me   contact me   all entries & tags   FAQ   GPG public key

GPG fingerprint: 2E0F FB60 7FEF 11D0 FB45 4DDC E979 E52A 7036 7A88