How to "smart" position and resize linux terminal windows?

I would like to brainstorm how to create a script that would work on multiple desktop UI's and with multiple Linux terminals (if possible) that would "smartly" position terminal window. Issue with statically setting size and position is obvious - different screen resolutions.

User case 1: Ann likes to work with multiple teminal window and she would like to have one use top half of the screen and other to use bottom half of the screen but width of each should be 80% of screen resolution.

User case 2: Bill uses only one terminal but he likes to have it on in the bottom right corner, and to have 40% width and 30% hight of full screen resolution.


Solution 1:

You should be able to do something with wmctrl:

wmctrl is a UNIX/Linux command line tool to interact with an EWMH/NetWM compatible X Window Manager.

The tool provides command line access to almost all the features defined in the EWMH 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.

Just write scripts for Ann and Bill that look something like this:

#!/bin/bash
xterm &
sleep 2  ## sleep just to let the terminas appear and become the active window
wmctrl -r :ACTIVE: -e 5,-1,-1,660,540
        -----------   -- -- -- --- ---
             |         | |  |   |   |---> Window height
             |         | |  |   |-------> Window width             
             |         | |  |-----------> Window Y coordinates
             |         | |--------------> Window X coordinates
             |         |----------------> Gravity
             |--------------------------> Apply to the active window

Gravity can be one of (source):

  • NorthWest (1)
  • North (2),
  • NorthEast (3),
  • West (4),
  • Center (5),
  • East (6),
  • SouthWest (7),
  • South (8),
  • SouthEast (9)
  • Static (10).

A gravity of 0 indicates that the Window Manager should use the gravity specified in WM_SIZE_HINTS.win_gravity.

You should be able to figure out a way of specifying the terminal window specifically if you look through man wmctrl. Otherwise, use my sleep && active hack.


Update in response to your comment:

I can get the active window to move to the bottom right hand corner of my screen with this:

wmctrl -r :ACTIVE: -e 4,3040,900,620,620

I'm not really sure what the gravity is doing, but specifying X and Y works. I am running a system with an extended desktop spread over two screens:

$ xrandr  | grep -w connected
VGA-0 connected 1440x900+1600+0 (normal left inverted right x axis y axis) 408mm x 255mm
DP-3 connected 1600x900+0+0 (normal left inverted right x axis y axis) 344mm x 194mm

So, 1600+1440 = 3040 which means that 3040 will place my window at the bottom right. You will need to tweak according to your setup of course.