0

I have the following code from lines 6-20

            <div>
                1
            </div>
            <div>
                2
            </div>
            <div>
                3
            </div>
            <div>
                4
            </div>
            <div>
                5
            </div>

I want to replace all <div> {anything here} </div> with none.

I tried the following

  1. :%s/<div>\W+.\W+div>//
  2. :6,20s/<div>\W+.\W+div>//
  3. :6,20s/<div>\W+.\W+div>/\0/
  4. :%s/<div>\W+.\W+div>/\0/

Nothing works. It says Pattern not found

But when I search with this /<div>\W+.\W+div>

It shows that there are 5 matches

How to achieve this substitution?

1
  • Welcome to Vi and Vim! This regexp (all of them really) isn't really valid in Vim, since the + is not a valid metacharacter and will only match a literal +, so I'm not sure how you're getting 5 matches... This regexp should work: /<div>\_.\{-}<\/div>/, can you check whether it works for you?
    – filbranden
    May 2, 2021 at 19:57

1 Answer 1

2

I'm not entirely sure what's wrong with the regex, though filbranden has some thoughts in the comments.

To solve the issue, though, I would do something a bit different: :global/<div>/normal! dat (delete the tag on the lines with an opening div tag).

2
  • I think dst is a vim-surround extension. The built-in dat does work for that, though.
    – filbranden
    May 2, 2021 at 21:08
  • 1
    Oh, crud, I forgot they wanted to delete everything between the tags, too /facepalm @filbranden
    – D. Ben Knoble
    May 2, 2021 at 21:49

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.