Can I include /range/ in substitute pattern?

45 views
Skip to first unread message

Pablo Contreras

unread,
Mar 14, 2017, 1:28:23 PM3/14/17
to vim_use
Hi All,

I'm wondering if you gentlemen will be able to help me here.

Let's say I have

=====
john [<--current-line]

sells kites
=====

if I want to add bikes to John's sales I do

:/sells kites/s//\0 and bikes/

which is great because it saves me typing 'sells kites' in the pattern

the inclusion of just s//.......

tells Vim 'use the last pattern found as part of the search pattern.

hence I get:

=====
john

sells kites and bikes [<--current-line]
=====

how about this:

=====
john [<--current-line]

sells kites and bikes
=====

I decide I don't like the order in which John is selling. I want to change this order:

so I could do:

:/kites/s/\(kites\)\(.*\)\(bikes\)/\3\2\1/

then surely:

=====
john

sells bikes and kites [<--current-line]
=====

result. But I'm just wondering is there a shorthand way of re-type 'kites' in my search pattern.

so far I've tried:

:/kites/s/\(~\)\(.*\)\(bikes\)/\3\2\1/

which doesn't work because you can only use ~ on the right hand side of :subst

similarly

:/kites/s/\(&\)\(.*\)\(bikes\)/\3\2\1/

because & has to be on the right.

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.

jaky...@theanythingbox.com

unread,
Mar 21, 2017, 1:31:16 AM3/21/17
to vim_use
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.


Pablo Contreras

unread,
Mar 21, 2017, 8:44:56 AM3/21/17
to vim...@googlegroups.com
Hi Jakykong,

love the name :)

(I was a big Kong fan in my time)

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.
> --
> --
> 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+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

jaky...@theanythingbox.com

unread,
Mar 21, 2017, 10:14:29 AM3/21/17
to vim_use
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

> \_.\{-}+


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!

Pablo Contreras

unread,
Mar 21, 2017, 1:24:28 PM3/21/17
to vim...@googlegroups.com
Lol serious??? Donkey Kong is my all time fave!! Cheers, JakyKong.


> > 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 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.
Reply all
Reply to author
Forward
0 new messages