Thursday, August 10, 2017

Re: wrapping a series of comma separated words

On Thu, Aug 10, 2017 at 11:57 AM, Chris Lott <chris@chrislott.org> wrote:
I'm constantly needing to wrap a series of comma separated titles with html <i> tags, so this:

    John has published work in foo, fubar, boo review, and many more.

Becomes this

    John has published work in <i>foo</i>, <i>fubar</i>, <i>boo review</i>, and many more.

Right now I'm visually highlighting each title and using emmet to wrap with the tags, but it seems like there must be a shorter way!

The basic idea is to use

:%s#pattern#<i>&</i>#g

I am using '#' instead of the usual '/' so that I do not have to escape '/' in the replacement.  If you can afford the extra character, then this is equivalent:

:%s/pattern/<i>&<\/i>/g
 
Note that '&' in the replacement corresponds to the matched text.  (:help sub-replace-special)

     The question is what pattern to use.  If you use '\k\+\ze,' then it will match a "word" preceding a comma:  in your example, "foo", "fubar", and "review".  The question is how to get "boo review" and not "in foo".  One possibility is to match either '\k\+' before a comma or '[^,]\+' after ", " and before ",", so the full pattern is ',\s*\zs[^,]\+\ze,\|\k\+\ze,' and the full command is

:%s#,\s*\zs[^,]\+\ze,\|\k\+\ze,#<i>&</i>#g

-- 
HTH
Benji Fisher

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment