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

Re: print -s and History Expansion



Chris Johnson wrote:
>    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.

To avoid the extra command line execution I would use:

commit-to-history() {
  print -s ${(z)BUFFER}
  BUFFER=
  zle accept-line
}

then there are no quoting problems.  You still need the accept-line if
you plan on using what you've just added to the history immediately,
since otherwise the history isn't reread.  However, it's now on an empty
buffer.

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

There's no way to avoid using a parameter without doing worse things.  Zle
widgets are anyway more readable and predictable than bindkey -s tricks.

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


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php



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