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

Re: Printing ^C, ^D key presses at end of prompt when pressed



On Feb 3, 11:05pm, Michael Treibton wrote:
} 
} In bash, if I type something at the prompt, and press ^C, my prompt
} would look like this:
} 
} $ some command ^C
} 
} That is the "^C" is explicitly printed.

Really.  Doesn't work for me.  Do you somehow have readline disabled?

You seem to be describing native TTY input mode with ctlecho turned on
in the stty settings.

} But in zsh, this doesn't happen, and as odd as it may seem I really
} miss this visual indication of this.

Zsh normally leaves the stty intr setting alone and handles the INT
signal.  Which means that when you type ^C, you're sending a signal
to the tty process group, not a normal keystroke to the shell input.
This has some helpful side-effects for process management, but means
the the line editor exits.

In order to behave the way you want, you have to trap the INT signal
and print the ^C yourself:

    TRAPINT() { print -n -u2 '^C'; return $((128+$1)) }

Some old versions of zsh may not behave correctly when returning from
the trap, i.e., may not propagate the effects of being interrupted.

} Likewise if I press ^D to
} logout from a prompt, that too is printed:
} 
} $ ^D

Again, zsh leaves the tty settings alone, so it doesn't get a ^D here,
it gets an end-of-file on the shell input descriptor.  This causes it
to exit unless ignoreeof is set.

One might therefore think this could be handled like so:

    zshexit() { print -n -u2 '^D' }

However, the line editor prints a newline when finished, before the
zshexit hook is called; so the ^D ends up on the next line rather
than at the point where it was typed.

Instead you have to override the widget that's bound to the ^D key:

    delete-char-or-list() {
	[[ -z $BUFFER ]] && print -n -u2 '^D' && exit 0
	zle .$WIDGET
    }
    zle -N delete-char-or-list
    setopt ignoreeof

Ignoreeof is necessary for the line editor to ever get the ^D kestroke
in the first place, otherwise it exits without calling your widget.



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