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.

No comments: