On 9/19/20 9:27 AM, Tim Chase wrote:
The only gotcha with this is that a paragraph must have a blank line before & after it. I've gotten stung doing this exact format of command because the match was in the first/last paragraph of the document. So you need to also ("\|") accept a match of the beginning of the file ("\%^") and end of the file ("\%$")

   :g/aaa/?^$\|\%^?,/^$\|\%$/s:aaa:bbb:g

As Sven says, it's ugly. But it's doable and fairly concise in what you're describing:

   Find all the "aaa"
   Starting there, search backwards to the previous blank line or BOF
   From there, search forwards to the next blank line or EOF
   Substitute "aaa" → "bbb" on all the lines of that range

Very interesting.

Thank you for that explanation Tim.

Alternatively, you could hack it with normal mode, something like

   :g/aaa/norm vip:s//bbb^V^M

where ^V^M is a control-V followed by a control-M (which makes this nasty to put in a mapping or vimrc). This works because vim knows that a "paragraph" is bounded by empty lines *or* BOF/EOF and does the ugly work for us.

ACK

Would it be worth while to include word boundaries \< and \> so that you don't accidentally get a sub-string?

    :g/aaa/?^$\|\%^?,/^$\|\%$/s:\<aaa\>:bbb:g

Also, is there any way to group the first "aaa" so that it could be (back) referenced in the substitution? }:-)

I can't imagine doing this in pretty much any other text editor without typing a LOT more.

~chuckle~



--
Grant. . . .
unix || die

--
--
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_use/dd4e9977-15d9-12d7-ee94-6d1faf50b553%40spamtrap.tnetconsulting.net.

Reply via email to