0

Given a word list, I would like to delete all lines that do not have a specific content.

For illustration, In the following list, I would like to delete all lines that do not end in s.

bird
chairs
dogs
cars
cat
elephant
elephants

After running the macro, the new list should be

chairs
dogs
cars
elephants

Does it matter that I am looking for a single character? If it does, then delete lines not ending in rs which would yield the following new list.

chairs
cars
1
  • 2
    Why use a macro when you have :h :global and :h :vglobal ? Either would work, just depends on which pattern you use. The latter is the easier one for your particular case.
    – B Layer
    Mar 19 at 16:34
4

As BLayer mentions, the idiom here is

:vglobal/pat/delete

Shorthand :v/pat/d. :vglobal (or :global!) acts on lines not matching the pattern, where as :global/:g acts on lines matching the pattern.

1
  • Thank you! This is exactly what I need. Mar 20 at 2:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.