Thursday, October 11, 2012

Re: Remove duplicate words or patterns inside lines

On 10/11/12 10:38, tjg wrote:
> Just for my information, and if you have the time, could you
> detail for me the forensics of this not-so-"ugly brute" ?

:%s/\([#@&]\=\<\w\+\>\).\{-}\zs \+[#@&]\@<!\1\>//g

\(...\) capture something of interest which is
[#@&] one of these characters
\= optionally
\< a word-starts-here boundary
\w\+ one or more Word characters
\> the word must end here
.\{-} as little as possible to meet the following
\zs consider the pattern-match starting here
(effects what gets replaced)
<space>\+ one-or-more mandatory spaces before the dupe
[#@&]\@<! assert that we can't mismatch[1] before here
\1 the thing we captured must match here
\> the match must end here too[2]

So it's roughly looking for something of interest followed by stuff,
followed by some spaces, followed by the thing-of-interest again
(and no more). It then replaces the spaces-plus-duplicate-match
with nothing.

Hope that helps make sense of it.

-tim



[1]
this is what prevents it from getting snagged on things like

my text has a &text or @text or #text

from matching



[2]
this prevents partial matches like

this text is textual

where "text" and "textual" might be considered a match





--
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

No comments: