Terminal size wrong after resize: stty size, resize and COLUMNS
When vi, less or top use only part of the window, the host has the wrong size. Check stty size, set it with stty rows and cols or resize, drop fixed COLUMNS.
If full-screen programs such as vi, less or top draw in only part of the window, wrap lines too early, or run off the bottom, the host has the wrong idea of your terminal size. Programs on the host do not look at your window. They read the size the host's terminal driver holds, and that number has to be sent to it.
Check what the host thinks
stty size
It prints rows, then columns, for example 24 80. Compare that with what you see. If the numbers match the window and one program is still wrong, that program is reading a fixed value (see "COLUMNS and LINES set by hand" below).
How the size normally reaches the host
- SSH sends the size when the session asks for a terminal, and sends a
window-changemessage each time your window changes size (RFC 4254, sections 6.2 and 6.7). - Telnet sends it only if both sides agree to the NAWS option (Negotiate About Window Size, RFC 1073). Without NAWS the host never learns your size and keeps a default, often 24 by 80.
- A serial console has no way to send it at all.
When the size held by the terminal driver changes, the kernel sends the SIGWINCH signal to the foreground process group, and a program that handles it redraws at the new size. A program that ignores the signal keeps the old size until it restarts.
Set the size by hand
Tell the terminal driver the right numbers:
stty rows 40 cols 120
Running programs get SIGWINCH and the shell updates its variables. Use this on a serial console, or on a Telnet host without NAWS.
To have the terminal report its own size, use resize (it comes with xterm). It asks the terminal for its size with an escape sequence, updates the terminal settings that stty size shows, and prints shell commands that set COLUMNS and LINES:
eval "$(resize)"
Run it again after each resize; nothing updates it automatically on a link that cannot send the size.
COLUMNS and LINES set by hand
Programs built with ncurses treat COLUMNS and LINES in the environment as an override of the real size. If a fixed value is exported, they keep using it after every resize.
Bash keeps both variables current: it sets them on SIGWINCH in an interactive shell, and after each external command when the checkwinsize option is on, which is the default. If they are exported, the new values are exported too. Other shells, such as dash, do not touch them, so an export COLUMNS=80 in .profile stays at 80 whatever the window does. Check with:
stty size
env | grep -E '^(COLUMNS|LINES)='
If env shows numbers that differ from stty size, find the line that exports them and remove it, or run unset COLUMNS LINES for the current session.
To turn on checkwinsize in bash if a system has it off:
shopt -s checkwinsize
Inside tmux
tmux sets the size of its windows from the attached clients, following its window-size option. With smallest, or with manual, a window can stay smaller than your screen. Run stty size inside and outside tmux to compare. See Shell and tmux basics.
The prompt line wraps wrongly but full-screen programs are fine
Then the size is right and the prompt is the problem: color codes in PS1 that readline counts as text. See Bash prompt wraps onto the same line: mark color codes in PS1.
Related
- Bash prompt wraps onto the same line: mark color codes in PS1
- TERM and terminfo
- Shell and tmux basics
- Telnet echo, typed characters twice, missing or visible passwords
- Fix a terminal full of garbage after printing a binary file
- tmux fills part of the screen with dots: another client is attached at a different size
Sources
- RFC 4254, sections 6.2 and 6.7: pty-req and window-change
- RFC 1073: Telnet Window Size Option (NAWS)
- stty(1): size, rows, cols
- TIOCSWINSZ(2const): SIGWINCH on a size change
- resize(1), xterm
- Bash Reference Manual: The Shopt Builtin (checkwinsize)
- Bash Reference Manual: Bash Variables (COLUMNS, LINES)
- ncurses(3X): ENVIRONMENT, COLUMNS and LINES
- tmux(1): window-size