Skip to content

Instantly share code, notes, and snippets.

@chirag64

chirag64/xrandr.sh

Forked from debloper/xrandr.sh
Last active Oct 17, 2020
Embed
What would you like to do?
#!/bin/bash
#If no argument is specified, ask for it and exit
if [[ -z "$@" ]];
then
echo "An argument is needed to run this script";
exit
else
arg="$@"
#Basic check to make sure argument number is valid. If not, display error and exit
if [[ $(($(echo $arg | grep -o "\s" | wc --chars) / 2 )) -ne 2 ]];
then
echo "Invalid Parameters. You need to specify parameters in the format \"width height refreshRate\""
echo "For example setResolution \"1920 1080 60\""
exit
fi
#Save stuff in variables and then use xrandr with those variables
modename=$(echo $arg | sed 's/\s/_/g')
display=$(xrandr | grep -Po '.+(?=\sconnected)')
if [[ "$(xrandr|grep $modename)" = "" ]];
then
xrandr --newmode $modename $(gtf $(echo $arg) | grep -oP '(?<="\s\s).+') &&
xrandr --addmode $display $modename
fi
xrandr --output $display --mode $modename
#If no error occurred, display success message
if [[ $? -eq 0 ]];
then
echo "Display changed successfully to $arg"
fi
fi
<<COMMENT
#Manual steps with explanation ahead by @debloper
# First we need to get the modeline string for xrandr
# Luckily, the tool "gtf" will help you calculate it.
# All you have to do is to pass the resolution & the-
# refresh-rate as the command parameters:
gtf 1920 1080 60
# In this case, the horizontal resolution is 1920px the
# vertical resolution is 1080px & refresh-rate is 60Hz.
# IMPORTANT: BE SURE THE MONITOR SUPPORTS THE RESOLUTION
# Typically, it outputs a line starting with "Modeline"
# e.g. "1920x1080_60.00" 172.80 1920 2040 2248 2576 1080 1081 1084 1118 -HSync +Vsync
# Copy this entire string (except for the starting "Modeline")
# Now, use "xrandr" to make the system recognize a new
# display mode. Pass the copied string as the parameter
# to the --newmode option:
xrandr --newmode "1920x1080_60.00" 172.80 1920 2040 2248 2576 1080 1081 1084 1118 -HSync +Vsync
# Well, the string within the quotes is the nick/alias
# of the display mode - you can as well pass something
# as "MyAwesomeHDResolution". But, careful! :-|
# Then all you have to do is to add the new mode to the
# display you want to apply, like this:
xrandr --addmode VGA1 "1920x1080_60.00"
# VGA1 is the display name, it might differ for you.
# Run "xrandr" without any parameters to be sure.
# The last parameter is the mode-alias/name which
# you've set in the previous command (--newmode)
# It should add the new mode to the display & apply it.
# Usually unlikely, but if it doesn't apply automatically
# then force it with this command:
xrandr --output VGA1 --mode "1920x1080_60.00"
# That's it... Enjoy the new awesome high-res display!
COMMENT
@brandontict

This comment has been minimized.

Copy link

@brandontict brandontict commented Sep 8, 2018

Thanks for sharing

@Gordan35

This comment has been minimized.

Copy link

@Gordan35 Gordan35 commented Jun 12, 2019

How to run this script please?

@chirag64

This comment has been minimized.

Copy link
Owner Author

@chirag64 chirag64 commented Jun 12, 2019

How to run this script please?

@Gordan35 Save this in a text file in your home folder, then run chmod +x xrandr.sh to make it executable.

Then you can run it by doing ./xrandr.sh 1920 1080 60 to set a resolution of 1920 x 1080 or you can change the parameters as per your needs

@bpav001

This comment has been minimized.

Copy link

@bpav001 bpav001 commented Jun 24, 2019

@chirag64 Hey, I followed the steps in your above comment and I get the following error:

xrandr: unrecognized option '1920_1080_60'

My xrandr.sh is in the home folder, I ran chmod +x xrandr.sh, and then I run the command ./xrandr.sh 1920 1080 60. Would you help me out please? I'm not sure what I'm doing wrong.

@chirag64

This comment has been minimized.

Copy link
Owner Author

@chirag64 chirag64 commented Jun 24, 2019

@bpav001: Check out the manual steps that start from line number 35. Patiently read through them and try those commands one by one

@machinejade

This comment has been minimized.

Copy link

@machinejade machinejade commented Sep 13, 2019

How do you make this permanent/persistent please?

@Dev-Sec0901

This comment has been minimized.

Copy link

@Dev-Sec0901 Dev-Sec0901 commented Jun 27, 2020

Thank you so much @chirag64, it saved hours...

@hrrieck

This comment has been minimized.

Copy link

@hrrieck hrrieck commented Sep 16, 2020

My screen resolution was maximized to 1024x768 (was 1920x1080) when I upgraded to Ubuntu 19 series and now I get the following message: xrandr: screen cannot be larger than 1024x768 (desired size 1920x1080), is there anything I can do about this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.