3

Suppose you have several windows open distributed across multiple monitors. Then you switch to using a single monitor (perhaps because you have removed your laptop from its docking station). All your windows are now on one screen.

Then you switch back to your multiple monitor configuration. You now have more screen space, but all your windows are initially piled up on a single monitor. It would be nice if your windows would go back to the monitor they were on originally.

Any ideas on how to make this happen?

1
  • The placement of windows is controlled by the window manager. What window manager or desktop environment are you using? Oct 15, 2013 at 18:59

2 Answers 2

2

xdotool

Rough idea but you could achieve what you want by creating a couple of commands using xdotool. Then you could run them accordingly when you have 1 or 2 monitors connected.

There's a pretty good example of how you could do this in this articled titled: Xubuntu – moving windows between monitors.

excerpt from section: Moving the active window to the other monitor (finally!)

Here’s what we need to do:

  • Find the active window
  • Get its maximized state and remember it
  • Remove maximization
  • Get its geometry
  • Calculate the new position
  • Move it
  • Maximize based on the previous state
  • Raise it

Here’s a script that does that:

wid=`xdotool getactivewindow`
max_state=`xprop -id $wid _NET_WM_STATE`

wmctrl -ir $wid -b remove,maximized_vert,maximized_horz
eval `xdotool getwindowgeometry --shell $wid`

new_x=1600
if [[ "$X" -ge "$new_x" ]]; then
  new_x=0
fi

xdotool windowmove $wid $new_x $Y
if [ -z "${max_state/*_NET_WM_STATE_MAXIMIZED_*/}" ]; then
  wmctrl -ir $wid -b add,maximized_vert,maximized_horz
fi

xdotool windowraise $wid

More interactive method

I also found another approach that also made use of xdotool but wrapped it in a shell script that you could then associate with a shortcut key. Using this method you could select a window so that it was raised and had focus and by hitting the shortcut key combination, would send the application to another window. The article is titled: Move Windows Between Monitors.

The method provides the following script, windowmove.sh:

#!/bin/bash
if [ $1 -eq 2 ]
then
POS1=`xrandr --current | head -2 | tail -1 | cut -d 'x' -f1 | cut -d ' ' -f3`
POS2=0
else
POS1=0
POS2=0
fi
/usr/bin/xdotool windowmove `/usr/bin/xdotool getwindowfocus` $POS1 $POS2
exit 0

POS1 calculates the width of your main screen by using the output of xrandr. If you find that the script can't move windows right, but it can move them left, then try replacing that line with POS1=1920, and replace 1920 with the width in pixels of your main monitor.

Then run the Keyboard Bindings applet:

$ gnome-keybinding-properties

NOTE: This is runable from different places on different distros via the GUI.

Create 2 keybindings using these 2 application launches:

  • binding #1's command: ./Scripts/windowmove.sh 1
  • binding #2's command: ./Scripts/windowmove.sh 2
1
  • Thanks. I should be able to hack together exactly what I want from this.
    – user31765
    Oct 24, 2013 at 14:22
1

No need for xdotool. You can record all window locations and then later restore them with just wmctrl:

Record: wmctrl -lG > ${rcfile}

Place:

while read row; do
  IFS=" " read id g x y w h _ <<< ${row}
  wmctrl -ir ${id} -e 0,${x},${y},${w},${h}
done < ${rcfile}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .