How to restore display of window after unplugging and replugging 2nd monitor

kdekwinmonitors

I have an HDMI monitor which I unplug and plug back into my laptop from time to time. When I unplug the monitor, all the windows are moved onto the laptop display. However, when I plug it back in, all the windows are moved onto the 2nd display (which is on the left of the laptop display), even though I haven't moved them myself.

How can I make KDE preserve which display each window is on across unplug-and-replug?

Best Answer

You can save the current position of windows with:

wmctrl -lG > ~/.windowlistrc

You can restore them with:

rcfile=~/.windowlistrc

while read row; do
  IFS=" " read id g x y w h _ <<< ${row}
  if [[ ${g} == "-1" ]]; then continue; fi
  wmctrl -ir ${id} -b toggle,maximized_vert,maximized_horz
  wmctrl -ir ${id} -b remove,maximized_vert,maximized_horz
  y=$(( ${y} - 30 ))
  wmctrl -ir ${id} -e 0,${x},${y},${w},${h}
done < ${rcfile}

The restore could be much simpler, but I found I needed to toggle things on and off for windows that were arranged by the window manager after dragging near a side or top, and then expanded (by the wm) to fill half the monitor.

Related Question