3

I'm aware that :g/<body/ .,/body>/d can be used to delete everything within the multi-line selection betweeh <body and body>.

I'm also aware that :g!/<pattern>/d can be used to delete all lines not matching the pattern.

How do I combine these 2 to delete everything except the the multi-line selection?

1
  • This will be very complex to be done in Vim. You can use the decorare, sort, undecorate pattern to achive this. But I doubt this is easily doable with a vim oneliner Nov 5, 2021 at 22:27

1 Answer 1

3

Best to delegate that task to Sed. Any of these work:

:%!sed '/<body/,/body>/\!d'
:%!sed -n '/<body/,/body>/p'

Note that the exclamation mark needs to be escaped lest Vim itself does something with it. If the regex gets fairly complicated I suggest running Sed from the shell because it might be a pain escaping Vim meta-characters.

1
  • You could maybe combine execute with shellescape or similar
    – D. Ben Knoble
    Nov 5, 2021 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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