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.