3

I really don't understand bash's use of control characters. I understand simple things like adding colors with escape sequences but am at a loss for how to do things like bind keys in .inputrc.

For example, is there a way to type things like Shifta and see how I should represent it in .inputrc?

Much like the fellow does here: http://linuxart.com/log/archives/2005/10/13/super-useful-inputrc

4

For reference, here's archive.org's copy of the (dead) link in OP's question. The codes shown therein are emacs lisp style codes. I don't know much about them. Of course, there's more than one way to skin a cat, but what follows is probably the easiest:

First, use showkey -a to get the hexadecimal representation for your keyboard sequence. For example, when I press CtrlShift~, I get:

^^       30 0036 0x1e

So, my hex representation is 0x1e. I can then add a line to my ~/.inputrc:

"\x1e": beginning-of-line

And the next time I open a bash shell (or any program that uses readline), typing CtrlShift~ will move my cursor to the beginning of the line. You can also do something like:

"\x1b\x5a": "type this text whenever I press alt+shift+z"
1

The basic problem posed in OP's question is how to work around a limitation of readline (and bash):

  • special keys such as function- and cursor-keys usually send escape sequences,
  • the escape sequences may (depending on the terminal and how it is initialized) be different.
  • one main difference across terminal descriptions is the use of application mode versus normal mode.
  • in application mode, a cursor key might send \eOA (using bash's name for the escape character), while in normal mode it would send \e[A.
  • full-screen applications (which is what the terminal database supports) conventionally use application mode; there are exceptions such as the terminal description provided for the Linux console which do not.
  • if the terminal description uses application mode, the keys defined in the terminal description do not match the values seen in the shell (because the shell does not turn on the corresponding feature smkx).

readline sort-of pays attention to the terminal database (so it can move the cursor around the line which you edit), but not enough to do anything useful with the information about application mode versus normal mode in key bindings. To work with readline, you have to provide the hardcoded escape sequences which match each terminal that you use. Of course, you can have multiple sequences mapped to the same function. Fortunately, conflicting uses of the same sequence are rare.

To portably find the characters sent by a special key, use cat -v, i.e., type that command and press your keys to see the result. It will each ^[ for each escape character. Other characters can be used as-is.

By the way, showkey is Linux-specific, and works only in the console.

Further reading:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.