Skip to content

Instantly share code, notes, and snippets.

@alexwilson
Created October 27, 2025 06:11
Show Gist options
  • Select an option

  • Save alexwilson/56c4b01d0e68323024116d8b6ef34f5a to your computer and use it in GitHub Desktop.

Select an option

Save alexwilson/56c4b01d0e68323024116d8b6ef34f5a to your computer and use it in GitHub Desktop.
DisplayPlacer script for quickly connecting to 1 external monitor
#!/bin/bash
# Always assume 2 displays: built-in (ID 1) and external (whatever isn't 1)
builtin_id="1"
all_ids=($(displayplacer list | grep "Contextual screen id:" | sed 's/.*Contextual screen id: //'))
external_id=""
for id in "${all_ids[@]}"; do
if [ "$id" != "1" ]; then
external_id="$id"
break
fi
done
# Get display information
displays_info=$(displayplacer list)
# Get built-in display width from current resolution
builtin_width=$(echo "$displays_info" | grep -A5 "Contextual screen id: 1" | grep "Resolution:" | sed 's/.*Resolution: \([0-9]*\)x.*/\1/')
# Get external display's best resolution (highest width without scaling)
external_section=$(echo "$displays_info" | sed -n "/Contextual screen id: $external_id/,/^Persistent screen id:/p")
external_width=$(echo "$external_section" | grep "res:" | grep -v "scaling:on" | sort -t: -k2 -nr | head -1 | sed 's/.*res:\([0-9]*\)x\([0-9]*\).*/\1/')
external_height=$(echo "$external_section" | grep "res:" | grep -v "scaling:on" | sort -t: -k2 -nr | head -1 | sed 's/.*res:\([0-9]*\)x\([0-9]*\).*/\2/')
# Calculate centering offset
width_diff=$((external_width - builtin_width))
x_offset=$((width_diff / 2))
y_offset=-$external_height
echo "Built-in: ${builtin_width}px wide (ID: $builtin_id)"
echo "External: ${external_width}x${external_height} (ID: $external_id)"
echo "Calculated offset: (-${x_offset}, ${y_offset})"
# Apply display configuration
displayplacer "id:$builtin_id mode:66" "id:$external_id origin:(-${x_offset},${y_offset}) resolution:${external_width}x${external_height} scaling:on"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment