wrapping a series of comma separated words

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

wrapping a series of comma separated words

Chris Lott
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!

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: wrapping a series of comma separated words

Benji Fisher-3
On Thu, Aug 10, 2017 at 11:57 AM, Chris Lott <[hidden email]> 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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: wrapping a series of comma separated words

Lifepillar
In reply to this post by Chris Lott
On 10/08/2017 18:57, Chris Lott 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!

I have never used emmet, but I'd use Tim Pope's Surround plugin for
that. Move at the beginning of the text to be wrapped, then type

yst,ti>

Explanation:

ys = invoke "yank surround"
t, = till the next comma
t  = wrap with a tag
i>  = the <i> tag

If you also install the Repeat plugin, the action will be
dot-repeatable. So, typing W. repeatedly will wrap the subsequent
words. Of course, if you find yourself doing this often, you might
define a mapping for the sequence above.

Enjoy,
Life



--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: wrapping a series of comma separated words

Ben Fritz
In reply to this post by Chris Lott
On Thursday, August 10, 2017 at 10:57:27 AM UTC-5, Chris Lott 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!

In general I find working with such lists easier with this pull request compiled in:

https://github.com/vim/vim/pull/958

I think in this specific case it would possibly only help a small amount, as I'd still probably visually select and then use the surround plugin on the visual selection.

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: wrapping a series of comma separated words

'Jürgen Krämer' via vim_use
Am 11.08.2017 um 21:08 schrieb Ben Fritz:

>
> On Thursday, August 10, 2017 at 10:57:27 AM UTC-5, Chris Lott 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!
>
> In general I find working with such lists easier with this pull
> request compiled in:
>
> https://github.com/vim/vim/pull/958
>
> I think in this specific case it would possibly only help a small
> amount, as I'd still probably visually select and then use the
> surround plugin on the visual selection.

I've also added a script for this purpose:
     https://github.com/Houl/charobj-vim

In the example, with surround.vim (and after setting up keys), you type once
     ysim,<i>
to make the first replacement and then go the next location (eg with mouse click)
and hit the dot key to repeat.

charobj still needs fine-tuning (ie options), because with
     foo, fubar, boo review
the result will be
<i>    foo</i>,<i> fubar</i>,<i> boo review</i>
(would be more useful to skip surrounding whitespace -- but maybe not always).

And it still assumes selection=inclusive.

Otherwise a count works and dot-repetitions works.

And the region is always within a single line, to make extending the region
to begin/end-of-line work.  Maybe this will change too (with option).

--
Andy

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: wrapping a series of comma separated words

'Jürgen Krämer' via vim_use
Am 14.08.2017 um 17:06 schrieb 'Andy Wokula' via vim_use:

> Am 11.08.2017 um 21:08 schrieb Ben Fritz:
>>
>> On Thursday, August 10, 2017 at 10:57:27 AM UTC-5, Chris Lott 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!
>>
>> In general I find working with such lists easier with this pull
>> request compiled in:
>>
>> https://github.com/vim/vim/pull/958
>>
>> I think in this specific case it would possibly only help a small
>> amount, as I'd still probably visually select and then use the
>> surround plugin on the visual selection.
>
> I've also added a script for this purpose:
>      https://github.com/Houl/charobj-vim
>
> In the example, with surround.vim (and after setting up keys), you type once
>      ysim,<i>
> to make the first replacement and then go the next location (eg with mouse click)
> and hit the dot key to repeat.
>
> charobj still needs fine-tuning (ie options), because with
>      foo, fubar, boo review
> the result will be
> <i>    foo</i>,<i> fubar</i>,<i> boo review</i>
> (would be more useful to skip surrounding whitespace -- but maybe not always).
>
> And it still assumes selection=inclusive.
>
> Otherwise a count works and dot-repetitions works.
>
> And the region is always within a single line, to make extending the region
> to begin/end-of-line work.  Maybe this will change too (with option).

There is also a very elaborate plugin (it creates a lot of mappings):
     https://github.com/wellle/targets.vim

It does provide mappings for a comma-separated list, and the inner whitespace
trimmed part can be selected with I, :
     ysI,<i>

but it requires commas before and after, not sure how to make it extend the region
to begin-(or end)-of-line.

--
Andy

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...