How to show a caret-C in canceled command line in zsh, like bash does

command lineshellzsh

When I'm in the middle of entering a line to my shell of choice, and change my mind, I can quickly throw C-c and start afresh. Under bash it looks like this:

user@machine:~$ rm everything^C
user@machine:~$ 

But on zsh, which I really prefer overall, shows nothing:

(~) rm -rf /
(~) 

Yeah, you may see why I would like to see that caret-C or similar message over the line so that I don't confuse which line have actually been executed and are in history for that matter.

This small issue is largely ungoogleable.

Best Answer

You can define a trap for SIGINT (triggered by CTRL-C), which will print ^C (or any other text you would like):

TRAPINT() {
  print -n "^C"
  return $(( 128 + $1 ))
}

This example is taken from man zshmisc. The return command has the following background:

Programs terminated by uncaught signals typically return the status 128 plus the signal number. Hence the [above code] causes the handler for SIGINT to print a message, then mimic the usual effect of the signal.

Related Question