Backspace and Delete key reversed

From Exterior Memory
Revision as of 20:50, 29 April 2009 by MacFreek (Talk | contribs) (Current Behaviour)

Jump to: navigation, search

Theory

Sometimes the backspace/delete does not work as expect. In a terminal or editor, pressing the backspace results in a delete command, or vice versa.

The mapping of the backspace and delete key to a given application behaviour has differed in the course of time, and per terminal and application. In fact, there are two mappings to take into account:

  • The mapping of a keystroke to a character sequence in the terminal (tty)
  • The mapping of a character sequence to a certain behaviour by an application, such as the shell (bash, tcsh, zsh) or editor (vi, emacs).

VT100

Originally, VT100 defined the keymapping:

Keystroke Glyph on keyboard ASCII Character(s) TTY representation Expected application behaviour
Control-H BS (8) ^H Erase to the left
Backspace DEL (127) ^? Erase to the right

So the delete key was undefined and the not the backspace key but control-H was mapped to the BS (backspace) character. How convenient.

Xterm

The X-term emulation of VT100 did it different:

Keystroke Glyph on keyboard ASCII Character(s) TTY representation Expected application behaviour
Control-H BS (8) ^H Erase to the left
Backspace BS (8) ^H Erase to the left
Delete DEL (127) ^? Erase to the right

This is slightly more logical, and the advantage was that the mapping of TTY character to application behaviour did not have to be changed.

VT220

The disadvantage of the X-term behaviour was that control-H could not be used by applications just like other control-shortcuts by GUI applications (e.g. control-F for find, or perhaps control-H for help).

For this reason, the behaviour was changed again, to the following mapping:

Keystroke Glyph on keyboard ASCII Character(s) TTY representation Expected application behaviour
Control-H BS (8) ^H (Passed on to application)
Backspace DEL (127) ^? Erase to the left
Delete ESC (27) + [3~ ("\e[3~") ^[[3~ Erase to the right

The disadvantage of the change is of course that both the TTY keymapping as well as the default application behaviour had to be changed.

How To Solve It

If you read this page, you most likely have problems with your current application behaviour and want to change the keystroke and characters mappings.

The VT220 mapping is more-or-less the default by most Linux distributions. You first action is to #Determine_Current_mapping make sure you are already using the VT220 mapping. You shouldn't use anything else, or you will be in trouble as soon as you log in from another computer.. Do not simply change the keystroke to character mapping. Do not deviate from the VT220 behaviour unless you Really understand what you are doing. If you use the VT220 mapping, but one or more applications still behave incorrectly, change the mapping of characters to applications instead.

Bellow are some tips to change the keymapping of applications. For completeness, it also contains information how to change the keystroke to TTY mapping.

More information can be found at:

Keystroke to TTY mapping

Determine Current mapping

You can determine the current mapping using the "control-V trick": First press control-V, then the keystroke you like to examine. This will print the characters that are sent to the terminal.

For example typing control-V + backspace prints:

^?

meaning that backspace is mapped to the DEL (127, 0x1F) character.

iTerm (Mac OS X application)

Apple Terminal application

The Mac OS X Terminal application allows you to check or uncheck "delete sends backspace" in the Window settings.

TTY to Application mapping

Vim

However, this is annoying, and there are two solutions to this problem.


The biggest problem with backspace vs. delete was caused by some terminal manufacturer many years ago.

The button on your keyboard in the upper right corner that usually has the glyphs "backspace" on it originally sent ASCII character 127 to the host. This was with vt100's or something "original" like that. IIRC they don't even have a key labeled "delete".

In the ASCII table, however, you will notice a character with the name "BS" (backspace) but it has the value 8. Some manufacturer at one time decided that the button labeled "backspace" on your keyboard should send the ASCII backspace character (8) and made the delete key send 127 (which is labeled DEL in man ascii on at least some systems).

The BS character also has the representation ^H, and the DEL character has the representation ^?.

If you look at your terminal and type "stty -a" you will see which character has the "erase" function (commonly known as "backspace"). Then you can check your terminal emulator settings to see character it sends when you press the backspace and delete keys. (At a shell simply type ^V[backspace] -- that is Ctrl-V then Backspace -- and you will see either ^H or ^?)

As you can see this is a big mess, but you just need to make sure that all your terminals and shells agree on what the backspace and delete buttons on your keyboard mean.