Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: print -s and History Expansion



Chris Johnson wrote:
> Multiword lines issued to history with print -s don't appear to be
> considered as multiple words, as evidenced by the unexpected history
> expansion found in the following interaction:
> 
> [cjohnson@sissy] ~ S1:  print -s 'echo a b c'
> [cjohnson@sissy] ~ S1: echo !!:1
> zsh: no such word in event

You'll kick yourself when I tell you... to get the line entered in the
history as multiple words, pass multiple words to echo:

% print -s echo a b c
% echo !!:1
a

No quotes.


You may have come across this obliquely, because the line you were
saving came from a scalar variable.  In that case, the best thing to do
is probably to zplit it.  If you've got histverify set it looks like
this (else you don't see the final line, just the result of it):

% var='echo one "two three" "four five"'
% print -s ${(z)var}
% print !!:2
% print "two three"


A more complicated case is where you already have a set of arguments,
but they aren't yet quoted, for example:

% array=(echo one 'two three' 'four five')

Here, the quotes have already been "used up" in generating the array.
You can requote the arguments to get all the parts of a command line
like this (again with histverify):

% print -s ${(qq)array}
% echo !!:$
% print 'four five'

That came up with single quotes; (q) would have used backslashes.


In other words, there are plenty of ways of doing it, it depends what
you're starting with.  That should give you enough to be getting on with.

-- 
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/



Messages sorted by: Reverse Date, Date, Thread, Author