Skip to content

Instantly share code, notes, and snippets.

@lmlsna
Created February 21, 2022 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmlsna/40231e9c46039013318887ee8eabd84b to your computer and use it in GitHub Desktop.
Save lmlsna/40231e9c46039013318887ee8eabd84b to your computer and use it in GitHub Desktop.
Setup weird resolution for ultrawide monitor on Ubuntu
#!/bin/bash
#
# Set a nonstandard resolution (ultrawide) interactively
#
###
# Define default resolution (and monitor ID if multiple monitors)
# Will be overridden is passed to script as arguments
#
#
width="${1-3440}" # Width in px, must be /8
height="${2-1440}" # Height in px, must be /8
fr="${3-50}" # Refresh rate, must be 50, 60, 75, 85
display="${4}" # Monitor name (if known, for multiple monitor setup)
###
# Use `cvt` to format the modeline for `xrandr`. Width/height shoudl be a multiple of 8.
# with a regex to remove the comment and leading word "Modeline"
#
# cvt <width> <height> [refresh=60]
#
modeline="$(cvt $width $height $fr | tail -n1 | sed 's/Modeline //g;s/"//g')"
###
# List all the active monitors and their current resolutions
# with a regex to reduce output to "<monitor> <resolution>".
#
# xrandr --current
#
monitors="$(xrandr --current|grep ' connected' | sed 's/\(connected\|primary\) //g;s/\([A-Z]\+-[0-9]\) \([0-9]\+x[0-9]\+\)\(.*\)$/\1 \2/g;')"
###
# If $display is defined and not found, abort.
#
# If $monitors returns more than one line (multiple monitor setup), then prompt for a choice.
# Otherwise just use the only active moitor $name
#
if [[ -n "$display" ]]; then
display="$(echo "$monitors" | grep -o "$display")"
if [[ -z "$display" ]]; then
echo "Specified monitor $display was not found. Quitting."
exit 2
fi
else
num_monitors=$(echo "$monitors" | wc -l)
echo "$num_monitors active monitors detected:"
if [[ $num_monitors -gt 1 ]]; then i=0
while [[ $i -lt $num_monitors ]]; do i=$((i+1))
echo -e "($i) $(echo "$monitors" | head -n $i | tail -n 1)"
done
echo -n "Select a monitor [1-$num_monitors]: "
read -n1 monitor_index
display="$(echo "$monitors" | head -n $monitor_index | tail -n 1 | cut -d ' ' -f 1)"
else
display="$(echo "$monitors" | cut -d ' ' -f 1)"
fi
fi
echo -ne "\nApply resolution of ${width}x${height}@${fr} to $display? [Y/n]: ";
read -n1 yn
echo ""
[[ "${yn,,}" == "n" ]] && echo "Quitting." && exit 1
##
# Define a new resolution mode
#
# xrandr --newmode <name> <clock MHz> <hdisp> <hsync-start> <hsync-end> <htotal> [flags...]
# [+HSync] [-HSync] [+VSync] [-VSync] [+CSync] [-CSync] [CSync] [Interlace] [DoubleScan]
#
name="$(echo "$modeline" | cut -d ' ' -f1)"
xrandr --newmode $modeline
xrandr --addmode $display $name
xrandr --output $display
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment