Skip to content

Instantly share code, notes, and snippets.

@chirag64
Forked from debloper/xrandr.sh
Last active February 10, 2024 02:30
Show Gist options
  • Star 51 You must be signed in to star a gist
  • Fork 26 You must be signed in to fork a gist
  • Save chirag64/7853413 to your computer and use it in GitHub Desktop.
Save chirag64/7853413 to your computer and use it in GitHub Desktop.
Added license on user request
#!/bin/bash
# Copyright © 2021 Chirag Bhatia
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#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
Copy link

Thanks for sharing

@Gordan35
Copy link

How to run this script please?

@chirag64
Copy link
Author

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

@benye001
Copy link

@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
Copy link
Author

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

@machinejade
Copy link

How do you make this permanent/persistent please?

@Dev-Sec0901
Copy link

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

@hrrieck
Copy link

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?

@Liebig861
Copy link

Liebig861 commented Jan 1, 2021

@chirag64 My screen resolution is 1024x768 but i need to add 1440x900 can you help me.........to get that, I'm new to Linux can you guide me step by step

@exesse
Copy link

exesse commented May 8, 2021

Hi Chirag,

very well made solution. Could you please add some(MIT, Apache 2.0) license to the file?

@chirag64
Copy link
Author

chirag64 commented May 8, 2021

Hi Chirag,

very well made solution. Could you please add some(MIT, Apache 2.0) license to the file?

Hi @debloper, since this is a fork of your file, I'll need you to add a license to your original script first (and yes, I do realize the irony of forking first and asking for license later, my apologies 😅)

@exesse
Copy link

exesse commented May 8, 2021

@chirag64, thank you!

@debloper
Copy link

Heh... I never thought this little script will have more than 1 user, but apparently it has 14 forks that are so much better than the original one! 😂

Here, I've added the license. Even though X11 may be on its way(land) out, hopefully this would still remain useful for legacy & DIY systems (where this was intended and useful in the first place). So, cheers!

@chirag64
Copy link
Author

@debloper

Heh... I never thought this little script will have more than 1 user, but apparently it has 14 forks that are so much better than the original one! 😂

People are likely discovering it because it was used in a highly voted StackExchange answer to a question about this problem (I think we may have discussed this before) - https://unix.stackexchange.com/a/227894/11912

Here, I've added the license

Thanks 👍🏼

@chirag64
Copy link
Author

@exesse

Could you please add some(MIT, Apache 2.0) license to the file?

Done 👍🏼

@exesse
Copy link

exesse commented May 10, 2021

Cool, thank you guys! I use it to solve the issue between macbook retina screen resolution and my headless linux host during chrome remote sessions. Otherwise I would have to do all those steps manually in every reboot. 🎉🎉🎉

@antiphase
Copy link

Lines 12-19; if the context were slightly different (i.e. you hadn't slurped all the positional parameters into a single string at that point), it's just a really elaborate way of writing
if [[ $# -ne 3 ]];
to test that there are 3 arguments provided

@nervocalm
Copy link

Thanks for sharing @chirag64!
worked like a charm.

@Fabxx
Copy link

Fabxx commented Nov 25, 2021

./*.sh 1600 900 60
./1600x900.sh: 1: If: not found
./1600x900.sh: 2: [[: not found
./1600x900.sh: 9: [[: not found
./1600x900.sh: 19: [[: not found
xrandr: cannot find mode 1600_900_60
./1600x900.sh: 27: [[: not found

@vijay1700
Copy link

Hi, my OS is EndLess and my screen resolution is 2560x1080, which is not listed in the Displays resolution drop-down menu.
Even the commands mentioned in the following link doesn't seem to work!

https://medium.com/@AbhiXpert/add-change-the-custom-resolution-of-your-display-using-xrandr-on-ubuntu-18-04-in-a-minute-338caec6e29

Nor your script is running!

@debloper
Copy link

Newer distros are gradually moving away from X11 and towards Wayland... so the tooling & commands for those systems won't be the same. This script is for older systems running on X11 display server.

You can discover if it's using X11 or Wayland with

echo "$XDG_SESSION_TYPE"

If it's using X11, but you're unable to run the script, then it'd be great if you could post the terminal output, in order to debug what's going on (because "doesn't work" is not a useful debug info).

For Wayland related issues, please let the upstream developers or the distro developers know of the issue & how to recreate it. Hopefully they'd be able to fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment