Managing Multiple Monitors on Linux Mint with xrandr
Managing Multiple Monitors on Linux Mint with xrandr
When using a Linux laptop with an external docking station, managing multiple monitors effectively can be challenging. If you often connect and disconnect external monitors, you may want a quick way to toggle between using only your laptop display and activating external monitors.
In this post, I will show a custom script using xrandr
to streamline the process. The script has been tested in Linux Mint 22, which is running X11.
Step 1: Understanding xrandr
for Monitor Management
xrandr
is a command-line utility for configuring and managing monitors in Linux. You can list connected displays, adjust resolutions, and enable or disable monitors. Here are a few quick commands to get started:
-
List all connected monitors:
xrandr
-
Enable or disable specific monitors:
xrandr --output HDMI-1 --off # Turn off an external display (HDMI-1) xrandr --output eDP-1 --auto # Enable the internal laptop display (eDP-1)
Step 2: Setting Up the Script
To simplify switching between display modes, we can write a script that:
- Detects connected monitors
- Allows toggling between using only the internal display or activating external monitors
- Logs actions for easy troubleshooting
Below is our script, toggle_displays.sh
. You can copy it to your local machine and customize it as needed.
The toggle_displays.sh
Script
#!/bin/bash
# toggle_displays.sh - A script to toggle between internal-only and external display setups using xrandr
# Version: 1.0
# Author: Craig
# Published: 2024-10-02
# Description: This script detects internal and external displays, enables only the internal display, or enables external displays when requested.
# Usage: ./toggle_displays.sh {internal|external} [--log]
# Example: ./toggle_displays.sh internal --log
# Versioning
#
# 1.0 - Release
#
SCRIPT_VERSION="1.4"
# Default log file path
LOG_FILE="toggle_displays.log"
LOG_TO_FILE=false
MODE=""
# Parse command-line arguments
for arg in "$@"; do
case $arg in
--log)
LOG_TO_FILE=true
shift # Remove --log from processing
;;
internal|external)
MODE=$arg
shift # Remove mode from processing
;;
*)
echo "Invalid option: $arg"
echo "Usage: $0 {internal|external} [--log]"
exit 1
;;
esac
done
# Validate that MODE is set (either "internal" or "external")
if [ -z "$MODE" ]; then
echo "Missing mode. Usage: $0 {internal|external} [--log]"
exit 1
fi
# Log function to output to console and optionally to a log file
log() {
if $LOG_TO_FILE; then
echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
else
echo "$(date +'%Y-%m-%d %H:%M:%S') - $1"
fi
}
# Log the start of the script and version
log "Starting toggle_displays.sh - Version $SCRIPT_VERSION"
# Log the initial DISPLAY and XAUTHORITY exports
log "Initial DISPLAY value: $DISPLAY"
log "Initial XAUTHORITY value: $XAUTHORITY"
# Check if the script is being run over SSH and set DISPLAY and XAUTHORITY if necessary
if [ -n "$SSH_CONNECTION" ]; then
export DISPLAY=:0
export XAUTHORITY=/home/$USER/.Xauthority
log "Detected SSH session. DISPLAY set to $DISPLAY"
log "XAUTHORITY set to $XAUTHORITY"
fi
# Automatically detect the internal and external displays
INTERNAL_DISPLAY=$(xrandr | grep " connected primary" | awk '{ print $1 }')
EXTERNAL_DISPLAYS=$(xrandr | grep " connected" | grep -v "$INTERNAL_DISPLAY" | awk '{ print $1 }')
# Log detected displays
log "Detected internal display: $INTERNAL_DISPLAY"
log "Detected external displays: $EXTERNAL_DISPLAYS"
# Function to use only the internal display
use_internal_only() {
log "Switching to internal display only..."
for display in $EXTERNAL_DISPLAYS; do
log "Disabling external display: $display"
xrandr --output "$display" --off
done
xrandr --output "$INTERNAL_DISPLAY" --auto
log "Switched to internal display only."
}
# Function to enable external displays
enable_external_displays() {
log "Enabling external displays..."
xrandr --output "$INTERNAL_DISPLAY" --auto
for display in $EXTERNAL_DISPLAYS; do
log "Enabling external display: $display"
xrandr --output "$display" --auto --right-of "$INTERNAL_DISPLAY"
done
log "Enabled external displays."
}
# Execute the corresponding function based on MODE
if [ "$MODE" == "internal" ]; then
use_internal_only
elif [ "$MODE" == "external" ]; then
enable_external_displays
fi
# Log the end of the script execution
log "Completed toggle_displays.sh - Version $SCRIPT_VERSION"
How to Use the Script
-
Save the Script: Copy the script above into a file called
toggle_displays.sh
. -
Make It Executable: Run the following command to make the script executable:
chmod +x toggle_displays.sh
-
Run the Script:
- To use only the internal display:
./toggle_displays.sh internal
- To enable external displays:
./toggle_displays.sh external
- To use only the internal display:
-
Enable Logging: Add the
--log
option to enable logging to a file. For example:./toggle_displays.sh external --log
The script will create a log file (toggle_displays.log
) in the same directory if logging is enabled. This log file will capture each action the script takes, including which displays are enabled or disabled.
Conclusion
With this script, you now have a way to switch between using only your laptop display and enabling external monitors.