Bash backspace deleting one word at a time
Some terminals send ^h
(character number 8) for the BackSpace key, and some send ^?
(character number 127). Many terminal emulators can be configured, and most programs can be configured to know which key to expect. Obviously the two sides must agree.
Stty is a way in which applications can query the terminal's configuration. erase = ^?
means that your terminal description file on the server claims that when your terminal sends ^?
, it means “erase the previous character”. werase = ^?
means that ^?
means “erase the previous word”. There's a contradiction between these two settings.
When you log in over ssh, the ssh client sends a name for the terminal, which is put in the TERM
environment variable. Programs on the server then look up descriptions of the terminal in a database (called termcap or terminfo). If these descriptions are broken, or if a configuration file somewhere (such as /etc/profile
or ~/.login
or ~/.bashrc
) overrides the descriptions with wrong data, you might find that you have a mismatch. For example, one possible source for your problem is if some initialization file contains the command stty werase '^?'
(forcing ^?
to mean a word erase) while your terminal sends ^?
for the backspace key.
Ideally, you should fix the conflict, as a lot of programs will read this information.
In Putty, you can configure which of ^?
or ^h
the BackSpace key sends. Check the value of this setting. Maybe the easiest way to fix your problem is to make Putty send ^h
for BackSpace, and make sure the server uses that setting (stty erase '^h'
, to be put in ~/.bashrc
, will force it).
Related videos on Youtube
Here Be Wolves
Updated on September 17, 2022Comments
-
Here Be Wolves over 1 year
There is a server at my work that uses csh as the default shell. I prefer bash, so whenever I login, I start
bash
.But for some reason, the backspace button deletes complete words instead of just deleting characters. Why is that? How do I change this and have the Bksp delete char by char??
FYI, Shift+Bksp deletes character by character. If possible, I'd like to have Shift+Bksp delete word by word. Only if possible.
Thanks
PS: Please don't tell me to hold the shift key; its downright annoying.
-
Sirex over 13 yearsare you using putty or similar to connect, or is this from anouther nix machine ?
-
Dennis Williamson over 13 yearsIf you do
stty -a
, does theerase
parameter say "^?". If you press Ctrl-v then Backspace, do you see "^?"? Another possibility you may see is "^H". What terminal software are you using? Does Backspace work normally in csh? -
Warnaud over 13 yearsI would check the ˜/.bashrc or ˜/.bash_profile or the same files from /etc/skel if they don't exist in your home folder
-
Here Be Wolves over 13 years@Sirex Yes, I'm using putty to connect. @Dennis yes,
stty -a
sayserase = ^?
. I'm using Putty. Yes, Bksp works okay withcsh
. Additionally, I seewerase = ^?
instty -a
output. -
Here Be Wolves over 13 years@Warnaud what would I check these files for? What am I looking for?
-
Warnaud over 13 yearsFirst look if they exist (the ˜/.bashrc and ˜/.bash_profile) rename them to see which one add this behaviour. If they don't exist, try commenting suspicious lines in /etc/bashrc or post them all here so we can look at them
-
Gilles 'SO- stop being evil' over 13 yearsAlso, what is the OS on the server (output of
uname -mrs
)? -
Here Be Wolves over 13 years@Gilles By default
TERM
is set toxterm
, but my.bashrc
changes it toansi
, without any consequence. Output ofuname -mrs
isLinux 2.4.21-40.ELsmp i686
.
-
-
Here Be Wolves over 13 yearsWhy is it that csh runs okay, but bash has problems with this? When I run
stty -a
I seeerase
as well aswerase
set to '^?. PS: I changed putty's backspace character to
^H` and backspace started working fine. But is there something I can put in .bashrc so I don't have to change putty's config? -
Gilles 'SO- stop being evil' over 13 years@Here: Since there are contradictory indications about
^?
, it could be that csh just happens to read the terminal settings in a different order from bash. You can putstty erase='^W'
(the usual setting) in your.bashrc
, then you won't have contradictory settings any more and^?
sent byBackSpace
should erase a single character.