## 3.16. Terminal Setup: Testing Window Size

I use several terminal windows of different sizes. I don't stretch the windows after I open them; instead, I set the size as I start each xterm. Here's an excerpt from my X setup file (Section 3.20) that opens the windows:

`-e` Section 5.22

>     xterm -title SETI -geometry 80x9+768+1 -e setiathome -verbose -nice 10 &
>     xterm -title "work xterm" -geometry 80x74+329-81 &

The first window has 9 rows (`80x9`) and the second has 74 rows (`80x74`).[10] I'd like the less (Section 12.3) pager to use different jump-target lines in larger windows. If the window has more than 24 lines, I want less to use its option -j3 to show search-matches on the third line of the window instead of the first.

> [10]Both windows have 80 columns. This is a Unix custom that comes from "the old days" when terminals all were 80 columns wide. But it's still a common width today -- and a good default when you don't need a wider window. Some people are even sort of weird about it, especially for reading email.

On many systems, the command stty size gives the number of rows and columns in the current window, like this:

>     $ stty size
>     74 80

Your system might need the command stty -a instead -- or it could have environment variables named LINES and COLUMNS. We'll use stty size in the following Bourne shell setup file. The set (Section 35.25) command puts the number of rows into the `$2` shell parameter. (Using set this way is portable to all shells, but it's a clumsy way to split stty's output into words. If you have a newer shell with array support, it'll be easier.) Then a series of if (Section 35.13)/then (Section 35.26) tests handle different window sizes:

>     LESS=emqc; export LESS
>     # Put number of rows into $2, configure session based on that:
>     set x `stty size`
>     if [ -z "$2" -o "$2" -lt 1 ]
>     then echo ".profile: bogus number of rows ($2) in window!?" 1>&2
>     elif [ "$2" -gt 24 ]
>     then LESS=j3$LESS
>        ...
>     fi

Additionally, you may be able to run resize on machines with the X Window System installed; it may output something like this:

>     schampeo@fugazi:1046 $ resize
>     COLUMNS=80;
>     LINES=37;
>     export COLUMNS LINES;

You may then capture the output and read it for the current setting or simply check the COLUMNS or LINES environment variables.

--JP and SJC

Copyright © 2003 O'Reilly & Associates. All rights reserved.