Tuesday, March 21, 2017

Re: Can I include /range/ in substitute pattern?

On Tuesday, March 21, 2017 at 5:44:56 AM UTC-7, Pablo Contreras wrote:
> Hi Jakykong,
>
> love the name :)
>
> (I was a big Kong fan in my time)

Oddly enough, I think in nearly 15 years of using this moniker, you're the first to notice that connection! :)

>
> I think I wasn't explaining myself clearly.
>
> Also I was a bit confused.
>
> What I was trying to do was this: (AutoIt3 BASIC)
>
> =====
> Dim $K_MAINGUIWIDTH
>
> $K_MAINGUIWIDTH = 320
>
> ..
>
> GUICreate( "hello world!", 200, 100 ........
> =====
> say I'm interested in that string $K_MAINGUIWIDTH right at the top there
>
> it's a GUI in AutoIt3
>
> I was looking change the GUICreate to use $K_MAINGUIWIDTH instead of a set 200
>
> I got confused because /RANGE/ and s// are different. The
> /RANGE/s//NEW TEXT/ is a special case where Vim lets you use the
> previously used pattern as the search pattern in the s//
>
> In fact my replacement can be achieved in s// exclusively
>
> you do:
> 1s/\(\_.\{-}\)\($K_MAINGUIWIDTH\)\(\_.\{-}\)200/\1\2\3\2/
>
> ask Vim to go skip through the entire fie
> \_.\{-}
> until it matches $K_MAINGUIWIDTH
> then keep going
> \_.\{-}+
> until you match 200
>
> then your substitute string you just put those things back
> \1\2\3
> then add your desired string
> \2
>
> Works for me!
>
> Cheers, Pablo.
>
>
>
> On 3/21/17, jakykong@theanythingbox.com <jakykong@theanythingbox.com> wrote:
> > On Tuesday, March 14, 2017 at 10:28:23 AM UTC-7, Pablo Contreras wrote:
> >
> >>
> >> my previous trick:
> >>
> >> :/kites/s//...... /
> >>
> >> doesn't work here because I'm trying to match both 'kites' and 'and
> >> bikes'
> >>
> >> Is this too ambitious?
> >>
> >> Thanks in advance! Pablo.
> >
> > If you had searched /kites/ first, I'd suggest <C-r>/ to paste the search
> > register, but clearly that isn't what you're after. If you do it that way,
> >
> > /kites
> > :s/\(<C-r>/\)\(.*\)\(bikes\)/\3\2\1/
> >
> > would shorthand typing kites (Just in case I'm not being clear about the key
> > sequence here, <C-r>/ means ctrl+r then '/', and these are two commands, one
> > to search and one to substitute)
> >
> > That works for your example on my machine.
> >
> >
> > --
> > --
> > 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 a topic in the
> > Google Groups "vim_use" group.
> > To unsubscribe from this topic, visit
> > https://groups.google.com/d/topic/vim_use/wKhhteq8SKI/unsubscribe.
> > To unsubscribe from this group and all its topics, send an email to
> > vim_use+unsubscribe@googlegroups.com.
> > For more options, visit https://groups.google.com/d/optout.
> >


I think you can simplify this further. First, 1s is unnecessary, s on its own defaults to replacing only the first occurrence of a pattern (you need the /g flag to change that behavior) and like most commands operates on the current line only by default (you need a range larger than 1 to change that behavior).

The movements you're doing may also be a bit overkill, depending on the situation precisely; if you simply match on GUICreate instead of the 200 it will probably narrow the results to one of a few calls (since GUICreate isn't something I would expect to be called massively often) - then just use the /gc flag to confirm which ones need substitution over the whole file (or most of it).

Finally, \zs can simplify your substitution. It lets you define where the match actually starts *after* some other matching. So, Give this a go:

%s/^GUICreate.*, \zs200/$K_MAINGUIWIDTH/gc

Just hit 'y' or 'n' a few times as relevant.


Hope it helps!

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