After window resizing, font size changes, etc., how can I easily and quickly check what is the current display width of my terminal?
3 Answers
This has been answered (and mis-answered) repeatedly. But:
tput colsprovides information that the operating system can tell you about the width.the
COLUMNSvariable may be set by your shell, but (a) it is unreliable (set in certain shells) and has the drawback that if exported will interfere with full-screen applications.the
resizeprogram can tell you the size for special cases where the terminal cannot negotiate its window-size with the operating system.
Further reading: COLUMNS in the ncurses manual page.
- 75,040
- 9
- 171
- 268
-
"interfering" with applications (full-screen or not) is not a drawback of exporting COLUMNS or LINES. It is a feature. It's how you control output height and width. e.g. `COLUMNS=80 dpkg -l '*apt*'` – cas May 07 '16 at 09:19
-
1
-
Depending on your shell, the COLUMNS (and LINES) variables may be automatically set when the window size changes. bash, zsh, ksh do. dash doesn't. tcsh doesn't.
From man bash:
COLUMNS Used by the select compound command to determine the terminal width when printing selection lists. Automatically set if the checkwinsize option is enabled or in an interactive shell upon receipt of a SIGWINCH.
and
LINES
Used by the select compound command to determine the column length for printing selection lists. Automatically set if the checkwinsize option is enabled or in an interactive shell upon receipt of a SIGWINCH.
I find the following alias useful:
$ alias ttystat='echo $(tty) $TERM ${COLUMNS}x$LINES'
$ ttystat
/dev/pts/2 xterm 192x51
- 1
- 7
- 119
- 185
There is undoubtedly a shorter way, but the following doesn't use anything but POSIX specified utilities to give a quick interactive check:
printf '0123456789\n1234567890\n' | sed '1s/./&&&&&&&&&&/g;1s/^.//;2s/.*/&&&&&&&&&&/'
Run the command and read the digits that are at the far right in your display. Read it as a two-digit number downward; that tells you how many columns you have.
Example output (shown as displayed) on a 72 column terminal:
$ printf '0123456789\n1234567890\n' | sed '1s/./&&&&&&&&&&/g;1s/^.//;2s/
.*/&&&&&&&&&&/'
000000000111111111122222222223333333333444444444455555555556666666666777
777777788888888889999999999
123456789012345678901234567890123456789012345678901234567890123456789012
3456789012345678901234567890
$
- 35,316
- 26
- 130
- 258
-
1It doesn't scale up if my terminal is 200 characters wide. `tput cols` is more generally useful. – Thomas Dickey May 07 '16 at 09:12
-
@ThomasDickey, thanks; you're correct. Generally if I come up with a hacky way to do something and am sure there must be a better way, I self-answer a question on U&L SE. I think it's better than putting the odd attempt at a solution in the question itself, where future readers must stumble through it to get to the better way. :) – Wildcard Jan 26 '17 at 10:10