Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In bash, how do I bind a function key to a command?

Example: I want to bind the F12 key to the command echo "foobar" such that every time I hit F12 the message "foobar" will be printed to screen. Ideally it could be any arbitrary shell command, not just builtins. How does one go about this?

like image 921
SiegeX Avatar asked Nov 17 '10 01:11

SiegeX


People also ask

How do you call a function in a bash script?

To invoke a bash function, simply use the function name. Commands between the curly braces are executed whenever the function is called in the shell script. The function definition must be placed before any calls to the function.

What is the function of BIND command in bash shell?

bind command is Bash shell builtin command. It is used to set Readline key bindings and variables. The keybindings are the keyboard actions that are bound to a function. So it can be used to change how the bash will react to keys or combinations of keys, being pressed on the keyboard.

How do you bind a function on a keyboard?

To reassign a keyConnect the keyboard that you want to configure. Select the Start button, and then select Microsoft Mouse and Keyboard Center. From the displayed list of key names, select the key that you want to reassign. In the command list of the key that you want to reassign, select a command.

How do I bind a key in Ubuntu?

It can be found as "System Settings" in the menu or accessed on the command line using "gnome-control-center". The key bindings are in the "Keyboard" section under a "Shortcuts" tab. To assign a key to an action, click on the right hand side of the window on the currently assigned button or "disabled".


2 Answers

You can determine the character sequence emitted by a key by pressing Ctrl-v at the command line, then pressing the key you're interested in. On my system for F12, I get ^[[24~. The ^[ represents Esc. Different types of terminals or terminal emulators can emit different codes for the same key.

At a Bash prompt you can enter a command like this to enable the key macro so you can try it out.

bind '"\e[24~":"foobar"' 

Now, when you press F12, you'll get "foobar" on the command line ready for further editing. If you wanted a keystroke to enter a command immediately, you can add a newline:

bind '"\e[24~":"pwd\n"' 

Now when you press F12, you'll get the current directory displayed without having to press Enter. What if you've already typed something on the line and you use this which automatically executes? It could get messy. However, you could clear the line as part of your macro:

bind '"\e[24~":"\C-k \C-upwd\n"' 

The space makes sure that the Ctrl-u has something to delete to keep the bell from ringing.

Once you've gotten the macro working the way you want, you can make it persistent by adding it to your ~/.inputrc file. There's no need for the bind command or the outer set of single quotes:

"\e[24~":"\C-k \C-upwd\n" 

Edit:

You can also create a key binding that will execute something without disturbing the current command line.

bind -x '"\eW":"who"' 

Then while you're typing a command that requires a username, for example, and you need to know the names of user who are logged in, you can press Alt-Shift-W and the output of who will be displayed and the prompt will be re-issued with your partial command intact and the cursor in the same position in the line.

Unfortunately, this doesn't work properly for keys such as F12 which output more than two characters. In some cases this can be worked around.

The command (who in this case) could be any executable - a program, script or function.

like image 154
Dennis Williamson Avatar answered Sep 21 '22 18:09

Dennis Williamson


You can define bash key bindings in ~/.inputrc (configuration file for the GNU Readline library). The syntax is

<keysym or key name>: macro

for example:

Control-o: "> output" 

will create a macro which inserts "> output" when you press ControlO

 "\e[11~": "echo foobar" 

will create a macro which inserts "echo foobar" when you press F1... I don't know what the keysym for F11 is off hand.

Edit:

.inputrc understands the \n escape sequence for linefeed, so you can use

 "\e[11~": "echo foobar\n" 

Which will effectively 'press enter' after the command is issued.

like image 31
Barton Chittenden Avatar answered Sep 20 '22 18:09

Barton Chittenden