4

I tried to create a mapping in order to enclose the visually selected paragraphs in

tags:

vnoremap <silent> _p <Esc>`>a</p><Esc>`<i<p><Esc>:'<,'>s/\n\{2,}/<\/p>\r\r<p>/g<CR>

Unfortunately, this mapping always adds an unwanted </p>\r\r<p> at the end of the selection (i.e. behind the </p> added behind the last character in step 1).

The selection does not end with a double linebreak, so the pattern should not match. What am I doing wrong?

From Answer

Thanks, Romain – I have the surround plugin installed, but it's not helpful when the selected block contains multiple paragraphs. The above mapping is used for this case, not to enclose a single paragraph.

My mapping first adds </p> to the end, then <p> to the beginning of the selection, afterwards it should replace each occurrence of two or more linebreaks with the appropriate combination of closing and opening tag.

I still do not understand why the replacement pattern is applied to the end of the selection, which never consists of a double linebreak.

1
  • Please review the FAQ. This is not a forum. Please use comments to reply to answers or alternatively update your question with further information. Sep 23, 2011 at 11:40

3 Answers 3

0

It's not really unfortunate since that's what you ask explicitely with the:'<,'>s/\n\{2,}/<\/p>\r\r<p>/g<CR> part.

The visually selected block is perfectly enclosed in <p></p> right before the :.

Do you know about the surround plugin? It solves this ans other problems beautifully. This plugin is a must when editing HTML.

0

After reading the documentation on \%V more closely, I can answer the question myself:

To make sure the whole pattern is inside the Visual area put \%V at the start and end of the pattern

So my mapping needed to be changed to

vnoremap <silent> _p <Esc>`>a</p><Esc>`<i<p><Esc>:'<,'>s/\%V\n\{2,}\%V/<\/p>\r\r<p>/ge<CR>:nohl<CR>

I apologize for improper research prior to posting my question.

0

I guess this 4 regex could looks like a little complicated, but works.

:'<,'>s/\(\(\n\)\+\)/*\1*/g |:'<,'>s/\*\(.*\)\*/\1/g | :'<,'>s/\(^.*\)\*/\<tag\>\1/g | :'<,'>s/\n<tag>\(.*\)/\r\1\<\\tag\>/g

Only limitations found at last 2 lines of a text, or when it's used * character must be replace to another one in these regex'.

3
  • This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review Jan 3, 2023 at 2:22
  • I have exhaustively test those regex, still working in Vim for Linux and Mac. Jan 3, 2023 at 13:35
  • Alan, that was from the answer reviews. There are only certain actions available to the reviewers. In this case, it isn't because the answer is incorrect. It's that you can improve your answer to make it a good one by explaining your answer. Even quoting a reference if applicable. Jan 3, 2023 at 22:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .