Bash traps are not a way to handle keyboard input; they handle signals. It works for Ctrl+C because your terminal (not Bash) is configured to send a SIGINT signal to Bash when that key combination is pressed (run stty -a
to see all such bindings). But signals aren't designed to be used in this way. trap ... SIGINT
should almost always be used to interrupt and terminate the program.
As @MarkSetchell suggested, bind
is the right tool for configuring key bindings like Ctrl+A (notation: C-a). The manual has more details about existing bindings you can configure (see the "Miscellaneous Commands" in particular, which provides some useful behavior like Esc Ctrl+A (M-C-e) which expands all variables in the current pending command). Notice that C-a is bound to beginning-of-line
by default, and C-b is bound to backward-char
. The Readline manual goes into more detail, since that's what's actually responsible for all this behavior. There's also plenty of articles and examples online about getting bind
to do different things, so simply searching for "bind" instead of "trap" may be enough to get you going :)
Here's a couple examples you can try:
bind "\C-a":display-shell-version
will cause Ctrl+A to invoke the readline function display-shell-version
which prints the current Bash version, instead of jumping the cursor to the start of the line.
bind -x '"\C-b":"echo Hello World"'
(note -x
) will cause Ctrl+B to invoke the shell command echo Hello World
, instead of moving the cursor back one character.
It sounds like you want to configure arbitrary keybindings, which you may find is a bit of an uphill battle due to the different layers involved. A dedicated GUI-aware program written in a "proper" programming language that monitors your keyboard would be better suited for something like what you're describing. Obviously that's more work than wrestling with Bash, so hopefully you can get something good-enough out of bind
:)