How to execute a script in shell when a shortcut key is pressed

32,945

You can use the builtin command, bind to map a keyboard shortcut so that it executes a command/shell script.

Example

Say we want to run the command, pwd, when we press the F12 key.

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

Now when I press F12 at my prompt, $:

$ pwd
/home/saml

Determining keyboard shortcuts

You can use the following technique to determine the escape code for a given keyboard shortcut. On most systems press Ctrl + V, release, and then press the key you want the code for. There are some other systems it will work with M instead of V

Example

Pressing Ctrl + V then release both Ctrl and V and finally press F12 in a terminal window returns this:

$ ^[[24~

This output can be interpreted as follows, ^[ is the Esc key. So when we want to specify this particular key using the bind command we need to use a \e to denote the Esc key followed by everything else from above. So the bind command looks like this:

$ bind '"\e[24~":"....."'

Executing a command in the middle

You can also make use of bind -x to setup keyboard shortcuts that will run commands while you're in the middle of typing something at the prompt, and these commands' output will be displayed, but what ever you were typing at the prompt will remain intact.

$ bind -x '"\eW":"..."'

NOTE: This method only works with keyboard shortcuts that output 1 character, so F12 won't work here.

Example

Let's alias the keyboard shortcut Alt + Shift + W.

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

Say I'm typing the command finger:

$ finger

Now I hit the keyboard shortcut Alt + Shift + W:

saml     tty1         2013-09-01 11:01 (:0)
saml     pts/0        2013-09-01 11:03 (:0.0)
saml     pts/1        2013-09-01 11:05 (:0.0)
saml     pts/2        2013-09-01 11:05 (:0.0)
saml     pts/5        2013-09-03 22:45 (:0.0)
$ finger

What's going on is bind is running the command defined, who, taking its output and inserting it in front of the prompt. If you repeat it you'll see what's going on, here's output from me hitting it 2 times:

saml     tty1         2013-09-01 11:01 (:0)
saml     pts/0        2013-09-01 11:03 (:0.0)
saml     pts/1        2013-09-01 11:05 (:0.0)
saml     pts/2        2013-09-01 11:05 (:0.0)
saml     pts/5        2013-09-03 22:45 (:0.0)
saml     tty1         2013-09-01 11:01 (:0)
saml     pts/0        2013-09-01 11:03 (:0.0)
saml     pts/1        2013-09-01 11:05 (:0.0)
saml     pts/2        2013-09-01 11:05 (:0.0)
saml     pts/5        2013-09-03 22:45 (:0.0)
$ finger

Your problem

So one idea would be to use the bind -x method above and cat to display this text file at your prompt:

$ bind -x '"\eW":"cat someinfo.txt"'

Now when I run commands I can see this file like so:

This is text from some 
multi-line file reminding
me how to do some 
stuff
$ finger 

The output of file someinfo.txt is being displayed above my finger command above.

References

Share:
32,945

Related videos on Youtube

user3539
Author by

user3539

Updated on September 18, 2022

Comments

  • user3539
    user3539 over 1 year

    How could I execute a script in Shell when a shortcut key is pressed.

    Essentially what I need is when a shortcut key is pressed the script should read from a file and display that content in the terminal.

    • Admin
      Admin over 9 years
      I tried the method you used bind '"\e[24~":"airmon-ng start wlan0\n"' but after I closed the terminal, the thing seems to be reset and all the hotkeys are gone
  • Lars Rohrbach
    Lars Rohrbach over 10 years
    So bind to a script that displays the contents.
  • aliteralmind
    aliteralmind over 9 years
    Ctrl+m is not working for me. It acts as if I just pressed enter. An alternative I think (I'm a bash newbie) is to type echo ' (space after the single-quote), then press Ctrl+v, then the key you want, and then space, single-quote...as described here. Example: echo ' ^[OD '
  • alper
    alper almost 4 years
    Can I apply these by using bindkey?