zsh-users
 help / color / mirror / code / Atom feed
* Global History Substitution
@ 2005-11-03 19:07 Chris Johnson
  2005-11-03 19:36 ` Philip Kizer
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Chris Johnson @ 2005-11-03 19:07 UTC (permalink / raw)
  To: zsh-users

Is there a way to do global substitution on the previous command in ^
notation?  For instance,

   $ echo a b b b
   $ ^b^beta^

yields

   $ echo a beta b b

Certainly I could do !!:gs/b/beta.  This isn't near as simple as
something like gs/b/beta or ^b^beta^g.  Anything I'm missing in the
manual?

Thanks!

-- 
Chris Johnson
cjohnson@cs.utk.edu
http://www.cs.utk.edu/~cjohnson


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Global History Substitution
  2005-11-03 19:07 Global History Substitution Chris Johnson
@ 2005-11-03 19:36 ` Philip Kizer
  2005-11-03 22:08 ` zzapper
  2005-11-04 11:09 ` Peter Stephenson
  2 siblings, 0 replies; 9+ messages in thread
From: Philip Kizer @ 2005-11-03 19:36 UTC (permalink / raw)
  To: zsh-users

On Nov 3, 2005, at 13:07, Chris Johnson wrote:
> Certainly I could do !!:gs/b/beta.  This isn't near as simple as
> something like gs/b/beta or ^b^beta^g.  Anything I'm missing in the
> manual?

Try:

% r b=beta


-p


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Global History Substitution
  2005-11-03 19:07 Global History Substitution Chris Johnson
  2005-11-03 19:36 ` Philip Kizer
@ 2005-11-03 22:08 ` zzapper
  2005-11-04 11:09 ` Peter Stephenson
  2 siblings, 0 replies; 9+ messages in thread
From: zzapper @ 2005-11-03 22:08 UTC (permalink / raw)
  To: zsh-users

On Thu, 3 Nov 2005 14:07:47 -0500,  wrote:

>Is there a way to do global substitution on the previous command in ^
>notation?  For instance,
>
>   $ echo a b b b
>   $ ^b^beta^
>
>yields
>
>   $ echo a beta b b
I also luv the ^^^ commands

^fred^joe             # edit previous command replace fred by joe
^str1^str2^:u:p       # replace str1 by str2 change case and just display
echo chim
^chim^&-&ney-&-&-cheree # reuse LHS   ( from Bart)

-- 
zzapper
Success for Techies and Vim,Zsh tips
http://SuccessTheory.com/


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Global History Substitution
  2005-11-03 19:07 Global History Substitution Chris Johnson
  2005-11-03 19:36 ` Philip Kizer
  2005-11-03 22:08 ` zzapper
@ 2005-11-04 11:09 ` Peter Stephenson
  2005-11-04 11:30   ` Peter Stephenson
  2 siblings, 1 reply; 9+ messages in thread
From: Peter Stephenson @ 2005-11-04 11:09 UTC (permalink / raw)
  To: zsh-users

Chris Johnson <cjohnson@cs.utk.edu> wrote:
> Is there a way to do global substitution on the previous command in ^
> notation?  For instance,
> 
>    $ echo a b b b
>    $ ^b^beta^
> 
> yields
> 
>    $ echo a beta b b
> 
> Certainly I could do !!:gs/b/beta.  This isn't near as simple as
> something like gs/b/beta or ^b^beta^g.  Anything I'm missing in the
> manual?

No, I think with this particular feature you're stuck.  It's a historical
oddity that the "g" for global doesn't work as a qualifier, so the ^
notation is restricted.

It turns out that fixing the code to use :g like a qualifier isn't that
difficult, however...  that's ^b^beta^:g, since if the colon is missing the
g is treated like ordinary text.

Index: Doc/Zsh/expn.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/expn.yo,v
retrieving revision 1.57
diff -u -r1.57 expn.yo
--- Doc/Zsh/expn.yo	1 Nov 2005 18:04:25 -0000	1.57
+++ Doc/Zsh/expn.yo	4 Nov 2005 11:05:01 -0000
@@ -117,6 +117,8 @@
 More precisely, the sequence `tt(^)var(foo)tt(^)var(bar)tt(^)' is
 synonymous with `tt(!!:s)tt(^)var(foo)tt(^)var(bar)tt(^)', hence other
 modifiers (see noderef(Modifiers)) may follow the final `tt(^)'.
+In particular, `tt(^)var(foo)tt(^)var(bar)tt(:g)' performs a global
+substitution.
 
 If the shell encounters the character sequence `tt(!")'
 in the input, the history mechanism is temporarily disabled until
@@ -254,10 +256,14 @@
 )
 item(tt(s/)var(l)tt(/)var(r)[tt(/)])(
 Substitute var(r) for var(l) as described below.
-Unless preceded immediately by a tt(g), with no colon between,
-the substitution is done only for the
+The substitution is done only for the
 first string that matches var(l).  For arrays and for filename
 generation, this applies to each word of the expanded text.
+
+The forms `tt(gs/)var(l)tt(/)var(r)' and `tt(s/)var(l)tt(/)var(r)tt(/:g)'
+perform global substitution, i.e. substitute every occurrence of var(r)
+for var(l).  Note that the tt(g) or tt(:g) must appear in exactly the
+position shown.
 )
 item(tt(&))(
 Repeat the previous tt(s) substitution.  Like tt(s), may be preceded
Index: Src/hist.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/hist.c,v
retrieving revision 1.60
diff -u -r1.60 hist.c
--- Src/hist.c	10 Aug 2005 10:56:41 -0000	1.60
+++ Src/hist.c	4 Nov 2005 11:05:02 -0000
@@ -295,13 +295,27 @@
 	hwaddc(ingetc());
 }
 
-/* extract :s/foo/bar/ delimiters and arguments */
+/*
+ * Extract :s/foo/bar/ delimiters and arguments
+ *
+ * The first character expected is the first delimiter.
+ * The arguments are stored in the hsubl and hsubr variables.
+ *
+ * subline is the part of the command line to be matched.
+ *
+ * *gbalp is set to 1 to indicate a trailing ':g', i.e. a
+ * global substitution.
+ *
+ * If a ':' was found but was not followed by a 'g',
+ * *cflagp is set to 1 and the input is backed up to the
+ * character following the colon.
+ */
 
 /**/
 static int
-getsubsargs(char *subline)
+getsubsargs(char *subline, int *gbalp, int *cflagp)
 {
-    int del;
+    int del, follow;
     char *ptr1, *ptr2;
 
     del = ingetc();
@@ -315,6 +329,17 @@
     }
     zsfree(hsubr);
     hsubr = ptr2;
+    follow = ingetc();
+    if (follow == ':') {
+	follow = ingetc();
+	if (follow == 'g')
+	    *gbalp = 1;
+	else {
+	    inungetc(follow);
+	    *cflagp = 1;
+	}
+    } else
+	inungetc(follow);
     if (hsubl && !strstr(subline, hsubl)) {
 	herrflush();
 	zerr("substitution failed", NULL, 0);
@@ -348,14 +373,16 @@
 
     /* look, no goto's */
     if (isfirstch && c == hatchar) {
+	int gbal = 0;
+
 	/* Line begins ^foo^bar */
 	isfirstch = 0;
 	inungetc(hatchar);
 	if (!(ehist = gethist(defev))
 	    || !(sline = getargs(ehist, 0, getargc(ehist)))
-	    || getsubsargs(sline) || !hsubl)
+	    || getsubsargs(sline, &gbal, &cflag) || !hsubl)
 	    return -1;
-	subst(&sline, hsubl, hsubr, 0);
+	subst(&sline, hsubl, hsubr, gbal);
     } else {
 	/* Line doesn't begin ^foo^bar */
 	if (c != ' ')
@@ -577,7 +604,7 @@
 		}
 		break;
 	    case 's':
-		if (getsubsargs(sline))
+		if (getsubsargs(sline, &gbal, &cflag))
 		    return -1; /* fall through */
 	    case '&':
 		if (hsubl && hsubr)

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


This message has been scanned for viruses by BlackSpider MailControl - www.blackspider.com


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Global History Substitution
  2005-11-04 11:09 ` Peter Stephenson
@ 2005-11-04 11:30   ` Peter Stephenson
  2005-11-04 13:12     ` zzapper
  2005-11-04 15:13     ` Bart Schaefer
  0 siblings, 2 replies; 9+ messages in thread
From: Peter Stephenson @ 2005-11-04 11:30 UTC (permalink / raw)
  To: zsh-users

Peter Stephenson <pws@csr.com> wrote:
> It turns out that fixing the code to use :g like a qualifier isn't that
> difficult, however...  that's ^b^beta^:g, since if the colon is missing the
> g is treated like ordinary text.

One subtlety is that we should ensure that

  !!:gs/foo/bar/:gs/this/that/

works as it always did, i.e. the g goes with the following s in both
cases.  I can fix that.

It does mean

  !!:s/foo/bar/:gs/this/that/

will change meaning, associating the g with the preceeding substitution
and then not treating the rest as text.  This might make the proposal
less than usable.

Using an uncoloned g isn't really a good idea since history always
treats trailing text as command line argument and I wouldn't want to change
that.

Hmm, how about

  !!:s/foo/bar/:G

or

  ^foo^bar^:G

instead?

pws


This message has been scanned for viruses by BlackSpider MailControl - www.blackspider.com


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Global History Substitution
  2005-11-04 11:30   ` Peter Stephenson
@ 2005-11-04 13:12     ` zzapper
  2005-11-04 15:13     ` Bart Schaefer
  1 sibling, 0 replies; 9+ messages in thread
From: zzapper @ 2005-11-04 13:12 UTC (permalink / raw)
  To: zsh-users

On Fri, 4 Nov 2005 11:30:38 +0000,  wrote:

>Peter Stephenson <pws@csr.com> wrote:
>> It turns out that fixing the code to use :g like a qualifier isn't that
>> difficult, however...  that's ^b^beta^:g, since if the colon is missing the
>> g is treated like ordinary text.
>
>One subtlety is that we should ensure that
>
>  !!:gs/foo/bar/:gs/this/that/
>
>works as it always did, i.e. the g goes with the following s in both
>cases.  I can fix that.
>
>It does mean
>
>  !!:s/foo/bar/:gs/this/that/
>
>will change meaning, associating the g with the preceeding substitution
>and then not treating the rest as text.  This might make the proposal
>less than usable.
>
>Using an uncoloned g isn't really a good idea since history always
>treats trailing text as command line argument and I wouldn't want to change
>that.
>
>Hmm, how about
>
>  !!:s/foo/bar/:G
>
>or
>
>  ^foo^bar^:G
Yes Please!

-- 
zzapper
Success for Techies and Vim,Zsh tips
http://SuccessTheory.com/


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Global History Substitution
  2005-11-04 11:30   ` Peter Stephenson
  2005-11-04 13:12     ` zzapper
@ 2005-11-04 15:13     ` Bart Schaefer
  2005-11-04 16:12       ` Peter Stephenson
  1 sibling, 1 reply; 9+ messages in thread
From: Bart Schaefer @ 2005-11-04 15:13 UTC (permalink / raw)
  To: Peter Stephenson, zsh-users

On Nov 4, 11:30am, Peter Stephenson wrote:
} 
} It does mean
} 
}   !!:s/foo/bar/:gs/this/that/
} 
} will change meaning, associating the g with the preceeding substitution

Do you mean the following substitution?

I think :gs should always be treated as a unit.

} and then not treating the rest as text.  This might make the proposal
} less than usable.

The same thing occurred to me when I saw your previous message.
 
} Hmm, how about
} 
}   !!:s/foo/bar/:G

Yes, I think that would be OK, especially because it also disambiguates
!!:s/foo/bar/:Gs/this/that/ -- the :G can always apply to the preceding
substitution and :g always to the following one.

That just leaves the question of whether the G is ignored in the case
!!:Gs/foo/bar/ (where there is no preceding substitution).

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Global History Substitution
  2005-11-04 15:13     ` Bart Schaefer
@ 2005-11-04 16:12       ` Peter Stephenson
  2005-11-08 19:07         ` zzapper
  0 siblings, 1 reply; 9+ messages in thread
From: Peter Stephenson @ 2005-11-04 16:12 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Nov 4, 11:30am, Peter Stephenson wrote:
> } 
> } It does mean
> } 
> }   !!:s/foo/bar/:gs/this/that/
> } 
> } will change meaning, associating the g with the preceeding substitution
> 
> Do you mean the following substitution?

No, I did mean the preceeding; that was the change of meaning.  Before it
would have meant an s followed by a gs.  With the original proposal it
would have meant an s...:g followed by an s.

> } Hmm, how about
> } 
> }   !!:s/foo/bar/:G
> 
> Yes, I think that would be OK, especially because it also disambiguates
> !!:s/foo/bar/:Gs/this/that/ -- the :G can always apply to the preceding
> substitution and :g always to the following one.
> 
> That just leaves the question of whether the G is ignored in the case
> !!:Gs/foo/bar/ (where there is no preceding substitution).

I think it's probably simpler always to require g at the start and :G at
the end.  I'll commit it in that form.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


This message has been scanned for viruses by BlackSpider MailControl - www.blackspider.com


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Global History Substitution
  2005-11-04 16:12       ` Peter Stephenson
@ 2005-11-08 19:07         ` zzapper
  0 siblings, 0 replies; 9+ messages in thread
From: zzapper @ 2005-11-08 19:07 UTC (permalink / raw)
  To: zsh-users

On Fri, 4 Nov 2005 16:12:49 +0000,  wrote:

>Bart Schaefer <schaefer@brasslantern.com> wrote:
>> On Nov 4, 11:30am, Peter Stephenson wrote:
>> } 
>> } It does mean
>> } 
>> }   !!:s/foo/bar/:gs/this/that/
>> } 
>> } will change meaning, associating the g with the preceeding substitution
>> 
>> Do you mean the following substitution?
>
>No, I did mean the preceeding; that was the change of meaning.  Before it
>would have meant an s followed by a gs.  With the original proposal it
>would have meant an s...:g followed by an s.
>
>> } Hmm, how about
>> } 
>> }   !!:s/foo/bar/:G
>> 
>> Yes, I think that would be OK, especially because it also disambiguates
>> !!:s/foo/bar/:Gs/this/that/ -- the :G can always apply to the preceding
>> substitution and :g always to the following one.
>> 
>> That just leaves the question of whether the G is ignored in the case
>> !!:Gs/foo/bar/ (where there is no preceding substitution).
>
>I think it's probably simpler always to require g at the start and :G at
>the end.  I'll commit it in that form.

That's the beauty of zsh (and open source generally) that a few experts can respond to user requests
and decide that a new feature would be jolly useful and implement it. Quite exciting really!

-- 
zzapper
Success for Techies and Vim,Zsh tips
http://SuccessTheory.com/


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2005-11-08 19:19 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-03 19:07 Global History Substitution Chris Johnson
2005-11-03 19:36 ` Philip Kizer
2005-11-03 22:08 ` zzapper
2005-11-04 11:09 ` Peter Stephenson
2005-11-04 11:30   ` Peter Stephenson
2005-11-04 13:12     ` zzapper
2005-11-04 15:13     ` Bart Schaefer
2005-11-04 16:12       ` Peter Stephenson
2005-11-08 19:07         ` zzapper

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).