I want to change which of my two screens is primary via CLI, but it doesn't work

I want to use the CLI to switch the “primary” property between my monitors. I have the built-in screen of my laptop and an external screen connected by HDMI.

I found a lot of advice for using xrandr (on Ubuntu forum mainly, though I’m on Manjaro). However, it doesn’t work for me.

Here’s (what I think is) the relevant part of the output of:

> xrandr

Screen 0: minimum 320 x 200, current 1920 x 2160, maximum 16384 x 16384
eDP-1 connected 1920x1080+0+1080 (normal left inverted right x axis y axis) 344mm x 193mm
   1920x1080    144.00*+  60.02    59.97    59.96    59.93  
   1680x1050     59.95    59.88  
   % [...] %
   320x175       85.27  
HDMI-1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 527mm x 296mm
   1920x1080     60.00*+  74.97    50.00    59.94  
   1600x900      60.00  
   % [...] %
   720x400       70.08  
DP-1-0 disconnected (normal left inverted right x axis y axis)
DP-1-1 disconnected (normal left inverted right x axis y axis)

When I do either:

xrandr --output eDP-1 --primary
xrandr --output HDMI-1 --primary

… I get no output, my terminal signals the command has run successfully, but nothing changes (the old primary remains primary).

And when I run:

xrandr --output $(xrandr --listactivemonitors | awk '{print $4}') --primary

… I get the error message:

xrandr: unrecognized option 'eDP-1' % (or 'HDMI-1')
Try 'xrandr --help' for more information.

So what’s going on…?

Thanks!

… just trying to give direction - I don’t know how to do what you want to do …

You’ll need to figure this (above) out first.


look at what the term in the brackets returns -

xrandr --listactivemonitors

the output of which you then feed to awk

for me, it returns two lines (I currently have only one monitor connected) -
the 4th field you are trying to catch is on the second line of two in total …

it’s likely four lines in total for two monitors … and so on
what you are after is on each second line - awk looks at the first line first (I guess)
so: the result will start with an empty line which probably needs to be taken care of

Try

xrandr --output "eDP-1" --primary

That’s the first thing I tried, something looked fishy to me with the status of the name of the display in the command. I get the same result though…

After double-checking and a little trial and error, I think the actual command needed to automatically switch between the two displays is:

 xrandr --output "$(xrandr --listactivemonitors | grep "1: " | awk '{print $4}')" --primary

However, now that it’s fixed it gives the same result as the other two commands (which makes sense, I guess): command succesful, no text output in the terminal, and no effect on which screen is primary.

Both on X11 and Wayland you can set primary monitor via Plasma settings GUI, I don’t know why you are complicating things via cli.

As an advice, multiple bugs related to multi monitors support were only fixed for Wayland, it might be good to use it, and if you insist on using cli then you can do it via kscreen-doctor command.

Well, CLI is not, in my opinion, the complicated option. I switch primary monitor at least 15 times a day (because I don’t use the same primary for video games and working), so doing it via GUI requires at least 5 clicks on various small buttons on the screen(s). A command takes about 1.5s to launch via KRunner, so to me that’s the simple and efficient option.

Yup, amazing, it works perfectly. For anyone else trying to do this, the command I’m using from now on is:

kscreen-doctor output.$(xrandr --listactivemonitors | grep "1: " | awk '{print $4}').primary

(of course I’ve put it in a script in my $PATH with a short and easy to type name).

Thank you everyone for your contribution!

This topic was automatically closed 36 hours after the last reply. New replies are no longer allowed.