1

Every time I start my PC, my leftmost monitor is configured to be my rightmost one. It works fine after I change it in display settings.

However, whenever I restart my PC, they're back to the wrong configuration. Is there a file I can edit that will force them to a certain configuration?

Jacob Vlijm
  • 83,767

2 Answers2

1

Monitor configurations on second monitors, as set in Monitors, often are not remembered after restart.

With the script below, you can define which of your monitors should be the left one on startup. It will top alline both monitors in a left/right setup.

The used commands are xrandr commands to position the screens in the spanning area of the two screens (see the link in "Notes").

Script to set up left/right monitor on startup

Since the commands to run depend on the current situation and which of your screens you actually want to have on the right/left, you'll need a script to check your screens, their horizontal resolutions, create the correct commands and automatically set up the desired situation on startup.

The script

#!/usr/bin/env python3
import subprocess
import sys

left = sys.argv[1]

# get the data on screens and resolutions, parsed from xrandr
current = [l.split() for l in subprocess.check_output(["xrandr"]).decode("utf-8").splitlines()]
# find the name(s), x-res of the screen(s)
screens = [
    (l[l.index("connected")-1], [s.split("x")[0] for s in l if s.count("+") == 2][0]) \
    for l in current if "connected" in l
    ]
# set up screen positions from left to right (important)
if left == screens[0][0]:
    subprocess.call(["xrandr", "--output", screens[0][0], "--pos", "0x0"])
    subprocess.call(["xrandr", "--output", screens[1][0], "--pos", screens[0][1]+"x0"])
elif left == screens[1][0]:
    subprocess.call(["xrandr", "--output",  screens[1][0], "--pos", "0x0"])
    subprocess.call(["xrandr", "--output",  screens[0][0], "--pos", screens[1][1]+"x0"])

How to use it

  1. Copy the script into an empty file, save it as setup_monitors.py
  2. Open a terminal window, run the command xrandr
  3. In the output, two lines (if your second monitor is connected) will contain the names of your "connected" screens. The names look like: DVI-I-1 or VGA-0. Make an educated guess which is the one you want on the left.
  4. Run the script (from a terminal) by the command:

    python3 /path/to/setup_monitors.py <left_screen>
    

    e.g.:

    python3 /path/to/setup_monitors.py DVI-I-1
    

    Test the command on both screens. The screens will black out for a second and be configurated.

  5. If all works fine, add a command to your Startup Applications: choose Dash > Startup Applications > Add the command

    /bin/bash -c "sleep 15 && python3 /path/to/setup_monitors.py DVI-I-1"
    

Note

  • The sleep is specifically needed for xrandr commands when a second monitor is involved. If the script (actually the commands inside it) run too early, the desktop is not fully loaded yet and the commands will either break or miss their target.
  • See also here for more info on what the command(s) inside the script are doing.
Jacob Vlijm
  • 83,767
  • Is there a way to make this work for 3 screens? Using it right now changes nothing that I can see. – Backbounds Sep 25 '15 at 05:17
  • @Backbounds Absolutely, five if you like. I can edit in an addition to the answer. I will be busy today and tomorrow however, but will get back to you, and I will need your help to test, since I can post an answer that should work, but my humble system only offers to connect two screens :) – Jacob Vlijm Sep 25 '15 at 06:46
1

Jacob's solution works but there are some additional alternatives. You could put the xrandr commands in your .xsessionrc file, so that they're executed at startup. You could also just write an xorg.conf entry to set your monitors explicitly left/right of each other. I'm not at my PC so I'm regrettably short on details at the moment, but will come back to fill them in later if I get time. In the meantime it would be worthwhile for you to read up on both xrandr and xorg.conf files in general; you might discover a better solution than what I end up putting here anyway. ^_^

EDIT: OK I'm finally getting around to this.

Using xrandr and ~/.xsessionrc

The xrandr utility is used to both get and set information about your displays. Type xrandr by itself on the command line and you'll get a listing of all your displays and the "modes" that they support. Here is some example output corresponding to an old Dell 4:3 LCD.

DP2 connected 1280x1024+1920+0 (normal left inverted right x axis y axis) 338mm x 270mm
   1280x1024      60.0*+   75.0  
   1152x864       75.0  
   1024x768       75.1     60.0  
   800x600        75.0     60.3  
   640x480        75.0     60.0  
   720x400        70.1  

You would add modes to these if your monitor's resolution isn't right, etc., but your problem is about positioning. xrandr provides simple parameters --left-of <output> and --right-of <output> that allow you to dictate where your displays sit in relation to each other.

So let's say your displays identify as FOO1 and FOO2. (They never will; this is just an example.) If you want FOO1 to be always on the left of FOO2, you would enter:

xrandr --output FOO1 --left-of FOO2

or

xrandr --output FOO2 --right-of FOO1

Typing this on the command line will immediately make the change for your current session. Inserting the same line into your ~/.xsessionrc file will make the change effective every time you start X.

Using xorg.conf

Your other alternative is to write an xorg.conf entry that will dictate display configuration as part of the X startup process. If you're on Ubuntu then this will likely be part of a file in an xorg.conf.d directory, either at /usr/share/X11/xorg.conf.d or /etc/X11/xorg.conf.d. So if you don't already have one, create a config file to drive your display adapters and monitors. I called mine 01-monitors.conf to ensure it got executed first. Reusing the previous example with FOO1 being to the left of FOO2, we can set up the same preference here with a Monitor config section.

Section "Monitor"
    Identifier "FOO1"
    Option "LeftOf" "FOO2"
EndSection

Note that the Identifier is the same one you'd get from xrandr.

Further Reading

The man page for the xorg.conf configuration system is available at http://www.x.org/archive/X11R7.7/doc/man/man5/xorg.conf.5.xhtml