17

I use a laptop and like to migrate during the day from one (physical) workplace to another and connect to an external monitor if available.

I also must multitask a lot, so I have a lot of windows open at any given time and many (virtual) workspaces to organize them on.

But each time I connect / disconnect the external monitor all my careful arrangement is gone. This makes me really mad.

Is there a desktop environment that just leaves the windows alone when you connect / disconnect a second monitor? Or even better, that remembers the window arrangement per monitor arrangement? So that when I went from my home office (17" monitor on top of the laptop screen) to my usual workplace (21" monitor left of the laptop screen) or my colleague's desk (19" monitor right of the latop screen), the windows end up where they were the last time that monitor was connected - or stay put if they never saw this monitor.

The way unity "handles" external monitors just makes me sad and wish I didn't have all those extra monitors to connect and once I connect it makes me chained to this arrangement, as if I had a desktop machine nailed to the floor.

6
  • Does this happen with any window manager? GNOME 3, KDE, LXDE, XFCE?
    – Braiam
    Commented Jan 31, 2014 at 21:53
  • I don't know. It happens with unity.
    – artm
    Commented Jan 31, 2014 at 22:12
  • Have you found a good solution to this? I've been forced to use a Dell laptop which I've put ubuntu on, but I am used to my MacBook Pro. On the Mac there is a program called Stay, which is indispensable, because it remembers window locations for each monitor configuration. Plug in a monitor, all windows move to where they were. Plug in a second monitor, they all move again. Unplug, they all go back to where they were. Furthermore the Mac is smart enough even without Stay to make sure all the windows are visible. On this Ubuntu laptop I keep having to find the windows that are off the screen.
    – Greywire
    Commented Nov 17, 2015 at 18:41
  • I cant find anything like Stay on linux. It looks like you can get all the information and control from individual commands, and the scripts below come close. I am working on improving these to get the functionality I need...
    – Greywire
    Commented Nov 17, 2015 at 18:41
  • Stay sounds like what I'm missing. No, I haven't found a solution.
    – artm
    Commented Nov 18, 2015 at 9:46

2 Answers 2

14
+100

I am providing you two shell scripts. It will help you to save your arrangement of windows positions and size. If somehow your desired windows arrangements get disturbed, you will be able to restore those arrangements with exact windows size and positions for all windows using these script.

You need to install wmctrl unless you already have it. Install via terminal,

sudo apt-get install wmctrl

Script to save windows configuration

# Script_Name: save_window_conf.sh
#!/bin/bash
if [ -f $HOME/.my_windows_config.txt ]; then
    echo -e "Information: Previous configuration file \"$HOME/.my_windows_config.txt\" already exists.\nTo save a new configuration remove it manually using the following command,\n\n\trm $HOME/.my_windows_config.txt"
    exit 1
else
    wmctrl -p -G -l | awk '($2 != -1)&&($3 != 0)&&($NF != "Desktop")' | awk '{print $1}' | while read mywinid
    do
        xwininfo -id "$mywinid" >> $HOME/.my_windows_config.txt
    done
fi

At execution the above script will save your windows position and size for all your open windows to a file named .my_windows_config.txt in your home directory. It is a hidden text file.

Script to reload the windows configuration

# Script_Name: load_window_conf.sh
#!/bin/bash

file=$HOME/.my_windows_config.txt
declare -a mywinid
declare -a x
declare -a y
declare -a width
declare -a height

nl=$(cat "$file" | grep xwininfo |wc -l)

for i in $(seq 1 $nl)
do
    mywinid[i]=$(cat "$file" | grep "xwininfo" | awk -v p="$i" '{if(NR==p) print $4}')
    x[i]=$(cat "$file" | grep "Absolute upper-left X" | awk -v p="$i" '{if(NR==p) print $NF}')
    y[i]=$(cat "$file" | grep "Absolute upper-left Y" | awk -v p="$i" '{if(NR==p) print $NF}')
    width[i]=$(cat "$file" | grep "Width" | awk -v p="$i" '{if(NR==p) print $NF}')
    height[i]=$(cat "$file" | grep "Height" | awk -v p="$i" '{if(NR==p) print $NF}')
done

for it in $(seq 1 $nl)
do
    wmctrl -i -r "${mywinid[$it]}" -e 0,"${x[$it]}","${y[$it]}","${width[it]}","${height[it]}"
done

When you execute the second script it will restore your windows position with exact size for all your windows.

Usage

Save these scripts in your $HOME/bin/ directory. Add $HOME/bin/ in your PATH. For this add these lines at the end of your $HOME/.bashrc

PATH=$HOME/bin:$PATH
export PATH

It will enable you execute those scripts with their name only. Give the scripts execution permission,

chmod +x $HOME/bin/save_window_conf.sh
chmod +x $HOME/bin/load_window_conf.sh

To save the configuration in your $HOME/.my_windows_config.txt

After you open and adjusted all your windows run in terminal,

save_window_conf.sh

To reload the configuration from your $HOME/.my_windows_config.txt

load_window_conf.sh

Hope it will solve your problem. Here is a screen shot,

enter image description here

7
  • 1
    Wow, thanks, this looks promising. I'll give the scripts a try.
    – artm
    Commented Jan 31, 2014 at 21:43
  • @artm I have edited a little glitch and a typo, now it seems ok. I tested it on Unity and Gnome-fallback-session, should work on gnome3. Let me know if you face any issue.
    – sourav c.
    Commented Jan 31, 2014 at 22:24
  • @artm it should be your decision when you wish to remove .my_windows_config.txt. If you want to load same windows configuration several time for existing windows you may wish to preserve it. But if you start a whole new session those windows id will be different and in that case it is better to remove old configuration file at $HOME/.my_windows_config.txt. You need to do remove the configuration saved file manually before saving a new configuration.
    – sourav c.
    Commented Feb 1, 2014 at 2:04
  • On XFCE4 in Ubuntu 18.04 I had to change gravity (first "0" in the -e parameter) to 31, otherwise the windows were restored about 31px right and down of where they were. I think that may be the size of the default XFCE panel. Thanks for the code!
    – akom
    Commented Sep 17, 2018 at 12:29
  • 1
    Nice. I added a 7z a $HOME/.my_windows_config.7z -si "$fn-$(date +"%Y-%m-%d--%H-%M-%S" -r "$fn")" <"$fn" for existing files instead of warning. (Where fn is the config file.). Perhaps someone else finds it useful. Commented May 4, 2020 at 23:03
-1

Common guide is here: Resolution

Your solutions about dynamical resolution is near to Xorg & XrandR or Xinerama

You have:
Four methods to setup

  1. By session with .xprofile.

  2. Dynamically by using xrandr tool

  3. Statically by setting in xorg.conf.

  4. Xrandr Graphical Front End GUI.

Good answer is Settings for multiple monitors are not stored

To workaround your Window Management across your workspaces you can use profiling system given by Compiz Config Manager

8
  • 2
    I don't have a problem with resolutions or display arrangements - those work as expected.
    – artm
    Commented Jan 30, 2014 at 22:50
  • So ANY desktop environment is FRIENDLY for you. Don't blame public, please.
    – swift
    Commented Feb 2, 2014 at 9:34
  • I don't blame anyone. I just want open windows to stay where I put them when connecting / disconnecting monitors.
    – artm
    Commented Feb 2, 2014 at 10:08
  • So you have four solutions with official reference. My answer adequately cover your "madness" ;)
    – swift
    Commented Feb 2, 2014 at 10:45
  • 2
    Oh, just try it for yourself: open a window on each workspace then connect a monitor that appears to the top of the original screen and see where you windows end up. Then tell me how xrandr is relevant to stopping windows from jumping randomly when a monitor is connected.
    – artm
    Commented Feb 2, 2014 at 21:14

You must log in to answer this question.

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