(Background: I'm a long-time tcsh user, gradually transitioning to bash, and trying to find equivalents for some useful tcsh-specific features.)
In tcsh, I can define a key binding that executes an external command. For example, given:
bindkey -c ^Gu uptime
I can type "Control-G u" in tcsh, and it will execute the uptime
command. I don't have to type Enter, the command doesn't appear in my history, and I can do it in the middle of an input line (I find the latter particularly useful for certain commands).
bash has a similar key binding mechanism via the GNU readline library, with bindings specified in $HOME/.inputrc
(or elsewhere). But after reading the info readline
documentation, I don't see a way for a key binding to execute an external command.
The closest thing I've figured out is to add something like this to my .inputrc
file:
"\C-gu": "uptime\n"
but that doesn't execute the command; rather, it acts as if I had typed uptime
followed by the Enter key. The command appears in my history (that's ok), and it works only on an empty line; if I type "echo control-Gu"
, then it prints uptime
rather than executing the command.
Another minor drawback is that the binding affects other commands that use GNU readline, such as the Perl debugger.
Is there a way to simulate the effect of tcsh's bindkey -c
in bash, by mapping a key sequence to the execution of a specified external command?
If it matters, I'm using bash 4.2.24 on Ubuntu 12.04 beta 2.
Best Answer
Not all
bash
line editing is controlled from~/.inputrc
; much of it is configured via thebind
builtin. In this case, you want something likein your
~/.bashrc
.