Discussion:
`reset` command makes backspace unusable
(too old to reply)
崔灏 (CUI Hao)
2018-05-29 09:10:01 UTC
Permalink
Hi, new to FreeBSD. On Linux, I often use `reset` command to recover
an broken terminal. On FreeBSD, `reset` seems to render backspace
unusable in some CLI programs.

Steps to reproduce the problem:
1. SSH login FreeBSD server from a Linux terminal emulator. TERM is
set to `xterm-256color`.
I tried Konsole / Termite. Both reproducible.
But xterm/uxterm doesn't trigger the problem.
2. Executing `reset` on FreeBSD.
3. Then pressing backspace will yields `^?` instead of backward
delete in most CLI programs including bash/vim/cat, but not tcsh and
sh.

Like this:
***@wmc:~$ reset
Erase is backspace.
***@wmc:~$ ^?^?^?^? # typing backspace...

I googled about the problem. `stty erase ^?` after `reset`, or `reset
-e ^?` fix it. But I still wonder why `reset` doesn't work like on
Linux. I do some investigation, here are some of my observations:

1. terminal line settings (`stty -a`)
I checked the output of `stty -a`. On Linux terminal (Termite), I saw
`erase = ^?`. For full output, see https://cfp.vim-cn.com/cbff6

After SSH login FreeBSD, `stty -a` shows `erase = ^?; erase2 = ^H`,
which seem to be consistent with Linux. See
https://cfp.vim-cn.com/cbff7

But after `reset`, the settings changed and becomes `erase = ^H;
erase2 = ^H`. see https://cfp.vim-cn.com/cbff8

2. termcap
It seems default terminal line settings are determined by termcap db.
I checked `/etc/termcap` and found the entry of `xterm-256color`,
which inherits settings of `xterm-basic` entry. `xterm-basic` sets
`kb=^H`.
On Linux, I get the termcap settings by `infocmp -Cr`. Terminal
`xterm-256color` sets `kb=^H`, while `xterm-termite` sets `kb=\177`.

I tried to override FreeBSD termcap settings in `~/.termcap`:
xterm-256color|xterm alias 3:\
:Co#256:pa#32767:\
:kb=\177:\
:AB=\E[48;5;%dm:AF=\E[38;5;%dm:tc=xterm-new

Above settings indeed works and `reset` won't alter erase setting.
However, Linux `xterm-256color` termcap does set `kb=^H` like FreeBSD,
but it doesn't cause the problem on Linux.

Although I have found several workaround to fix backspace problem, may
anyone explain why terminal settings inconsistent on Linux and
FreeBSD? Is this a bug or just some compatibility issues? Are there
other compatibility issues with Linux terminal emulator?
--
崔灏 / CUI Hao
Homepage: i-yu.me
Twitter: @cuihaoleo
Polytropon
2018-05-31 07:11:29 UTC
Permalink
Post by 崔灏 (CUI Hao)
Hi, new to FreeBSD. On Linux, I often use `reset` command to recover
an broken terminal. On FreeBSD, `reset` seems to render backspace
unusable in some CLI programs.
1. SSH login FreeBSD server from a Linux terminal emulator. TERM is
set to `xterm-256color`.
I tried Konsole / Termite. Both reproducible.
But xterm/uxterm doesn't trigger the problem.
2. Executing `reset` on FreeBSD.
3. Then pressing backspace will yields `^?` instead of backward
delete in most CLI programs including bash/vim/cat, but not tcsh and
sh.
Erase is backspace.
This indicates that backspace's ^H has been altered to ^?, which
usually is delete, but it only works when the "support chain" does
react correctly to this code (the C shell sometimes doesn't, which
is why you see the "replacement text" instead of the shell per-
forming the correct edit action).
Post by 崔灏 (CUI Hao)
I googled about the problem. `stty erase ^?` after `reset`, or `reset
-e ^?` fix it. But I still wonder why `reset` doesn't work like on
1. terminal line settings (`stty -a`)
I checked the output of `stty -a`. On Linux terminal (Termite), I saw
`erase = ^?`. For full output, see https://cfp.vim-cn.com/cbff6
Here is some relevant output to compare:

% stty -a
[...]
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
eol2 = <undef>; erase = ^H; erase2 = ^H; intr = ^C; kill = ^U;
lnext = ^V; min = 1; quit = ^\; reprint = ^R; start = ^Q;
status = ^T; stop = ^S; susp = ^Z; time = 0; werase = ^W;

Note that backspace is called "erase" here, and correctly set to ^H,
while there is no mentioning of delete (see further notes).
Post by 崔灏 (CUI Hao)
After SSH login FreeBSD, `stty -a` shows `erase = ^?; erase2 = ^H`,
which seem to be consistent with Linux. See
https://cfp.vim-cn.com/cbff7
This makes the DEL key the backspace key, if I read it correctly.
Post by 崔灏 (CUI Hao)
But after `reset`, the settings changed and becomes `erase = ^H;
erase2 = ^H`. see https://cfp.vim-cn.com/cbff8
Now ^H equals backspace (here: "erase"), which is what should
happen when the terminal capabilities are reset according to
the database.
Post by 崔灏 (CUI Hao)
2. termcap
It seems default terminal line settings are determined by termcap db.
I checked `/etc/termcap` and found the entry of `xterm-256color`,
which inherits settings of `xterm-basic` entry. `xterm-basic` sets
`kb=^H`.
Yes, that again is "key backspace" with the correct setting.
Post by 崔灏 (CUI Hao)
On Linux, I get the termcap settings by `infocmp -Cr`. Terminal
`xterm-256color` sets `kb=^H`, while `xterm-termite` sets `kb=\177`.
Key code 177 (0x7F or 127) is a problematic key code because
it sometimes means backspace, sometimes delete.
Post by 崔灏 (CUI Hao)
xterm-256color|xterm alias 3:\
:Co#256:pa#32767:\
:kb=\177:\
:AB=\E[48;5;%dm:AF=\E[38;5;%dm:tc=xterm-new
Above settings indeed works and `reset` won't alter erase setting.
However, Linux `xterm-256color` termcap does set `kb=^H` like FreeBSD,
but it doesn't cause the problem on Linux.
This kind of modification should not be needed. However, you
can do certain customization to your C shell's settings (and
let's assume for now you're using the C shell as your dialog
shell, simply because this is FreeBSD's default).

Add the following settings to /etc/csh.cshrc (for global effect)
or to ~/.cshrc (for user-local effect):

if ($?prompt) then
# ... stuff omitted ...
bindkey ^? delete-char # delete for console
bindkey ^[[3~ delete-char # delete for xterm
# ... more stuff ...
endif

You can also experiment with the "ESC sequence spelling" of the
key codes (using "\e" instead of "^["), like this:

bindkey "\e[3~" delete-char # delete
bindkey "\e[1~" beginning-of-line # home
bindkey "\e[4~" end-of-line # end

Note that backspace will be reset to ^H which is the correct
setting for this key, so there should be no need to add it.

You can still _always_ simply press Ctrl+H to check if it does
correctly perform the backspace operation on the current line.
Post by 崔灏 (CUI Hao)
Although I have found several workaround to fix backspace problem, may
anyone explain why terminal settings inconsistent on Linux and
FreeBSD? Is this a bug or just some compatibility issues? Are there
other compatibility issues with Linux terminal emulator?
This is probably because FreeBSD's console subsystem currently
still is in a flow, and Linux is always in a flow, so there is
no real agreement about how things should be solved in a cross-
compatible way... ;-)
--
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...
Loading...