Wednesday, August 13, 2014

Some .inputrc detail

I've passed by the .inputrc file sometimes in the past.
It's the file associated with the readline(3), stty(1) and termio(7I).

What I'd like to do now is to show how to use all of this to present one basic (suboptimal) setting to the behavior of the Del key, specially under the sun-color terminal. The case for the xterm terminal is easier to solve by tweaking .inputrc only.

The first command to note about is bind which is a bash built-in command.
It can set and display many bash key bindings and variables.
(not to mention commands bindings)
For all options:

$ help bind
...

$ bind -P |grep -v 'not bound' |less
...

Here's an example from a tweak for a xterm terminal:

$ bind -q delete-char
delete-char can be invoked via "\C-d", "\e[3~". 

Here's another example for a default sun-color terminal:

$ bind -q backward-delete-char
backward-delete-char can be invoked via "\C-h", "\C-?".

$ bind -q delete-char
delete-char can be invoked via "\C-d".

The above settings for sun-color could lead to a hard time until you figure out what's going on! This behavior of the Del key on a sun-color terminal is unacceptable! All that follows is related to the sun-color terminal type.

I was getting the following reading of the Del key:
  
$ read -n 1
^?

And the following reading for the Backspace key:

$ read -n 1
^H
 
And as seen above both "^?" and "^H" were set to backward-delete-char.
That's a nice way to drive you mad until you find out what to blame!
These default configurations may lock you in a tricky confusion.

NOTE
Some years later while investigating how to better adjust the layout of my keyboard while at the console, I've found out more about this issue around  "^?" and "^H".  In fact, I would have to dig more into terminal operations, but it may help to add that by default the backspace key must generate what terminal expect which is "^?" and not "^H". In fact, the best way to adjust this is not as I'll show below, but by reconfiguring the keyboard as shown on this other more recent post: Keyboard - Layouts.

SUB-OPTIMAL FIX:

To help our way out of this mess, here comes the stty shell command.
But if not used with care it may actually complicate matters even more.
This is because as per termio(7I) a DEL is supposed to be Backspace!
At the same time stty says that <BS> is "^H" and <DEL> is "^?".
For all stty options:
 
$ stty -h

The obscure solution is to tweak the terminal (tty) erase configuration.
This way, first, we restore the usual erase (Backspace) control sequence to "^h":
 
# stty erase '^h'

Or better yet, set it in the .bashrc:
 
...
if [[ "$TERM" = sun-color ]]; then
   stty erase '^h'  
fi
...
 
# stty -a
...

... erase = ^h; ...
...

Then we fix the Del key behavior by an appropriate entry in .inputrc:

...
$if term=sun-color
...
"\C-?":delete-char          # Del
...
$endif
...