How to Using Xrandr for Multi Monitor - Windows Basics


Wednesday, December 6, 2023

How to Using Xrandr for Multi Monitor

While dual-monitor setups are something we take for granted when using Linux, things aren't always as simple as plug and play. You might be using a desktop environment that doesn't want to work with your hardware, or have a window manager that insists on setting one of your monitors to a lower resolution than the one it supports. For problems like these, XRandR will prove to be an extremely valuable tool.

Find out the resolutions supported by your monitor

Start by querying XRandR to see which resolutions your display supports:

xrandr -q

In the screenshot, we see a monitor named HDMI-A-1 connected with a resolution of 1920×1080. “Screen 0” is the space used for the entire “screen real estate” you have in your current configuration. In this case, it is twice the width of the HDMI-A-1 display (3840×1080) because there is another display with the same resolution placed to the right of the one we are viewing. By scrolling further down the output we can see the second monitor called DP-1.

An asterisk (*) next to the resolution represents the selected mode. In the X protocol, resolutions are always labeled as “modes”. The plus sign (+) next to the resolution is what XRandR believes is the optimal mode for that particular display.

You'll notice when you type xrandr -q into your terminal that your monitors will always have the cable type prefix they use to connect to your computer (e.g., HDMI, DP, VGA, DVI). This ensures that the display name is not too complicated.

Set up XRandR properly

For simplicity, the remainder of this guide will assume you are using two DisplayPort (DP) displays with optimal resolution at 1080p (1920×1080).

Let's expand your two screens to immediately learn how XRandR works:

xrandr --auto --output DP-0 --mode 1920x1080 --left-of DP-1

xrandr --auto --output DP-1 --mode 1920x1080

Below is a summary of what these commands mean:

  • The --auto flag is used to enable a disabled device that is already connected or to disable a device that is no longer detected. This helps fix some issues where the screen may not display anything.
  • The first command tells XRandR to set the display connected to the main DisplayPort interface on your graphics hardware to set the resolution to 1920×1080 and to point the display to the left of the other display. If this is not what you want to do, change --left-of to --right-of. You can also use --left-of in the second command instead to achieve the same thing.
  • The second command simply sets the second monitor's resolution to 1920×1080.

Want to expand your screen vertically? Just switch --left-of or --right-of to --above or --below.

Want to project on both screens? Use --same-as instead. For example, if you wanted DP-1 to mirror DP-0, this is what you would enter:

xrandr --auto --output DP-1 --same-as DP-0

This is especially useful when you want to use the projector on a laptop or something similar.

If you want to set a specific refresh rate, use the --rate flag:

xrandr --auto --output DP-0 --mode 1920x1080 --rate 60

Remember, XRandR is not magic. It can't set a refresh rate that it doesn't know your display supports. For example, you can't turn your 60Hz monitor into a killer 144Hz gaming monitor with a simple command unless that's the refresh rate it supports. The query executed at the beginning will tell you in one neat column the refresh rates available for a given resolution with your particular hardware.

Also, keep in mind that the desktop environment or window manager you are currently using may not allow you to set the refresh rate higher than the maximum speed of the slowest display.

To turn off the screen, just use --off:

xrandr --output DP-1 --off

Note how the example doesn't include --auto here. It's not really necessary as it only serves as a backup to turn on displays that might otherwise be turned off but are connected. This is not what we want to do right now.

To turn the display back on, set the display's mode using the --auto flag:

xrandr --auto --output DP-1 --mode 1920x1080

Maintain XRandR settings across reboots

Now you have your display set up exactly the way you want it, but don't rush to reboot yet. Your computer will lose these settings as soon as it restarts. To prevent this, we will have to write a script.

Create a file named in your home directory. For simplicity's sake, we'll go back to the first example where we set up two 1080p DisplayPort displays, with the DP-0 being the display on the left. In that case, this is what we fill in the file:


xrandr --auto --output DP-0 --mode 1920x1080 --left-of DP-1

xrandr --auto --output DP-1 --mode 1920x1080

If you think of another command, replace what the example wrote with that command and save the file. Just make sure that the top line (#!/bin/bash) is preserved.

That helps ensure that Linux uses the correct shell to execute your script.

Don't forget to reopen your terminal and make this script executable:

chmod a+x ~/

All you have to do now is set this script as the startup program in the desktop environment of your choice.

Is there a GUI option for this?

Every desktop environment has a user interface for XRandR. Just take a look at your settings. If you know where to set the resolution on your monitor, you've found the GUI.

But if you're using a window manager or a really strange environment, you might not have the XRandR UI pre-installed. In these cases, you might want to try ARandR, which is a simple, straightforward interface and even saves pre-made scripts for you.

To use it, open the application and navigate to the Outputs menu. From there, simply set the resolution and direction you want for each output.

Save your changes and enjoy!

No comments:

Post a Comment