Tim, but your 4-line example can be solved exactly by the command I previously suggested; for your case it would be just :g/one a/s/a/b/ Mind that it gets only slightly more difficult if the substitution pattern would also be part of the "global" pattern, as if your lines would have one e one e one one e then it would be necessary to better characterize the substitution pattern; in this case, for example, by adding the space to the pattern and replacement. :g/one e/s/ e/ b/ There's yet more complicated patterns thinkable. Solutions can likely be found with such elementary ex-commands without the need to resort to vim-scripting. Janis ________________________________________ Von: vim_use@googlegroups.com <vim_use@googlegroups.com> im Auftrag von Tim Chase <vim@tim.thechases.com> Gesendet: Freitag, 23. Oktober 2026 16:51 An: vim_use@googlegroups.com Betreff: Re: AW: Atomic Substitution. On 2026-10-23 13:46, Janis Papanagnou wrote: >> I woud like to first make ether a range selection or a grep on ines >> and after that substitute them but only if the substitution can take >> place on all itemm. Is that possible? > > Your description is not quite clear to me. - It sounds as if you want > something like I *think* the intent is that the OP only wants to do the replacement if *all* the lines match the problem, so if you had something like one a one a one one a and they want something like :g/one/s/a/b/ but only if *all* the /one/ matches have a substitute that will take place. In my example above, the 3rd one doesn't have an "a" that would be substituted, so it sounds like the OP would like the whole batch to fail. One would likely have to check the count of substitution-lines before and the number of substitution-lines after, and see if they match. Maybe something like this wrapped in a function: let a=0 let b=0 g/pattern1/let a+=1 g/pattern1/g/pattern2/let b+=1 if (a == b) | g/pattern1/s/pattern2/replacement/g | endif For the OP's "range of lines" version, the pattern1 would be just /^/ and the 3 ":g" commands would need the relevant range before them '<,'>g/pattern1/let a+=1 '<,'>g/pattern1/g/pattern2/let b+=1 if (a == b) | '<,'>g/pattern1/s/pattern2/replacement/g | endif I might be misunderstanding, but at least that's how I parse the "only if the substitution can take place on all" :-) -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 visit https://groups.google.com/d/msgid/vim_use/att0fQbUZpyh6kIH%40thechases.com. -- -- 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 visit https://groups.google.com/d/msgid/vim_use/PAXPR05MB8908533F9BDD37696C3F7DD8F3642%40PAXPR05MB8908.eurprd05.prod.outlook.com.
Friday, October 23, 2026
Re: Atomic Substitution.
On 2026-10-23 16:55, Vim Users wrote: > But I have overseen one thing. Lets see I have a block like this: > > 1 > 1 > 2 > 1 > 1 > 1 > 1 > > And I want to substitute all 1 to 3 but only if all lines can be substituted. > > So this should be refuted while > > 1 > 1 > 1 > 1 > 1 > 1 > 1 > > should be substituted. Maybe something like let refrain=0 | exec 'v/pattern/let refrain+=1' | if (refrain == 0) | %s//replacement/g | endif This assumes we shouldn't refrain from doing the substitution, but if any lines in the file don't match, it will set 'refrain' to a non-zero value. The 'if' block then checks the value to see if it should refrain from doing the substution, and only proceed with the substitution if all the lines matched the /pattern/ in question. Making it work for arbitrary ranges or grep-subsets of lines gets a little trickier, but doable. -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 visit https://groups.google.com/d/msgid/vim_use/att6Zo3U865p7NW-%40thechases.com.
Re: Atomic Substitution.
But I have overseen one thing. Lets see I have a block like this: 1 1 2 1 1 1 1 And I want to substitute all 1 to 3 but only if all lines can be substituted. So this should be refuted while 1 1 1 1 1 1 1 should be substituted. On 2026-10-23 13:46:11, Janis Papanagnou wrote: > Your description is not quite clear to me. - It sounds as if you want something like > > :g/pattern/s/pattern/replacement/g > > or > > :g/grep-pattern/s/other-pattern/replacement/g > > i.e. use of the "global" command 'g' to select all existing matching lines, > and only on those do another replacement on the second pattern expression. > > For example to replace the word "world" by "vim" only in lines where > "hello world" is seen (but not, e.g., in lines where "world wide web" is written) > > :g/hello world/s/world/vim/g > > If that's not what you want, please clarify. > > Janis > > ________________________________________ > Von: 'Frank Schwidom' via vim_use <vim_use@googlegroups.com> > Gesendet: Freitag, 23. Oktober 2026 15:33 > An: vim_use@googlegroups.com > Betreff: Atomic Substitution. > > Hi. > > I woud like to first make ether a range selection or a grep on ines and after that substitute them but only if the substitution can take place on all itemm. Is that possible? > > Thanks in Advance. > > -- > -- > 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 visit https://groups.google.com/d/msgid/vim_use/attiQu6zokeSUzll%40debian64. > > -- > -- > 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 visit https://groups.google.com/d/msgid/vim_use/PAXPR05MB890835EB32D675404728FFDEF3642%40PAXPR05MB8908.eurprd05.prod.outlook.com. > -- -- 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 visit https://groups.google.com/d/msgid/vim_use/att1TWog2NG6DgMo%40debian64.
Re: AW: Atomic Substitution.
On 2026-10-23 13:46, Janis Papanagnou wrote: >> I woud like to first make ether a range selection or a grep on ines >> and after that substitute them but only if the substitution can take >> place on all itemm. Is that possible? > > Your description is not quite clear to me. - It sounds as if you want > something like I *think* the intent is that the OP only wants to do the replacement if *all* the lines match the problem, so if you had something like one a one a one one a and they want something like :g/one/s/a/b/ but only if *all* the /one/ matches have a substitute that will take place. In my example above, the 3rd one doesn't have an "a" that would be substituted, so it sounds like the OP would like the whole batch to fail. One would likely have to check the count of substitution-lines before and the number of substitution-lines after, and see if they match. Maybe something like this wrapped in a function: let a=0 let b=0 g/pattern1/let a+=1 g/pattern1/g/pattern2/let b+=1 if (a == b) | g/pattern1/s/pattern2/replacement/g | endif For the OP's "range of lines" version, the pattern1 would be just /^/ and the 3 ":g" commands would need the relevant range before them '<,'>g/pattern1/let a+=1 '<,'>g/pattern1/g/pattern2/let b+=1 if (a == b) | '<,'>g/pattern1/s/pattern2/replacement/g | endif I might be misunderstanding, but at least that's how I parse the "only if the substitution can take place on all" :-) -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 visit https://groups.google.com/d/msgid/vim_use/att0fQbUZpyh6kIH%40thechases.com.
Re: Atomic Substitution.
I see, it already seems to work, thanks. On 2026-10-23 13:46:11, Janis Papanagnou wrote: > Your description is not quite clear to me. - It sounds as if you want something like > > :g/pattern/s/pattern/replacement/g > > or > > :g/grep-pattern/s/other-pattern/replacement/g > > i.e. use of the "global" command 'g' to select all existing matching lines, > and only on those do another replacement on the second pattern expression. > > For example to replace the word "world" by "vim" only in lines where > "hello world" is seen (but not, e.g., in lines where "world wide web" is written) > > :g/hello world/s/world/vim/g > > If that's not what you want, please clarify. > > Janis > > ________________________________________ > Von: 'Frank Schwidom' via vim_use <vim_use@googlegroups.com> > Gesendet: Freitag, 23. Oktober 2026 15:33 > An: vim_use@googlegroups.com > Betreff: Atomic Substitution. > > Hi. > > I woud like to first make ether a range selection or a grep on ines and after that substitute them but only if the substitution can take place on all itemm. Is that possible? > > Thanks in Advance. > > -- > -- > 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 visit https://groups.google.com/d/msgid/vim_use/attiQu6zokeSUzll%40debian64. > > -- > -- > 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 visit https://groups.google.com/d/msgid/vim_use/PAXPR05MB890835EB32D675404728FFDEF3642%40PAXPR05MB8908.eurprd05.prod.outlook.com. > -- -- 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 visit https://groups.google.com/d/msgid/vim_use/att0HG0tOJXe6AEV%40debian64.
AW: Atomic Substitution.
Your description is not quite clear to me. - It sounds as if you want something like :g/pattern/s/pattern/replacement/g or :g/grep-pattern/s/other-pattern/replacement/g i.e. use of the "global" command 'g' to select all existing matching lines, and only on those do another replacement on the second pattern expression. For example to replace the word "world" by "vim" only in lines where "hello world" is seen (but not, e.g., in lines where "world wide web" is written) :g/hello world/s/world/vim/g If that's not what you want, please clarify. Janis ________________________________________ Von: 'Frank Schwidom' via vim_use <vim_use@googlegroups.com> Gesendet: Freitag, 23. Oktober 2026 15:33 An: vim_use@googlegroups.com Betreff: Atomic Substitution. Hi. I woud like to first make ether a range selection or a grep on ines and after that substitute them but only if the substitution can take place on all itemm. Is that possible? Thanks in Advance. -- -- 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 visit https://groups.google.com/d/msgid/vim_use/attiQu6zokeSUzll%40debian64. -- -- 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 visit https://groups.google.com/d/msgid/vim_use/PAXPR05MB890835EB32D675404728FFDEF3642%40PAXPR05MB8908.eurprd05.prod.outlook.com.
Atomic Substitution.
Hi. I woud like to first make ether a range selection or a grep on ines and after that substitute them but only if the substitution can take place on all itemm. Is that possible? Thanks in Advance. -- -- 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 visit https://groups.google.com/d/msgid/vim_use/attiQu6zokeSUzll%40debian64.