Difference between revisions of "Backspace and Delete key reversed"

From Exterior Memory
Jump to: navigation, search
(How To Solve It)
(Determine Current mapping)
Line 97: Line 97:
  stty -a
  stty -a
look for the line
erase = ^?
If it lists erase = ^H, then you have the wrong keymapping.
===iTerm (Mac OS X application)===
===iTerm (Mac OS X application)===

Revision as of 23:52, 29 April 2009


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).


Originally, VT100 defined the keymapping:

Keystroke Glyph on keyboard ASCII Character(s) TTY representation Expected application behaviour
Control-H BS (8, 0x08) ^H Erase to the left
Backspace DEL (127, 0x7F) ^? 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.


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, 0x08) ^H Erase to the left
Backspace BS (8, 0x08) ^H Erase to the left
Delete DEL (127, 0x7F) ^? 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.


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, which is now the default by most Linux distributions:

Keystroke Glyph on keyboard ASCII Character(s) TTY representation Expected application behaviour
Control-H BS (8, 0x08) ^H (Passed on to application)
Backspace DEL (127, 0x7F) ^? Erase to the left
Delete ESC (27, 0x1B) + [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 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 application behaviour instead.

The rest of this article contains information to make sure your terminal uses the VT220 keymapping, and the applications make use of this.

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.

Make sure that you are currently using the VT220 mapping:

Keystroke to Type Expected Output (for VT220 mapping)
control-V backspace ^?
control-V delete ^[[3~
control-V control-H ^H

If it isn't, change the mapping, as explained in this section. If it is correct, do not change the terminal settings, but instead, change the application behaviour.

An alternative way to find the keymap is to use the stty command:

stty -a

look for the line

erase = ^?

If it lists erase = ^H, then you have the wrong keymapping.

iTerm (Mac OS X application)

iTerm is an alternative to Apple's Terminal. By default it comes with two Keyboard profiles, Global and xterm. Both profiles use the correct (VT220) mapping by default.

Unfortunately, the key names are ill-defined. "del" refers to the delete key, while "delete" refers to the backspace key.

The xterm defines the "delete" (= backspace, really) key to be mapped to "send hex 7f". Indead, it sends ^?. This is the correct default for the backspace key.

If you want to make the keys explicit, set:

delete (backspace key) send hex code "7F"
del (delete key) send escape sequence "[3~"

Apple Terminal application

The default behaviour of the Apple's Terminal application is good.

In the Preference dialog, Setting item:

  • In the Keyboard tab, make sure that "forward delete" maps to "\033[3~"
  • In the Advanced tab, make sure the "Delete sends Ctrl-H" checkbox is off

TTY to Application mapping


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.