Thursday, May 14, 2020

Re: How to search a match on a line and then make a change on the next line?

On 2020-05-14 13:32, boB Stepp wrote:
>> :g/\(field [[:alnum:]_]*\)address2/+s/pobox/[\1]pobox/
>>
>>you'll see resulting lines like
>>
>> []pobox
>>
>>to show that the contents of \1 are empty.
>
> Hmm. That is what I was speculating -- that "\1" was empty -- and
> you have proven that plus giving me another little trouble shooting
> technique. But an example in the book "Learning the vi and Vim
> Editors, 7th ed." by Arnold Robbins, Elbert Hannah and Linda Lamb
> made me believe what I was trying was doable. In chapter 6:
> Global Replacement on page 82 the authors give this snippet of text:
>
> mgibox routine;
> mgrbox routine;
> mgabox routine;
>
> In this exercise we want to replace "box" with "square". The
> authors give two ways to accomplish this. One of them is:
>
> :g/mg\([ira]]\)box/s//mg\1square/g
>
> Here where the line found is also the line to change (unlike mine
> where I want to change the next line) the "\1" register is
> accessible.

In this case it's not whether the line is the same but that the
pattern is the same. By (re)using the pattern from the :g in the :s
by using an empty pattern here:

s//replacement/flags

it (re)captures the portion captured from the same pattern found in
the :g part of the command. So if you happened to have two adjacent
lines both containing "capture", you could do something like

:g/\(capture\)/+s//replacement with \1 in it/

(which has the "+" relative-offset to the line after the initial
match) to replace the one on the 2nd line with "replacement with
capture in it".

Hopefully that sheds a bit of light on matters?

-tim





--
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200514142007.34ce5a94%40bigbox.attlocal.net.

No comments:

Post a Comment