2

I want to swap with a shortcut between 2 modes using dual monitor:

  • Both display on
  • Left display on
  • Optionally, if only right display is on, turn both display on

This question was asked many times. However all sources found online are not accurate: there is always a small bit missing, e.g. command to detect which monitors are on is in fact used to detect which are connected; or the script is missing, etc.

So I combine all the answers here below.

To make this Q&A, I used the following sources (I might have forgot some):

  1. To detect which monitors are on:

  2. To get the scripting:

Eliah Kagan
  • 117,780
hyamanieu
  • 248
  • 4
  • 16

2 Answers2

1

It was helpful for me to do a similar thing using arandr

sudo apt install arandr arandr

using the tool, you can configure a setup, then Layout > Save as... and name it to a run script like 'home_monitors_layout.sh' etc.

Then you can edit the script, or use the generated layout commands and plug it into the other answer provided here instead of manually configuring left/right/center/etc. layout

adowdy
  • 239
  • Plugging into the other answer - meaning setting up the shortcut in Settings > Keyboard (for me the "add shortcut" was at the bottom in 19.04). The default location created by arandr was /home/[username]/.screenlayout/[saved_setting].sh – gabore Jul 08 '19 at 19:55
1

Go to your command line and create a .sh file (this will end up in home, I personnaly put them in a subfolder):

gedit ~/swap_monitors.sh

Copy & paste the following batch script. Note the bash call at the beginning, not sh

#!/bin/bash


## script to toggle between monitor modes

currentmonitor=$(xrandr | awk '/\ connected/ && /[[:digit:]]x[[:digit:]].*+/{print $1}')
double=$'DisplayPort-0\nHDMI-A-0'
right=$'DisplayPort-0'
left=$'HDMI-A-0'

if [ "$currentmonitor" = "$double" ]; then
    xrandr --output $left --auto --primary --output $right --off
elif [ "$currentmonitor" = "$right" ]; then
    xrandr --output $left --auto --primary --output $right --auto --right-of $left
else
    xrandr --output $left --auto --primary --output $right --auto --right-of $left
fi

exit 0

change the varibles double, right and left to the name of your monitors (check them with xrandr -q). Change the xrandr commands also if needed, e.g. perhaps you prefer primary on the right screen.

Note the elif conditions leads to the same command than after else so it is useless here. But I left it in case you want another behavior (e.g. circle between double>right>left>double>...)

Go to System Settings>keyboard panel (nautilus) and add the shortcut:

  • name : Swap monitor mode

  • command : /home/[username]/swap_monitors.sh

I personnaly added the shortcut Super+F4.

hyamanieu
  • 248
  • 4
  • 16