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

Re: Tip of the day: restoring an aborted command-line



On Thu, 2015-07-02 at 21:29 +0000, zzapper wrote:
> Oliver Kiddle <okiddle@xxxxxxxxxxx> wrote in
> news:30469.1435788779@xxxxxxxxxxxxxxxx: 
> 
> > When you abort a command-line with Ctrl-C or whatever, the Zsh line
> > editor sets $ZLE_LINE_ABORTED to the contents of the command-line
> > before it was aborted. Sometimes you might want to recover the aborted
> > line and you could bind a dedicated key to that purpose. The following
> 
> > 
> >   zle-line-init () {
> >     if [[ -n $ZLE_LINE_ABORTED ]]; then
> >       local savebuf="$BUFFER" savecur="$CURSOR"
> >       BUFFER="$ZLE_LINE_ABORTED" 
> >       CURSOR="$#BUFFER" 
> >       zle split-undo
> >       BUFFER="$savebuf" CURSOR="$savecur" 
> >     fi
> >   }
> 
> > Oliver
> is $ZLE_LINE_ABORTED dependent on any autoload? as i have it on cygwin
> but not on centos (both 5.08) 

It shouldn't.

I'd like to propose the following enhancement:

         zle-line-init () {
            if [[ -n $ZLE_LINE_ABORTED ]]; then
              _last_aborted_line=$ZLE_LINE_ABORTED    
            fi
            if [[ -n $_last_aborted_line ]]    
            then
              local savebuf="$BUFFER" savecur="$CURSOR"
              BUFFER="$_last_aborted_line"     
              CURSOR="$#BUFFER" 
              zle split-undo
              BUFFER="$savebuf" CURSOR="$savecur"
            fi
          }
        zle -N zle-line-init
        zle-line-finish() { 
           unset _last_aborted_line
        }
        zle -N zle-line-finish  

Now this handle the case when you press Ctrl-C multiple times in a row.
Eg:
        zsh% foo=^C
        zsh% ^C
        zsh% ^C
        zsh% <undo>
        zsh% foo=

Phil.



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