#!/bin/bash # toggle display arrangements. # If there is only one monitor, then run xrandr --auto # Otherwise assume there are two displays. A small one (e.g. laptop) # and a big one (e.g. monitor). Identify the displays, and toggle # between 3 states: small one only -> both with the big one to the # left of the small one -> big one only small one: <20 inch big one: # >20 inch # Make sure the variables from pipe can be assigned # https://stackoverflow.com/questions/42963395/bash-assign-variable-from-pipe shopt -s lastpipe i=0 regex="^([^ ]+).* .*$" # Read the output of xrandr for connected display names and sizes xrandr | grep " connected " | while IFS=$'\n' read -r line; do if [[ $line =~ $regex ]]; then name["$i"]="${BASH_REMATCH[1]}" (( i++ )) fi done # Only one monitor: run xrandr --auto if (( i == 1 )); then xrandr --auto exit 0 fi regex="^.*/([0-9]+)x.*/([0-9]+).* ([^ ]+)$" xrandr --listactivemonitors | while IFS=$'\n' read -r line; do if [[ $line =~ $regex ]]; then if (( "${BASH_REMATCH[1]}" > 500 )); then bigger="${BASH_REMATCH[3]}" if [[ $bigger == "${name[0]}" ]]; then smaller=${name[1]} else smaller=${name[0]} fi else smaller="${BASH_REMATCH[3]}" if [[ $smaller == "${name[0]}" ]]; then bigger=${name[1]} else bigger=${name[0]} fi fi fi done # If only smaller on, then have both on if ! xrandr --listactivemonitors | grep -q "$bigger"; then xrandr --output "$bigger" --primary --auto --output "$smaller" --right-of "$bigger" --auto # if only bigger on, then only smaller on elif ! xrandr --listactivemonitors | grep -q "$smaller"; then xrandr --output "$bigger" --auto --left-of "$smaller" --output "$smaller" --primary --auto xrandr --output "$bigger" --off # if both are on, then only bigger on else xrandr --output "$bigger" --primary --auto --left-of "$smaller" --output "$smaller" --auto xrandr --output "$smaller" --off fi