Stty settings are pathologically altered

sttytmuxxterm

I just set up a new computer, and as usual had to alter the settings in xterm in order to make the delete keys work properly. (Ctrl-H sends ^H, backspace sends ^?, delete sends ^[[3~. This is, of course, the objectively correct way to do it.) While the default xterm settings are problematic, in this setup everything works fine, at least on xterm's end.

The problem is that, for some reason, the stty settings in an xterm are always set to erase = ^H. As well as messing things up in non-readline standard input, this also makes tmux begin silently translating ^H to ^? in its windows, which makes things like emacs rather painful.

I have no idea why stty is set this way. It isn't the default setting; typing stty alone to display differences from default shows the erase = ^H; line, and manually typing stty erase ^? removes this line. (This also fixes the issues with stdin and tmux.) However, typing this in every terminal I start is tedious, and while I could put it in .bashrc or something, this strikes me as not being the right way to do it.

What is it that causes stty to use this particular incorrect, non-default setting? And how can I make it stop?

Best Answer

There are a lot of ways to do it. The way you mention could be one. xterm is a program that runs another one - it wraps another program in a pty - usually your shell - and channels the input you feed it to the wrapped programs. The thing about pseudo-terminals is they are just emulated devices - and so xterm takes a guess at the device you'll eventually be typing at it on. Of course, you can get a lot more specific. xterm honors all kinds of environment variables - and, better still, xresources.

From man xterm:

  • ttyModes (class TtyModes)
    • Specifies a string containing terminal setting keywords and the characters to which they may be bound. Allowable keywords include: brk, dsusp, eof, eol, eol2, erase, erase2, flush, intr, kill, lnext, quit, rprnt, start, status, stop, susp, swtch and weras. Control characters may be specified as ^char (e.g., ^c or ^u) and ^? may be used to indicate delete (127). Use ^- to denote undef. Use \034 to represent ^\, since a literal backslash in an X resource escapes the next character.
    • This is very useful for overriding the default terminal settings without having to do an stty every time an xterm is started. Note, however, that the stty program on a given host may use different keywords; xterm's table is built-in.
    • If the ttyModes resource specifies a value for erase, that overrides the ptyInitialErase resource setting, i.e., xterm initializes the terminal to match that value.
Related Question