On 2020-09-19 14:08, Sven Guckes wrote:
>> I need to replace string "aaa" with "bbb"
>> only in paragraphs that starts with "XXX".
>
> :g/^XXX/?^$?,/^$/s:aaa:bbb:g
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
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.
I can't imagine doing this in pretty much any other text editor
without typing a LOT more.
-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/20200919102732.2b1c5dbb%40bigbox.attlocal.net.
Saturday, September 19, 2020
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment