Bash, how to globally fix ^H and ^? backspace problems

12

5

I'd like to fix this frequent problem where the shell on a remote server thinks my terminal's backspace key is ^? and sometimes it thinks it is ^H, and happens to be incorrect and outputs the wrong character when I press backspace. If I set it to ^H or ^? with stty erase ^H or stty erase ^? in my .bashrc file, and use some other terminal to access the server, it often ends up wrong. So I'm stuck having to manually type stty erase [whatever] to fix it when I notice the backspace key is wrong.

What I'd like to do is bind both ^? and ^H to backspace, because if I can do this, I can just add it to all of my .bashrc files, and it will certainly end this nightmare. Is this possible? If so, how?

fragsworth

Posted 2013-09-04T22:28:21.977

Reputation: 417

Answers

8

This page has all the information you will ever need on this issue; I suggest you read it. Now, if you are using bash, it should be enough to create an ~/.inputrc file containing these lines:

"\e[3~": delete-char
# this is actually equivalent to "\C-?": delete-char
# VT
"\e[1~": beginning-of-line
"\e[4~": end-of-line
# kvt
"\e[H":beginning-of-line
"\e[F":end-of-line
# rxvt and konsole (i.e. the KDE-app...)
"\e[7~":beginning-of-line
"\e[8~":end-of-line

As an added bonus, they will make Home and End work as well.

terdon

Posted 2013-09-04T22:28:21.977

Reputation: 45 216

2

Most of the information in http://web.archive.org/web/20120621035133/http://www.ibb.net/~anne/keyboard/keyboard.html is indeed what you need. One correction to the information, is of their suggestion (for XTerm):

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

While this will get XTerm to send the right character, and change stty to have backspace as ^?, it will still erroniously report ^H as backspace under some occasions, breaking i.e. backspace in Vim instert mode (see here: https://bugs.gentoo.org/154090). To avoid this, use VT100.backarrowKey: false instead, so:

*VT100.backarrowKey: false
*VT100.Translations: #override \
          <Key>Delete:    string("\033[3~")\n\
          <Key>Home:      string("\033[1~")\n\
          <Key>End:       string("\033[4~")
*ttyModes: erase ^? 

(see also [please insert link here])

amosonn

Posted 2013-09-04T22:28:21.977

Reputation: 21

Read over "Why do I need 50 reputation to comment" to ensure you understand how you can start commenting.

– Pimp Juice IT – 2017-10-16T13:40:51.003

I did not require any clarification from anyone. Rather, I was providing clarification and a minor correction to the above answer. But, if this is rather documented in an answer, so be it. I added the around text making it a full answer. – amosonn – 2017-10-16T14:11:31.160

Also, this link is missing from my answer, but I can't add it there. https://wiki.archlinux.org/index.php/Xterm#Fix_the_backspace_key

– amosonn – 2017-10-16T14:14:26.057