Resetting the terminal

terminal

In the Terminal Shell menu, there are two options for reset: Hard Reset and Reset. The former clears your screen; the latter just fixes it after nasty escape sequences.

It appears that Hard Reset is equivalent to the bash builtin reset. What is Reset (the softer version) equivalent to? stty sane?

(I would like to reset my terminal after an SSH connection disconnects without clearing the screen; the menu option is working for me but I would like it to be automatic.)

Best Answer

Probably stty sane though there are also various reset strings that vary by terminal and the TERM type (rs1 and ilk, see terminfo(5)) that may or may not be in use, and it's hard to debug something that is usually intercepted by the terminal itself to figure out what the exact bytes are. For example Terminal.app with TERM=xterm shows different reset escape sequences than TERM=xterm-256color does:

% infocmp | grep rs'[123]'
    rmul=\E[24m, rs1=\Ec, rs2=\E[!p\E[?3;4l\E[4l\E>, sc=\E7,
% echo $TERM
xterm
% TERM=xterm-256color infocmp | grep rs'[123]'
    rmso=\E[27m, rmul=\E[24m, rs1=\Ec\E]104\007,
    rs2=\E[!p\E[?3;4l\E[4l\E>, sc=\E7,

For automatic cleanup a shell function that wraps ssh with may suffice; the following one takes efforts to try to preserve the shell exit code (which is not the same as the 16-bit exit status word actually returned by a program) and resets various things. Whether this suffices for you depends on exactly what the remote end is doing and exactly how you want to clean that up.

function ssh() {
   local ret
   command ssh "$@"
   ret=$?
   stty sane
   # (optional) reset string, see terminfo(5)
   tput rs1
   # (optional) clear junk linux likes to spam terminal title bar with
   printf "\033]2;\007"
   return $ret
}

More methods to reset the terminal.