How to make vim replacement again until there are no more matches?

I have a file with text that looks like

{\o H}{\o e}{\o l}{\o l}{\o o} {\o W}{\o o}{\o r}{\o l}{\o d}

I want the text to look like this

{\o Hello} {\o World}

There are probably several tools that I can use to solve this problem, but I'm trying to use vim lookups. I still have

:%s/{\\o \(.\{-}\)}{\\o \(.\{-}\)}/{\\o \1\2}/g

Templates here

.\{-} match some non greedy characters

{\\o .\{-}}match regex string {\o .*}but not greedy with.*

and \( ... \)creates capture groups for the callback.

When I run this replacement once, I get

{\o He}{\o ll}{\o o} {\o Wo}{\o rl}{\o d}

I can run this command two more times to get

{\o Hell}{\o o} {\o Worl}{\o d}

and then

{\o Hello} {\o World}

, , , vim , . , .

- , , ? , . , - ?

+4
2

subst-replace, \=, :s

:%s/\({\\o \S}\)\+/\='{\o '.substitute(submatch(0), '{\\o \(\S\)}', '\1', 'g').'}'/g

, "", . {\o H}{\o e}{\o l}{\o l}{\o o}, "", submatch(0). {\o } substitute(submatch(0), '{\\o \(\S\)}', '\1', 'g'). Hello. , {\o }.

.:

:h :s
:h sub-replace-expresion
:h substitute(
:h submatch(
:h literal-string
:h /\S

. {\o }. , - .

:%s/{\\o \(.\)}/\1/g
:%s/\S+/{\\o &}/g

, , , , , .

, |:

:%s/{\\o \(.\)}/\1/g|%s/\S+/{\\o &}/g
+4

:

. - , . substitute , . , , .

  • "q" . , .

    qqq
    

    :let @q=""
    

    .

  • . :

    qq
    
  • substitute, .

    :%s/{\\o \(.\{-}\)}{\\o \(.\{-}\)}/{\\o \1\2}/g<cr>@q
    

    @q - . , "q" . ,

  • .

    q@q
    

    , , , .

:

qqqqq:%s/{\\o \(.\{-}\)}{\\o \(.\{-}\)}/{\\o \1\2}/g<cr>@qq@q

5 'q . "q" :

:let @q=':%s/{\\o \(.\{-}\)}{\\o \(.\{-}\)}/{\\o \1\2}/g<cr>@q'

<cr>@q

vim-, .

+4

Source: https://habr.com/ru/post/1652232/


All Articles