2

I find myself using bash more often than not on remote machines even though fish is my preferred shell. Fish has small, but nice feature that when you hit Ctrl+C something like this happens:

if command running
   send SIGINT
else
   clear line (don't start a new one)

It would be nice to be able to do this in bash too.

I imagine it would involve trapping SIGINT, which comes from stty being configured to send it once Ctrl+C is hit, but I haven't found out how to execute the pseudo-code above.

What I've tried

trap 'tput dl1' SIGINT

which clears the line, but still continues to start a new line/prompt (it's like hitting enter on an empty prompt) and does so only if I've not navigated in history :\

1

OK, after a bit of fiddling with trap, tput and terminfo, I think I figured it out (thanks to another question).

For a standard commandline with only 1 line e.g bash-4.3$ echo something here

trap 'tput dl1; tput cuu1' SIGINT is what I needed.

bash will:

  • delete the line
  • move the cursor up

and finally start a new line (hence the move the cursor up).

If your commandline has 2 or more lines e.g

name@host /path/to/some/dir
$ echo something here

Then another tput dl1; tput cuu1 will have to be added, to delete the extra line --> trap 'tput dl1; tput cuu1; tput dl1; tput cuu1' SIGINT

I guess I'll see if this has any adverse effects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.