Ubuntu – dynamic-multiple-monitor friendly desktop environment available for Ubuntu

desktop-environmentsmultiple-monitorsunity

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.

Best Answer

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

Related Question