Difference between revisions of "Backspace and Delete key reversed"

From Exterior Memory
Jump to: navigation, search
(Terminal (terminfo))
(Keystroke to TTY mapping)
Line 103: Line 103:
 
If it lists <tt>erase = ^H</tt>, then you have the wrong keymapping.
 
If it lists <tt>erase = ^H</tt>, then you have the wrong keymapping.
  
===Terminal (terminfo)===
+
===Xterm===
  
'''Warning: I do no longer recommend to fiddle with the terminfo settings. It is a hassle and there are often better ways to achieve the same results. See above.'''
+
If you have problem with mappings keys in Xterm, create the following <tt>.Xdefaults</tt> file in your home directory:
  
Run the command
+
  *VT100.Translations: #override \
  infocmp
+
              <Key>BackSpace: string(0x7F)\n\
And look for the settings:
+
              <Key>Delete:    string("\033[3~")
 +
*ttyModes: erase ^?
  
{|
+
===loadkeys and xmodmap===
! Keystroke !! Expected Mapping
+
|-
+
| kbs (backspace key) || <tt>\177</tt> (or <tt>^?</tt>)
+
|-
+
| kdch1 (delete key)  || <tt>\E[3~</tt>
+
|}
+
  
If this is incorrect, you may want to try another terminfo setting, or (if you are the daring type) create your own terminfo file.
 
  
The terminfo files are typically stored in the <tt>/usr/share/terminfo/</tt> directory. Run <tt>toe</tt> to get a (long) list of all available terminal types:
+
Getting the delete key to work : xmodmap -e 'keycode 59 = Delete' or xmodmap -e 'keysym BackSpace = Delete'
toe            # on BSD-like systems
+
toe -a          # on Linux-like systems
+
  
For example, this term is wrong:
 
% infocmp xterm | grep kbs
 
kPRV=\E[5;2~, kRIT=\E[1;2C, kb2=\EOE, kbs=^H, kcbt=\E[Z
 
  
While this xterm-color (on another machine) is correct:
+
===X windows===
% infocmp xterm-color | grep kbs
+
is2=\E[m\E[?7h\E[4l\E>\E7\E[r\E[?1;3;4;6l\E8, kbs=\177,
+
 
+
If you found to correct one, set $TERM=xterm-color to pick the correct xterm. If you can't locate a good option on your machine, copy it (from another machine) to <tt>~/.terminfo/x/</tt> and set $TERM to whatever file yu copied (thus <tt>export TERM=xterm-color</tt> if you copied <tt>~/.terminfo/x/xterm-color</tt>)
+
 
+
If you want to create your own custom terminfo file, know that ther terminfo files are compiled. You can extract them using <tt>infocmp</tt> and compile them with <tt>tic</tt>.
+
 
+
===Terminfo===
+
 
+
 
+
===Xterm GUI===
+
  
  
Line 167: Line 145:
 
| del (delete key) || send escape sequence "[3~"
 
| del (delete key) || send escape sequence "[3~"
 
|}
 
|}
 +
 +
===Terminal (terminfo)===
 +
 +
''Warning: I do no longer recommend to fiddle with the terminfo settings. It is a hassle and there are often better ways to achieve the same results. See above.''
 +
 +
Run the command
 +
infocmp
 +
And look for the settings:
 +
 +
{|
 +
! Keystroke !! Expected Mapping
 +
|-
 +
| kbs (backspace key) || <tt>\177</tt> (or <tt>^?</tt>)
 +
|-
 +
| kdch1 (delete key)  || <tt>\E[3~</tt>
 +
|}
 +
 +
If this is incorrect, you may want to try another terminfo setting, or (if you are the daring type) create your own terminfo file.
 +
 +
The terminfo files are typically stored in the <tt>/usr/share/terminfo/</tt> directory. Run <tt>toe</tt> to get a (long) list of all available terminal types:
 +
toe            # on BSD-like systems
 +
toe -a          # on Linux-like systems
 +
 +
For example, this term is wrong:
 +
% infocmp xterm | grep kbs
 +
kPRV=\E[5;2~, kRIT=\E[1;2C, kb2=\EOE, kbs=^H, kcbt=\E[Z
 +
 +
While this xterm-color (on another machine) is correct:
 +
% infocmp xterm-color | grep kbs
 +
is2=\E[m\E[?7h\E[4l\E>\E7\E[r\E[?1;3;4;6l\E8, kbs=\177,
 +
 +
If you found to correct one, set $TERM=xterm-color to pick the correct xterm. If you can't locate a good option on your machine, copy it (from another machine) to <tt>~/.terminfo/x/</tt> and set $TERM to whatever file yu copied (thus <tt>export TERM=xterm-color</tt> if you copied <tt>~/.terminfo/x/xterm-color</tt>)
 +
 +
If you want to create your own custom terminfo file, know that ther terminfo files are compiled. You can extract them using <tt>infocmp</tt> and compile them with <tt>tic</tt>.
  
 
==TTY to Application mapping==
 
==TTY to Application mapping==

Revision as of 01:39, 30 April 2009

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

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

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, 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 (but less reliable) 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.

Xterm

If you have problem with mappings keys in Xterm, create the following .Xdefaults file in your home directory:

*VT100.Translations: #override \
             <Key>BackSpace: string(0x7F)\n\
             <Key>Delete:    string("\033[3~")
*ttyModes: erase ^? 

loadkeys and xmodmap

Getting the delete key to work : xmodmap -e 'keycode 59 = Delete' or xmodmap -e 'keysym BackSpace = Delete'


X windows

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

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~"

Terminal (terminfo)

Warning: I do no longer recommend to fiddle with the terminfo settings. It is a hassle and there are often better ways to achieve the same results. See above.

Run the command

infocmp

And look for the settings:

Keystroke Expected Mapping
kbs (backspace key) \177 (or ^?)
kdch1 (delete key) \E[3~

If this is incorrect, you may want to try another terminfo setting, or (if you are the daring type) create your own terminfo file.

The terminfo files are typically stored in the /usr/share/terminfo/ directory. Run toe to get a (long) list of all available terminal types:

toe             # on BSD-like systems
toe -a          # on Linux-like systems

For example, this term is wrong:

% infocmp xterm | grep kbs
kPRV=\E[5;2~, kRIT=\E[1;2C, kb2=\EOE, kbs=^H, kcbt=\E[Z

While this xterm-color (on another machine) is correct:

% infocmp xterm-color | grep kbs
is2=\E[m\E[?7h\E[4l\E>\E7\E[r\E[?1;3;4;6l\E8, kbs=\177,

If you found to correct one, set $TERM=xterm-color to pick the correct xterm. If you can't locate a good option on your machine, copy it (from another machine) to ~/.terminfo/x/ and set $TERM to whatever file yu copied (thus export TERM=xterm-color if you copied ~/.terminfo/x/xterm-color)

If you want to create your own custom terminfo file, know that ther terminfo files are compiled. You can extract them using infocmp and compile them with tic.

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.