Skip to content

Instantly share code, notes, and snippets.

@VladSavitsky
Created March 17, 2024 19:16
Show Gist options
  • Save VladSavitsky/39c1dd7d1a796727125b742429c785c5 to your computer and use it in GitHub Desktop.
Save VladSavitsky/39c1dd7d1a796727125b742429c785c5 to your computer and use it in GitHub Desktop.
Dual Monitors configuration from command line
#!/usr/bin/env bash
# Summary: Enables and disables monitors.
# Usage: mon <command:|e|i|l|r|m>
# Help:
# Params:
# <command>
# - e|external - Enable only external. Default.
# - i|internal - Enable only internal (built-in).
# - l|left - Enable both but left is primary.
# - r|right - Enable both but right is primary.
# - m|irror - Enable both and show the same on both.
#
# Installation:
# 1. Add script 'mon' to the PATH.
# 2. Update section 'Settings'.
#
# WARNING: Update names of internal and external monitors:
# To get names of monitors run: xrandr;
configure_monitors() {
local command=${1:-e};
# Settings.
# Position of the external monitor.
local position="right";
# Machine name of the monitors.
local internal="eDP-1";
local external="DP-1";
# Should terminal window be closes when configuration completed.
local do_close_terminal="no";
# Do the magic.
xrandr --auto;
if [[ $command =~ ^(external|ext|e)$ ]]; then :;
xrandr --output $internal --off --output $external --auto;
elif [[ $command =~ ^(internal|int|i)$ ]]; then :;
xrandr --output $internal --auto --output $external --off;
elif [[ $command =~ ^(right|r)$ ]]; then :;
xrandr --output $external --primary "--${position}-of" $internal;
elif [[ $command =~ ^(left|l)$ ]]; then :;
#Built-in is primary on left:
xrandr --output $external "--${position}-of" $internal;
xrandr --output $internal --primary;
elif [[ $command =~ ^(mirror|m)$ ]]; then :;
xrandr --auto;
fi;
# Close terminal window.
if [[ $do_close_terminal == "yes" ]]; then :;
# Delay required to browser have time to start process.
sleep 1;
# Get process id ($$) and kill parent process to close terminal.
PPPID=$(awk '{print $4}' "/proc/$$/stat");
kill -9 $PPPID;
fi;
}
configure_monitors "$@";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment