regex in vim: how can I replace `+` expanded characters

I am trying to replace all leading occurrences of = with #.

=
lorem
==
ipsum
====
foo=bar

Ususally I would do something like this.

:%s/^====/####/g
:%s/^===/###/g
:%s/^==/##/g
:%s/^=/#/g

Not nice, but gets the job done.

My startng point is :%s/^\(=+\)/#/g, but this would turn all leading occurences of = into a single #.

Two appoaches I have in mind, but don’t know how to implement are:

  • have all occurences of = replaced within the capture group.
  • determine the length of the capture group and .

Is there a way to solve this in a regex vim one-liner?

Read more here: Source link