I spent the better part of a weekend trying to connect to my home computer through remote desktop tools. I tried Anydesk and then I tried Teamviewer and later I even tried Nomachine. Every single one of them fought me on the screen resolution. No matter what I selected the image looked stretched or squeezed or just strangely blurry. It felt like the computer and the remote software were speaking two completely different languages.
After a while I realized that the only reliable approach was to figure out the correct resolution myself. I tested one setting after another until I finally found a sweet spot that actually looked right on my monitor. It was far from elegant but at least it worked.
What annoyed me even more was the need to manually add new resolution profiles to xrandr. Doing it by hand over and over again felt like a tiny repetitive chore that slowly drained my patience. I wanted something cleaner and I wanted it to be automatic. So I wrote a small script that handled everything for me. Now I can test and apply new resolutions with a single command and I never have to poke around xrandr by hand again.
It is a small victory but a very satisfying one.
USAGE:
With refresh rate:
./script.sh 1536x864 60
Without refresh rate:
./script.sh 1536x864
If you are satisfied, press y enter, if you are not, or not seeing the screen, it will revert back to the previous resolution within 10 seconds.
Bash script:
#!/usr/bin/env bash
set -euo pipefail
# Usage: ./safe-resolution.sh WIDTHxHEIGHT [REFRESH]
# Example: ./safe-resolution.sh 1536x864 60
if [[ $# -lt 1 ]]; then
echo "Usage: $0 WIDTHxHEIGHT [REFRESH]"
exit 1
fi
RES="$1"
REFRESH="${2:-60}" # default to 60 Hz if not provided
if [[ ! "$RES" =~ ^([0-9]+)x([0-9]+)$ ]]; then
echo "Resolution must be in WIDTHxHEIGHT form, e.g. 1536x864"
exit 1
fi
WIDTH="${BASH_REMATCH[1]}"
HEIGHT="${BASH_REMATCH[2]}"
# Detect connected display (prefer primary)
OUTPUT=$(xrandr | awk '/ connected primary/{print $1; exit} / connected/{print $1; exit}')
if [[ -z "${OUTPUT:-}" ]]; then
echo "Could not detect a connected display output."
exit 1
fi
# Detect current active mode for that output (the one with the *)
CURRENT_MODE=$(xrandr | awk -v out="$OUTPUT" '
$1 == out { inout = 1; next }
inout && /\*/ {
print $1
exit
}
')
if [[ -z "${CURRENT_MODE:-}" ]]; then
echo "Warning: could not detect current mode, falling back to 1440x900"
CURRENT_MODE="1440x900"
fi
echo "Using output: $OUTPUT"
echo "Current mode: $CURRENT_MODE"
echo "Target resolution: ${WIDTH}x${HEIGHT} @ ${REFRESH}Hz"
# Generate modeline with cvt
MODEL_LINE=$(cvt "$WIDTH" "$HEIGHT" "$REFRESH" | awk '/^Modeline/ { $1=""; sub(/^ /,""); print }')
if [[ -z "${MODEL_LINE:-}" ]]; then
echo "Failed to generate modeline with cvt."
exit 1
fi
MODE_NAME=$(awk '{print $1}' <<< "$MODEL_LINE")
MODE_ARGS=$(awk '{$1=""; sub(/^ /,""); print}' <<< "$MODEL_LINE")
echo "Mode name: $MODE_NAME"
echo "Modeline args: $MODE_ARGS"
# Create the new mode (ignore error if it already exists)
if ! xrandr --newmode "$MODE_NAME" $MODE_ARGS 2>/dev/null; then
echo "Note: mode may already exist, continuing..."
fi
# Attach the mode to the output (ignore error if already attached)
if ! xrandr --addmode "$OUTPUT" "$MODE_NAME" 2>/dev/null; then
echo "Note: mode may already be attached to $OUTPUT, continuing..."
fi
echo
echo "Switching $OUTPUT to $MODE_NAME..."
echo
# Try to switch to the new mode
if ! xrandr --output "$OUTPUT" --mode "$MODE_NAME"; then
echo "Failed to set mode $MODE_NAME on $OUTPUT"
exit 1
fi
echo "Resolution changed to $MODE_NAME."
echo "Keep this resolution? Type 'y' and press Enter within 10 seconds to accept."
echo "If you don't respond or type anything else, it will revert to $CURRENT_MODE."
# Ask for confirmation with 10-second timeout
if read -r -t 10 answer; then
if [[ "$answer" == [Yy] ]]; then
echo "Resolution confirmed. Keeping $MODE_NAME."
exit 0
else
echo "Answer was not 'y'. Reverting to $CURRENT_MODE..."
fi
else
echo
echo "No response within 10 seconds. Reverting to $CURRENT_MODE..."
fi
# Revert to previous mode
if xrandr --output "$OUTPUT" --mode "$CURRENT_MODE"; then
echo "Reverted to $CURRENT_MODE on $OUTPUT."
else
echo "Warning: failed to revert to $CURRENT_MODE on $OUTPUT. Please fix via display settings."
fi