0

After window resizing, font size changes, etc., how can I easily and quickly check what is the current display width of my terminal?

  • 1
    stty size will report all the info you need which you can then parse, cut, etc. – bsd May 7 '16 at 9:27
3

This has been answered (and mis-answered) repeatedly. But:

  • tput cols provides information that the operating system can tell you about the width.

  • the COLUMNS variable 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 resize program 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.

| improve this answer | |
  • "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 7 '16 at 9:19
  • 1
    I get bug reports, you don't. Bye. – Thomas Dickey May 7 '16 at 9:20
  • learn how to handle those vars correctly then. ttfn. – cas May 7 '16 at 9:21
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
| improve this answer | |
0

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
$
| improve this answer | |
  • 1
    It doesn't scale up if my terminal is 200 characters wide. tput cols is more generally useful. – Thomas Dickey May 7 '16 at 9: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

Not the answer you're looking for? Browse other questions tagged or ask your own question.