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

Example: I want to bind the F12 key to the echo "foobar" command so that every time I press F12 , the message "foobar" will be printed on the screen. Ideally, this could be any arbitrary shell command, not just the built-in ones. How can I do that?

+84
bash shell binding
Nov 17 '10 at 1:48
source share
4 answers

You can determine the sequence of characters that comes from a key by pressing Ctrl - v on the command line, and then pressing the button of interest to you. On my system for F12, I get ^[[24~ , ^[ represents Esc . Different types of terminals or terminal emulators can allocate different codes for the same key.

At the Bash prompt, you can enter such a command to enable a key macro so you can try it.

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

Now when you press F12 , you will get "foobar" at the command prompt, ready for further editing. If you want a keystroke to immediately enter a command, you can add a new line:

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

Now when you press F12 , you will see the current directory without pressing Enter . What if you already dialed something on the line and use it that automatically runs? It can get messy. However, you can clear the line as part of your macro:

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

The space ensures that Ctrl - u has something to delete so that the call does not ring.

Once the macro works the way you want, you can make it permanent by adding it to your ~/.inputrc file. There is no need for a bind command or an external set of single quotes:

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

Edit:

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

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

Then, when you enter a command that requires a username, for example, and you need to know the names of the users who are logged in, you can press Alt - Shift - W , and the output from who will be displayed and the invitation will be displayed with the incomplete command saved and cursor in the same position on the line.

Unfortunately, this does not work properly for keys like F12 that output more than two characters. In some cases, this can be circumvented.

A command ( who in this case) can be any executable file — a program, script, or function.

+147
Nov 17 2018-10-11T00:
source share

You can define bash key bindings in .inputrc (the configuration file for the GNU Readline library). Syntax

& lt; keysym or key name & gt ;: macro

eg:

 Control-o: "> output" 

will create a macro that inserts "> output" when you press Control O

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

will create a macro that inserts "echo foobar" when you press F1 ... I don’t know which keyboard for F11 is not used.

Edit:

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

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

Which will effectively "press enter" after the command.

+17
Nov 17 '10 at 3:14
source share

This solution applies to X11 environments and has nothing to do with bash, but adds the following to your .Xmodmaps

  % loadkeys keycode 88 = F12 string F12 = "foobar" % 

will send the string "foobar" to the terminal after pressing F12.

+7
Nov 17 '10 at 1:59
source share

I wanted to bind Ctrl+B to the command. Inspired by the answer above, I tried to use bind but could not figure out which series of mysterious squiggles ( \e[24~ ?) Translate to Ctrl+B

On a Mac, go to the “Settings” application of the “Terminal” application, “Profiles” → “Keyboard” → + then press the desired key combination and it will exit. For me, Ctrl+B led to \002 which I successfully associated with the command

bind '"\002":"echo command"'

Also, if you want the command to be executed immediately (and not just inserted into the prompt), you can add Enter at the end of your command, for example, like this:

bind '"\002":"echo command\015"'

+2
Jul 20 '18 at 9:55
source share



All Articles