Search code examples
searchvimreplace

Delete end of line starting at second occurence of search pattern in vim


I have a text file in which many lines contain twice the symbol =, as in:

Animals:
clown=fish=vertebrate
cow=mammal=vertebrate
bug=insect=invertebrate
slug==snail
etc

I want to delete everything that is after the second = on each line only if the two = are not together, resulting in:

Animals:
clown=fish
cow=mammal
bug=insect
slug==snail
etc

How I can I do this? I guess search for the second occurence of =, then select all results, then select until the end of line, then delete, but most of these steps I couldn't find a easy way to do.


Solution

  • This should be enough:

    %s/=[^=]\+\zs=.*//
    

    The interesting part is \zs. Look for it in the docs via :help \zs.

    Beside that, I'm matching an equal sign (the first =) followed by 1 or more (\+) characters other than the equal sign ([^=]), followed by another equal sign.