g[lobal] with s[ubstitute]

43 views
Skip to first unread message

Bee

unread,
Jun 16, 2013, 7:12:54 PM6/16/13
to vim_use
g[lobal] with s[ubstitute]

I used the following to replace ';' with '; ' only in lines with '}'

It works, but if I want to stop early using 'q' it does not quit, I
must use ^C.

Also if I use 'a' to replace all, search/replace does not exit, again
needing ^C.

:g/}/s/;/; /gc

Another solution?

Bill

Ben Fritz

unread,
Jun 16, 2013, 8:53:17 PM6/16/13
to vim...@googlegroups.com

This will apply a 1-line substitute to every line containing a }.

I would find a way to include the } in the pattern and use a full-file range %.

Something like:

:%s#.*}\&\(.*\);#; #gc

Christian Brabandt

unread,
Jun 18, 2013, 3:46:54 AM6/18/13
to vim...@googlegroups.com, vim...@googlegroups.com
That is the way the :g command works. :g marks each relevant line
and then runs the specified command for each marked line. That means
for every line matching } it runs a "new" :s command with confirmation.

So basically, you need to "quit"/"all" for each line again.

Here is a patch, that makes "q" abort for all invocations of :s commands
commands when used in conjuction with :g and 'a' work across all
invocations of ':s' commands when using :g


regards,
Christian
global_sub_confirm.diff

Tim Chase

unread,
Jun 18, 2013, 9:44:33 AM6/18/13
to vim...@googlegroups.com
On 2013-06-18 09:46, Christian Brabandt wrote:
> On Mon, June 17, 2013 01:12, Bee wrote:
> > Also if I use 'a' to replace all, search/replace does not exit,
> > again needing ^C.
> >
> > :g/}/s/;/; /gc
>
> So basically, you need to "quit"/"all" for each line again.
>
> Here is a patch, that makes "q" abort for all invocations of :s
> commands commands when used in conjuction with :g and 'a' work
> across all invocations of ':s' commands when using :g

I'm not sure Bram would except this, as it does break traditional
behavior. Granted, *MOST* of the time, your patch does what I want.
However, there have been times that I've wanted to have selective
control for a given block:

:g/Henry/'{;'}s/William/Mary/gc

where I want to review whether this particular paragraph containing
"Henry" is one in which I want to do the substitutions, in which case
I either do or don't want to do all the substitutions in the
paragraph. Your patch breaks that.

I think I'd prefer it as an extra response to the confirmation:

:g/foo/s/bar/baz/gc
...
replace with baz (y/n/a/A/q/Q/l/^E/^Y)?

where "Q" would quit globally like your patch does while "q" would
just quit for this invocation of :s and "A" would answer "All"
globally, while the "a" would do all of the replacements for the
current invocation. Ideally, this prompt would be dynamically created
so that the "A" and "Q" options only show up in the event we're
nested inside a :g command; if not inside a :g command then the
prompt remains as it currently is. From my quick testing here, Vim
ignores the capital letters when replying to this question, so it's
not like the new uses would mask behavior someone might have been
using.

-tim




Ben Fritz

unread,
Jun 18, 2013, 10:35:23 AM6/18/13
to vim...@googlegroups.com
On Tuesday, June 18, 2013 8:44:33 AM UTC-5, Tim Chase wrote:
> I think I'd prefer it as an extra response to the confirmation:
>
>
>
> :g/foo/s/bar/baz/gc
>
> ...
>
> replace with baz (y/n/a/A/q/Q/l/^E/^Y)?
>
>
>
> where "Q" would quit globally like your patch does while "q" would
>
> just quit for this invocation of :s and "A" would answer "All"
>
> globally, while the "a" would do all of the replacements for the
>
> current invocation.

That would be much nicer (also for the current options) if the displayed text gave some hint what each choice will actually do.

I agree with not getting rid of the old behavior.

Tim Chase

unread,
Jun 18, 2013, 10:47:16 AM6/18/13
to vim...@googlegroups.com, fritzo...@gmail.com
On 2013-06-18 07:35, Ben Fritz wrote:
> On Tuesday, June 18, 2013 8:44:33 AM UTC-5, Tim Chase wrote:
> > I think I'd prefer it as an extra response to the confirmation:
> >
> > :g/foo/s/bar/baz/gc
> > ...
> > replace with baz (y/n/a/A/q/Q/l/^E/^Y)?
>
> That would be much nicer (also for the current options) if the
> displayed text gave some hint what each choice will actually do.

I've seen other programs that use similar prompts adding a "?"
option that will elaborate on what the various choices do (e.g. "git
add -p") right at the prompt. I've used vim long enough to know what
that prompt means without looking it up (well, except for "l" which I
could guess, but don't use enough to remember off the top of my
head), but I agree that a simple

replace with $REPLACEMENT (y/n/a/q/l/^E/^Y/?)?

might make it easier to make the decision mid-replacement rather than
needing to abort, possibly undo multiple changes, and then visit

:help s_flags

to learn what they mean.

-tim



Bee

unread,
Jun 18, 2013, 11:44:29 AM6/18/13
to vim_use
On Jun 18, 6:44 am, Tim Chase<v...@tim.thechases.com> wrote:
>   :g/foo/s/bar/baz/gc
>   ...
>   replace with baz (y/n/a/A/q/Q/l/^E/^Y)?

Instinctively when a or q did not work,
I tried A and Q thinking that must mean 'really' do...

I hope this gets accepted.

Bill

Christian Brabandt

unread,
Jun 18, 2013, 2:36:05 PM6/18/13
to vim...@googlegroups.com, vim-dev Mailingliste
Hi Tim!
Attached is an updated patch, implementing your suggestions, as well as
a hit '?' to show a help banner.

Bram, if this gets merged, I think it makes sense to also merge the 'u'
flag for undo the last substitution (that I have posted a while ago).

regards,
Christian
global_subst_confirm_msg.diff

Bram Moolenaar

unread,
Jun 18, 2013, 4:10:46 PM6/18/13
to Christian Brabandt, vim...@googlegroups.com, vim-dev Mailingliste
Do they interfere? It's always better to keep change separate if
possible, so that when one of them causes trouble we can revert it.

> {
> sub_nsubs = 0;
> sub_nlines = 0;
> + global_do_ask = TRUE;

This name is a bit confusing, since it doesn't actually switch on
asking. How about global_stop_asking? It's set to FALSE at the start
and end of :global, when A or Q is typed then it's set to TRUE.


--
hundred-and-one symptoms of being an internet addict:
246. You use up your free 100 hours in less than a week.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
Reply all
Reply to author
Forward
0 new messages