0

I have documents with groups of <hr/> tags:

<p>stuff</p>
<hr/>
<hr/>
<hr/>
<p>stuff</p>

Grateful for tip how to replace with a single instance of this tag in Vim.

2
  • And you didn't talk about any attempt. Commented May 1, 2017 at 15:26
  • Top marks for observation Commented May 1, 2017 at 15:34

3 Answers 3

3

You can make <hr/>\n a group and search for multiples of it, replacing with a single one. Also note that in Vim you can use different delimiters, which is specially helpful if you're working with slashes for example. And you don't need to close the substitute command if you don't have flags.

:%s#\(<hr/>\n\)\+#\1

With \v to enable very magic, even more escaping is avoided. However the < and > will be treated as special word boundaries. So you have to escape them instead.

:%s#\v(\<hr/\>\n)+#\1

And of course, if the only duplicated lines in your file are those tags, this is enough as well:

:%!uniq
Sign up to request clarification or add additional context in comments.

Comments

1

You can use this search-replace in vim:

:%s/<hr\/>\n\(<hr\/>\n\)\+/\1/

<hr\/>\n\(<hr\/>\n\)\+ will find 2 or more lines containing <hr/> and we replace it using \1 which is <hr/>\n.

4 Comments

Nice, thanks. Do parentheses replace square brackets in Vim?
Actually sorry I'm getting E488: Trailing characters
Re square brackets no it's just I was trying earlier to achieve this with [<hr\/>] but always get Pattern not found
That works ta. I just got :%s/<hr\/>\(\n<hr\/>\)\+/<hr\/>/r to work also. Super helpful thanks
0

Also works:

:%s/\(<hr\/>\n\)\+/<hr\/>\r

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.