13

On DE we can resize terminal (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?

20

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.

|improve this answer|||||
  • sir is it not possible to get output in terms of pixels ? – Edward Torvalds Apr 19 '15 at 1:54
  • Note that in bash the echo command in this answer will not see the variables, since the read in the pipeline will be executed in a subshell. (It works in ksh, though.) For bash you may want to use, e.g., read myrows mycols < <( stty size ). – Janis Apr 19 '15 at 1:59
1

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
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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