Center a window via command line

21,123

Solution 1

wmctrl tool provides command line access to almost all the features defined in the EWMH (Extended Window Manager Hints) specification. It can be used, for example, to get information about the window manager, to get a detailed list of desktops and managed windows, to switch and resize desktops, to make windows full-screen, always-above or sticky, and to activate, close, move, resize, maximize and minimize them.

You can install it by

sudo apt-get install wmctrl

You can get information about your virtual desktops (workspaces) with wmctrl -d

one@onezero:~$ wmctrl -d
0  * DG: 2720x1536  VP: 0,0  WA: 0,24 1360x744  N/A

And list open windows with wmctrl -l. The -G option shows you the geometry of the windows:

one@onezero:~$ wmctrl -l
0x02000004  0 onezero Desktop
0x02e00002  0     N/A DNDCollectionWindow
0x02e00003  0     N/A launcher
0x01e00004  0 onezero cairo-dock
0x02e00004  0     N/A panel
0x04800061  0 onezero Transmission
0x02e0000a  0     N/A Dash
0x03a00044  0 onezero arranging windows from the gnu/linux command line with wmctrl ~ Moving to Freedom - Chromium
0x04400006  0 onezero one@onezero: ~
0x04c000e9  0 onezero Google - Mozilla Firefox

wmctrl -lG

one@onezero:~$ wmctrl -lG
0x02000004  0 0    0    1360 768  onezero Desktop
0x02e00002  0 -1460 -868 1360 768      N/A DNDCollectionWindow
0x02e00003  0 0    24   58   744      N/A launcher
0x01e00004  0 290  653  780  115  onezero cairo-dock
0x02e00004  0 0    0    1360 24       N/A panel
0x04800061  0 408  95   732  500  onezero Transmission
0x02e0000a  0 -1402 -844 1302 744      N/A Dash
0x03a00044  0 0    24   1360 744  onezero Center a window via command line - Ask Ubuntu - Stack Exchange - Chromium
0x04400006  0 127  94   983  434  onezero one@onezero: ~
0x04c000e9  0 5    47   1349 715  onezero Google - Mozilla Firefox

You can specify a window by referencing its title or partial title after -r. -e is for moving and resizing

wmctrl -r "Mozilla Firefox" -e <G>,<X>,<Y>,<W>,<H>

<G>: Gravity specified as a number. The numbers are defined in the EWMH specification. The value of zero is particularly
     useful, it means "use the default gravity of the window".
<X>,<Y>: Coordinates of new position of the window.
<W>,<H>: New width and height of the window.

So, to move a window to the upper left corner and make it 1000 pixels wide by 700 tall, you’d use 0,0,0,1000,700

one@onezero:~$ wmctrl -r "Mozilla Firefox" -e 0,0,0,1000,700

enter image description here

To move/resize it . For that, I used the workaround of “unmaximizing” it first, using the -b option

wmctrl -r "Mozilla Firefox" -b add,maximized_vert,maximized_horz

wmctrl -r "Mozilla Firefox" -b remove,maximized_vert,maximized_horz

one@onezero:~$ wmctrl -r "Mozilla Firefox" -b add,maximized_vert,maximized_horz

enter image description here

The Things You Need To Understand 1st

The -e option expects a list of comma separated integers: "gravity,X,Y,width,height"

enter image description here

thats is my screen Resolution so x = 1360 & y = 786

Aligning a window to left-half of the screen

one@onezero:~$ wmctrl -r "Mozilla Firefox" -e 1,0,0,680,768

Aligning a window to right-half of the screen

one@onezero:~$ wmctrl -r "Mozilla Firefox" -e 1,680,0,680,768

Aligning a window to center of screen 1360 / 4 = 340

one@onezero:~$ wmctrl -r "Mozilla Firefox" -e 1,340,0,680,768

enter image description here

Manipulate it as of your screen settings

For More Help 1 2 3 4

Solution 2

that works with the currently active window

IFS='x' read screenWidth screenHeight < <(xdpyinfo | grep dimensions | grep -o '[0-9x]*' | head -n1)

width=$(xdotool getactivewindow getwindowgeometry --shell | head -4 | tail -1 | sed 's/[^0-9]*//')
height=$(xdotool getactivewindow getwindowgeometry --shell | head -5 | tail -1 | sed 's/[^0-9]*//')

newPosX=$((screenWidth/2-width/2))
newPosY=$((screenHeight/2-height/2))

xdotool getactivewindow windowmove "$newPosX" "$newPosY"

Solution 3

If anyone wants a code snippet to copy/paste to do this, here's one:

winname='foo'
IFS='x' read sw sh < <(xdpyinfo | grep dimensions | grep -o '[0-9x]*' | head -n1)
read wx wy ww wh < <(wmctrl -lG | grep $winname | sed 's/^[^ ]* *[^ ]* //;s/[^0-9 ].*//;')
wmctrl -r $winname -e 0,$(($sw/2-$ww/2)),$(($sh/2-$wh/2)),$ww,$wh

Replace foo with the name of the window you want to center in the first line, of course.


Explanation (breakdown in the form of an example console session):

Getting the screen dimensions

llama@llama:~$ xdpyinfo | grep dimensions
  dimensions:    1920x1080 pixels (508x285 millimeters)
llama@llama:~$ xdpyinfo | grep dimensions | grep -o '[0-9x]*'
1920x1080
x
508x285
llama@llama:~$ xdpyinfo | grep dimensions | grep -o '[0-9x]*' | head -n1
1920x1080
llama@llama:~$ IFS='x' read sw sh < <(xdpyinfo | grep dimensions | grep -o '[0-9x]*' | head -n1)
llama@llama:~$ echo $sw $sh
1920 1080

Getting the window's geometry information

llama@llama:~$ wmctrl -lG | grep foo
0x00a0000c  0 1113 510  722  475  llama foo
llama@llama:~$ wmctrl -lG | grep foo | sed 's/^[^ ]*//;'
  0 1113 510  722  475  llama foo
llama@llama:~$ wmctrl -lG | grep foo | sed 's/^[^ ]* *[^ ]*//;'
 1113 510  722  475  llama foo
llama@llama:~$ wmctrl -lG | grep foo | sed 's/^[^ ]* *[^ ]* //;s/[^0-9 ].*//;'
1143 505  722  475  
llama@llama:~$ read wx wy ww wh < <(wmctrl -lG | grep foo | sed 's/^[^ ]* *[^ ]* //;s/[^0-9 ].*//;')
llama@llama:~$ echo $wx $wy $ww $wh
1143 505 722 475

Moving the window

llama@llama:~$ echo 0,foo,bar,$ww,$wh
0,foo,bar,722,475
llama@llama:~$ echo 0,$(($sw/2)),bar,$ww,$wh                                    
0,960,bar,722,475
llama@llama:~$ echo 0,$(($sw/2-$ww/2)),bar,$ww,$wh
0,599,bar,722,475
llama@llama:~$ echo 0,$(($sw/2-$ww/2)),$(($sh/2-$wh/2)),$ww,$wh
0,599,303,722,475
Share:
21,123

Related videos on Youtube

EpsilonVector
Author by

EpsilonVector

Updated on September 18, 2022

Comments

  • EpsilonVector
    EpsilonVector over 1 year

    Is there a way to either place a window in the center of the screen after it is opened, or cause it to open in the center of the screen?

    This needs to be done using command line.

  • wingedsubmariner
    wingedsubmariner almost 8 years
    This needs bash or zsh to run, plain sh won't work.
  • Ronan Jouchet
    Ronan Jouchet over 6 years
    Works 👍, except under Unity for windows that are 1/2 or 1/4 screen tiled (e.g. drag a window to the right side, it will occupy the right half of your screen). These windows seems to be special, and I don't know which xdotool action to call to first make them "normal" windows before calling windowmove. Ideas?
  • Ronan Jouchet
    Ronan Jouchet over 6 years
    Found a solution to my problem on this askubuntu thread. Involves calling wmctrl too, better solutions welcome. TL;DR: call wmctrl -ir "$(xdotool getactivewindow)" -b remove,maximized_vert,maximized_horz before the final xdotool getactivewindow windowmove call.
  • lemontree
    lemontree about 4 years
    This apparently moves the window to the center of the visible screen space -- so if you're working on two monitors, it will position the window right in the middle split up between the two screens. Is there a way to center the window within one screen (e.g. the primary one) only?