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

Re: Append cancelled commands to history



Last year, a discussion started on this list on how to save a partially
typed command to the history when you press the interrupt key - so that
you can return to this command later.

From ideas of several people, we built the following simple function:

On Mon, Jul 06, 2009, Nadav Har'El wrote about "Re: Append cancelled commands to history":
> # When a line is killed, put it in the history anyway in case we want to
> # return to it
> TRAPINT() {
>         # Store the current buffer in the history.
>         zle && 
>         [[ $HISTNO -eq $HISTCMD ]] && # only if we're not back in the history
>         print -s -r -- $BUFFER
> 
>         # Return the default exit code so zsh aborts the current command.
>         return $1
> }

This runs on interrupt and saves the currently edited ZLE buffer to the
history, but only if we were editing a new command (if I go up the history
and then press interrupt, I see no reason to save the old command again in
my history): 

But now I have a new problem. I want this to also work when editing multi-line
commands, e.g., if I start typing

	for i in *
	do

and then right after the do - without a newline - I press interrupt, I want
to be able to later go up the history to continue editing this command.

I thought this would be simple - just replace the print line with
	print -s -r -- "$PREBUFFER$BUFFER"

to print the full multiline command. But the strange thing is: It appears
that when I kill a multiline zle, zsh already (regardless of my function)
saves all the previous lines except the one now edited (i.e., saves $PREBUFFER)
to the history, so with my function the command is saved twice, once without
the first line, and once with the first line.

Does anyone know why zsh saves interrupted partial multiline commands, but
without their last line? Can this be avoided? And what was the rationale
to save the beginning of a multiline command and not everything - or not
at all for a single line command?

Thanks,
Nadav.


-- 
Nadav Har'El                        |        Monday, Mar 15 2010, 1 Nisan 5770
nyh@xxxxxxxxxxxxxxxxxxx             |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |Always go to other people's funerals,
http://nadav.harel.org.il           |otherwise they won't come to yours.



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