tim_helck
tim_helck

Reputation: 43

VIM -- can I use Global Command to filter on two different conditions?

I have a file that looks like this:

    'description': 'US RTS'
    'sgv:activationStatus': true
    'sgv:assignedSegments': ['segments:16c...
    'sgv:created': 2019-09-25T12:31:36.088-04:00
    'sgv:createdBy': '203796'
    'sgv:lastModified': 2020-08-11T15:19:39.230-04:00
...

I want to remove all the lines that contain 'sgv:' EXCEPT for the ones that also contain 'assignedSegments'. I can do it like this:

:g!/assignedSegments/s/sgv:.*/& DELETE/
:g/DELETE/d

But I think that there may be a more elegant way to do it in a one-liner. Any suggestions?

Upvotes: 3

Views: 157

Answers (1)

William Pursell
William Pursell

Reputation: 212356

This seems to be what you want:

:g/sgv:/v/assignedSegments/d

From help :g

                                                                E147
When the command is used recursively, it only works on one line.  Giving a
range is then not allowed. This is useful to find all lines that match a
pattern and do not match another pattern:
        :g/found/v/notfound/{cmd}
This first finds all lines containing "found", but only executes {cmd} when
there is no match for "notfound".

Upvotes: 6

Related Questions