Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

(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.

share|improve this question
    
As a long-time user of tcsh myself, I'm curious why you are making the transition (other than perhaps the prevalence of bash shells/users) –  Levon May 25 '12 at 14:08
    
@Levon: bash is installed by default more commonly than tcsh. bash is better for scripting, and it's nice to use the same language for scripting and interactive use. –  Keith Thompson May 25 '12 at 15:13
    
All true .. maybe I'll make the switch eventually too (most of my scripts are actually in Python :-) .. but I see your point for sure. Thanks. –  Levon May 25 '12 at 15:22
    
@Levon: And most of my scripts are in Perl, –  Keith Thompson May 25 '12 at 15:29
add comment

1 Answer 1

up vote 5 down vote accepted

Not all bash line editing is controlled from ~/.inputrc; much of it is configured via the bind builtin. In this case, you want something like

bind -x '"\C-gu":uptime'

in your ~/.bashrc.

share|improve this answer
    
Thanks, that's exactly what I was looking for. This could well be the final nail in the coffin of my tcsh usage. –  Keith Thompson Apr 19 '12 at 23:00
    
@KeithThompson Or you could switch to zsh, which does this easily, and more generally nabbed pretty much every feature of tcsh and bash. –  Gilles Apr 19 '12 at 23:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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