Nikolaos A. Patsopoulos wrote:
Tim Chase wrote:
I have a text that has many occurrences of a pattern . I want to delete every consecutive occurrence, e.g.

Pattern Pattern other text Pattern Pattern Pattern Pattern other text Pattern Pattern Pattern
should look like this:
Pattern other text Pattern other text Pattern

I've used:

:%s/\(Pattern\s\+\)\(Pattern\)/\1/g

but have to run this more than once with: %&g to result the wanted text.

Can I do this with one command only? If not can I write a while function?:

You seem to be close.  The following did it for me,

  :%s/\(Pattern\)\(\s\+Pattern\)\+/\1/g

or, if you're lazy,

  :%s/\(Pattern\)\(\s\+\1\)\+/\1/g

(no need to type the Pattern a 2nd time)

HTH,

-tim



I tried Jorgen' s code (all possible ways) but I still had to run the command more than once. Tim's worked great. I forgot to mention that pattern is a regexp, so Tony's couldn't be tested. Thank you all for your time.

By the way, can someone explain if I could the while function???

Thanks,

Nikos



Of course you could:

    function RemoveDuplicates(pattern) range
        exe a:firstline
        while search(a:pattern . a:pattern, 'c', a:lastline)
            exe 's/\(' . a:pattern . '\)' . a:pattern . '/\1'
        endwhile
    endfunction
    command -range=% -bar -nargs=0 ScrapDup call RemoveDuplicates(<q-args>)

The above will remove one duplicate at each iteration and exit when there are none left. IMHO a single substitute is more elegant though.

Best regards,
Tony.
--
It is against the law for a monster to enter the corporate limits of
Urbana, Illinois.

Reply via email to