Bash – the general format of keyname for key bindings in “inputrc” file

bashreadline

I understand that the format for key bindings in inputrc file is:

keyname: function-name or macro

For keyname, I saw things like:

"\e[B"
"\e[1~"
"\e[5D"
"\M-l"
"\C-[OD"
"\e\e[D"

What is the meaning of these keynames, and what is the general format for keyname ? Please share a link to docs.

Best Answer

Keybinding can be done using one of the following forms:

  1. keyname: command_name
  2. "keystroke_sequence": command_name

In first form you can spell out the name for a single key. For example, CONTROL-U would be written as control-u. This is useful for binding commands to single keys.

In the second form, you specify a string that describes a sequence of keys that will be bound to the command. The one you gave as an example is the emacs-tyle backslash escape sequences to represent the special keys

\C - Control
\M - Meta
\e - Escape

You can specify a backslash using another backslash – \\. Similarly ' and " can be escaped too - \' and \"

Update

These characters is what is interpreted by your terminal when you press special keys. You don't want to bind regular alphabets and numerics in your key binding as you might be using them on regular basis and can cause issues when you accidentally hit a combination that has been mapped in your ~/.inputrc or /etc/inputrc file.

[1~ is what is interpreted by your terminal when you press your HOME button. 

To learn more, simply type read on your terminal prompt and press all types of special keys like Function Keys, HOME, END, Arrow Keys etc and see what gets displayed.

Here is a small reference I found that can offer some basic understanding. Good luck! :)