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

Re: print -s and History Expansion



Peter Stephenson sent me the following 1.5K:

> > 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

That fixes the problem.  Thanks!  The oblique way that I ran into this
was actually through a post from 1997 (maybe) you made to zsh-users.
Someone asked how to commit a line to history without executing it, and
you offered

   bindkey -s "^X^H" "^['^A print -s ^M"

Since removing the the quote-line (^[') will introduce trouble with &
and so on which really do need to be quoted, I've had to resort to my
own widget:

   commit-to-history() {
      # zsplit current command, expand result to all its quoted fields
      # ("f1" "f2" "f3"), prepend it with a command to make it written
      # to history, and then execute it.
      BUFFER=" print -s ${${(z)BUFFER}[@]}"
      zle accept-line
   }
   zle -N commit-to-history
   bindkey "^X^H" commit-to-history

I tried using ${(qq){$(z)BUFFER}} but that didn't work.

Perhaps I could have stuck with the simpler bind, but it seems that
somehow I need to get the command into a parameter before I can split
it.  Is there any way to expand literals?

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



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