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?

  • 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 '21 at 22:27

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.

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

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.