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

5

1

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.

nperson325681

Posted 2014-11-06T11:09:11.570

Reputation: 1 401

Did you try stty echoctl ? – wurtel – 2014-11-06T12:11:55.143

@wurtel: that has effect on bash but not on zsh. – nperson325681 – 2014-11-06T12:15:07.643

1You could use the kill-whole-line widget - by default bound to ^U (C-u) - instead of ^C. This just removes everything from the current line and nothing confusing would remain. – Adaephon – 2014-11-06T13:44:34.030

@Adaephon: well, that's the current workout whenever I use that. I think there should be a way to make that character show. Or other message, by some means. – nperson325681 – 2014-11-06T15:11:21.330

Answers

7

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.

mpy

Posted 2014-11-06T11:09:11.570

Reputation: 20 866