how to exactily repeat the n matched pattern in result string

Viewed 227

How to exactly repeat the n matched pattern in result string?

Example if I have the folowing text:

++ '[' -f /etc/bashrc ']'
++ . /etc/bashrc
+++ '[' '[\u@\h \W]\$ ' ']'
+++ '[' -z 'printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"' ']'
+++ shopt -s checkwinsize
+++ '[' '[\u@\h \W]\$ ' = '\s-\v\$ ' ']'
+++ shopt -q login_shell
+++ '[' 506 -gt 199 ']'
++++ id -gn

Now I want to substitute every '+' for 3 spaces, but it can only happen at the begining of the pattern. I would use :<range>s/^<pattern> :%s/+/ /g, but if it there were a '+' in the rest of the text I would simply mess it up.

The question: How to match every + at begining and repeat the same count of found + in the result string? expected:

^   ++$  -> ^         $
^   +++$ -> ^            $
^   +$   -> ^      $

Thanks

3 Answers
Related