Changing UI scaling from command line

I am using Ubuntu 18.10.

I am trying to create two aliases in my .bashrc for commands to "resdown" and "resup" (i.e. decrease and increase my screen resolution respectively).

The resdown command is as follows, and works just fine:

xrandr --output DP-2 --mode 3840x2160

The issue I have is with resup. When I run the following:

xrandr --output DP-2 --mode 3840x2160

The UI scaling is set back to 100%. I need to change the scaling to 200%, or everything on my 4K display is incredibly small.

I have been experimenting with the "--scale" option for xrandr, but it entirely screws up my display. As such, I am not sure that it is doing what I think it is doing:

xrandr --output DP-2 --mode 3840x2160 --scale 2x2

If I used my second monitor to go back into gnome-control-center, I can set the screen resolution and scaling just fine, and everything goes back to normal. I want, however, to do this from the command line.

Basically - I want to know how to set the "Scale" setting that you see in gnome-control-center (see below) from the command line. How can I do this?:

enter image description here


Tl;Dr:
sed -i -e '/<scale>/ s/1/2/' ~/.config/monitors.xml && sudo systemctl restart gdm

Mutter does listen to some settings in Gsettings, and after looking at the code, there is some legacy gsettings support for deprecated settings like gsettings ui-scaling-factor. For other things, it supports using XML for configuration. You'll probably need to Alt+F2, r to restart gnome-shell for settings to apply... or figure out the dbus details from the code.

Here is my config for an old monitor... notice the line... easy to sed

~/.config/monitors.xml
<monitors version="2">
  <configuration>
    <logicalmonitor>
      <x>0</x>
      <y>0</y>
      <scale>1</scale>
      <primary>yes</primary>
      <transform>
        <rotation>upside_down</rotation>
        <flipped>no</flipped>
      </transform>
      <monitor>
        <monitorspec>
          <connector>HDMI-1</connector>
          <vendor>ACR</vendor>
          <product>V226HQL</product>
          <serial>T0WAA0072442</serial>
        </monitorspec>
        <mode>
          <width>1920</width>
          <height>1080</height>
          <rate>60</rate>
        </mode>
      </monitor>
    </logicalmonitor>
  </configuration>

I put together the codes above to do 200x. It works on wayland and X, but will log you out. If you can do ALt+F2,r, that would obviously be better... I tried doing it with a terminal command and just kept crashing gnome. Might use Robot.js or xdotool to send a restart command... like this

xdotool keydown alt && sleep 1 && xdotool key F2 keyup alt && sleep 1 && xdotool key r && sleep 1 && xdotool key KP_Enter

My full version for X11 swap-res.sh... this works on an ubuntu keyboard shortcut too

#!/bin/bash
File=~/.config/monitors.xml
if grep -q "<scale>1" "$File"; then
  sed -i -e '/<scale>1/ s/1/3/' "$File"
else
  sed -i -e '/<scale>3/ s/3/1/' "$File"
fi

# Unix Utils
xdotool keydown alt && sleep 1 && xdotool key F2 keyup alt \
  && sleep 1 && xdotool key r key KP_Enter

Source: https://gitlab.gnome.org/GNOME/mutter/ (quite literally, leave a star!)

SEO / Similar Questions:
Scaling Gnome login screen on HiDPI display
How do I use Mutter with GNOME Shell?
Changing UI scaling from command line