Within a desktop environment we can resize terminals (GNOME Terminal for example) for our convenience.
How can I know the size of the terminal in terms of pixels or number of columns and rows?
Within a desktop environment we can resize terminals (GNOME Terminal for example) for our convenience.
How can I know the size of the terminal in terms of pixels or number of columns and rows?
If you issue the command
stty size
it returns the size of the current terminal in rows and columns. Example:
$ stty size
24 80
You can read the rows and columns into variables like this (thanks to Janis' comment):
$ read myrows mycols < <(stty size)
Obtaining the size in pixels requires knowledge of your screen's resolution and I don't think stty
has direct access to such information.
In a desktop environment, you are using X, and the xwininfo
utility can show the size of the window in pixels. Also, if you are running on the desktop (and not, for instance, remotely connected), the terminal emulator provides a variable $WINDOWID
which you can use as a parameter to xwininfo
, e.g.,
xwininfo -id $WINDOWID
and get a listing list this:
xwininfo: Window id: 0xc00025 "uxterm"
Absolute upper-left X: 65
Absolute upper-left Y: 167
Relative upper-left X: 0
Relative upper-left Y: 22
Width: 624
Height: 577
Depth: 24
Visual: 0x22
Visual Class: TrueColor
Border width: 1
Class: InputOutput
Colormap: 0x21 (installed)
Bit Gravity State: NorthWestGravity
Window Gravity State: NorthWestGravity
Backing Store State: NotUseful
Save Under State: no
Map State: IsViewable
Override Redirect State: no
Corners: +65+167 -589+167 -589-256 +65-256
-geometry 103x42+65+145
In this example, the lines with Width
and Height
are the size in pixels. The last line, with -geometry
gives the size in characters (as well as the position of the upper-left corner — in pixels).
Talking of resizing the window, the resize
program shows the number of lines and columns. For this example, it shows
$ resize
set noglob;
setenv COLUMNS '103';
setenv LINES '42';
unset noglob;
The question did not indicate how the information might be used, but since the output is text, in a predictable format, it is readily scripted. Here is a simple example using awk:
#!/bin/sh
if [ -n "$WINDOWID" ]
then
xwininfo -id $WINDOWID | awk '
BEGIN { px = 0; py = 0; chars = "?x?"; }
/Height:/ { py = $2; }
/Width:/ { px = $2; }
/-geometry/ { chars = $2; sub("+.*","",chars); }
END { printf "%dx%d pixels, %s chars\n", py, px, chars; }'
else
printf '? no WINDOWID found\n'
fi
which prints
577x624 pixels, 103x42 chars
GNOME Shell 3.36.7
session doesn't brings WINDOWID
variable set :(
– artu-hnrq
Apr 11 '21 at 22:59
bash
theecho
command in this answer will not see the variables, since theread
in the pipeline will be executed in a subshell. (It works inksh
, though.) Forbash
you may want to use, e.g.,read myrows mycols < <( stty size )
. – Janis Apr 19 '15 at 01:59resize
which outputs a shell script to set the size in environment variables. – symcbean Sep 15 '21 at 13:50