Home / Alt manpages / tset(1)

  • tset(1)
  • User command
  • linux

Recover a Broken Linux Terminal with reset and tset

You will learn how to recover a terminal after a program leaves it in raw or otherwise confusing mode, check the terminal type, and initialise TERM when it is wrong. The examples match the ncurses 6.4 tset and reset installed by Ubuntu's ncurses-bin package, version 6.4+20240113-1ubuntu2.2.

Allow about ten minutes. You need a shell attached to the terminal you want to repair. No command here needs sudo. Terminal repair changes the current terminal's settings, but it does not alter files, services or system configuration.

1. Recover the terminal with reset

Run reset in the affected terminal:

$ reset

The command sends the terminal's reset sequence and restores sane line settings: cooked input and echo are enabled, raw and cbreak modes are disabled, newline translation is enabled, and unset special characters are restored to defaults. The screen may redraw or show control characters while this happens. That is expected.

Checkpoint

Type a short command such as printf '%s\n' recovered. You should see the command as you type it and then see:

recovered

If the terminal does not accept the Return key, press Control-J, which is normally the line-feed character, then type reset and press Control-J again. The command may not echo while the terminal is damaged. This is the recovery path to remember when the keyboard appears to stop working.

2. Check which terminal type tset sees

Once input works again, ask tset to report the terminal type without initialising anything:

$ tset -q
xterm

-q is a query: it writes the detected type to standard output and does not change the terminal. The result depends on TERM and the terminal database, so your output may be screen, tmux, linux or another valid name.

For a status-only check in a script, use the command's exit status rather than parsing the name:

if tset -q >/tmp/terminal-type; then
    printf 'terminal type: %s\n' "$(cat /tmp/terminal-type)"
else
    status=$?
    printf 'tset could not identify the terminal (status %s)\n' "$status" >&2
    exit "$status"
fi

The temporary file is only an example for a script that needs to pass the value to another command. In an interactive shell, terminal_type=$(tset -q) is shorter. If tset reports that it cannot obtain terminal attributes, check that the command is running on a real terminal rather than a pipe, redirected input or a detached job.

3. Reinitialise a terminal deliberately

reset is the normal recovery command. Use tset when you also need terminal initialisation or want to select a specific type:

$ tset -I xterm
xterm

The argument forces the terminal type to xterm. Replace it with the type that describes the terminal emulator you are actually using. The -I option prevents terminal and tab initialisation strings being sent; it is useful when you only want the mode and type handling without another screen reset. Without -I, tset can write initialisation data to the terminal and wait briefly after a hardware reset.

Do not guess the type merely because the name sounds familiar. A mismatch can produce broken function keys, colours or screen control. Start with the existing value:

$ printf '%s\n' "$TERM"
xterm
$ tset -q
xterm

If those values disagree, investigate the terminal emulator, multiplexer or login environment before putting a new value into a shell profile.

4. Set TERM for the current shell only

When TERM is missing or incorrect, tset -s prints shell commands that set it. With a POSIX shell, ask for the commands and evaluate them in the current shell:

eval "$(tset -s -I xterm)"
printf '%s\n' "$TERM"

The final line should print xterm. tset chooses POSIX shell syntax unless SHELL ends in csh. The -s output is intended to be evaluated; running tset -s by itself only prints text and cannot change the parent shell's environment.

This example changes only the current shell. That is a useful checkpoint: if the new value fixes the session, make a permanent profile change separately and only after confirming the terminal type. Do not put a guessed value in ~/.profile merely to silence an error. A new login on a different emulator may need a different value.

5. Avoid the common traps

  • Do not use reset in a pipeline as if it were a text filter. It needs terminal attributes and writes control sequences to the terminal.
  • Do not use sudo for terminal repair. It can operate on a different controlling terminal and does not grant a damaged terminal a useful setting.
  • Do not confuse -q with recovery. It only reports the type; it intentionally does not initialise or reset anything.
  • Do not overwrite a working profile without a fallback. If you test a persistent change, keep another login or terminal open so you can undo it.

If reset prints an error, confirm that the session has a controlling terminal with tty. If tset -q names an unknown terminal, check whether its terminfo entry exists and whether TERM was inherited from a multiplexer or remote login. The command cannot invent a missing terminal description.

Done means

  • The affected terminal echoes input and accepts Return after reset, or the Control-J recovery path.
  • tset -q reports the terminal type without changing terminal state.
  • The chosen type matches the actual emulator, remote session or multiplexer.
  • Any TERM change made with tset -s is limited to the current shell until deliberately reviewed.
  • No elevated privileges, service restart or permanent configuration change was needed for ordinary recovery.