I wrote a set-up script for my dual screen set-up some while ago, for when X went bonkers and I needed to restore sanity, but I just found a spare monitor and worked out I can use DP from a USB-C port I didn't know I had.

It suddenly occurred to me that xrandr might be able to create its own command-line for the current set-up, rather than me having to fiddle about writing it myself, but I can't find any options for that.

Is there a utility for this? Is it a stupid idea?

  • I recommend letting autorandr handle the xrandr or arandr details behind the scenes for you. Example here: askubuntu.com/a/1130337/8 It's much simpler, just be sure to disable/remove the prior solutions so they don't clash with each other. Nov 21, 2021 at 21:23

3 Answers 3


I've had the same problem before which led me to create a function in ~/.bashrc called xreset:

xreset () {

    xrandr --output HDMI-0  --mode 1920x1080 --pos 0x0       --rotate normal \
           --output eDP-1-1 --mode 1920x1080 --pos 3840x2160 --rotate normal \
           --output DP-1-1  --mode 3840x2160 --pos 1920x0    --rotate normal

} # xreset

After you get your monitors arranged by position and resolution, grab the current setup with:

$ xrandr | grep " connected"

HDMI-0 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 1107mm x 623mm
eDP-1-1 connected primary 1920x1080+3840+2160 (normal left inverted right x axis y axis) 382mm x 215mm
DP-1-1 connected 3840x2160+1920+0 (normal left inverted right x axis y axis) 1600mm x 900mm

All the information is there for you to create your own xreset function.

  • 1
    Thanks, but it was that very translation I was looking to automate, and it turns out it's available from arandr, as I mention below. Sep 12, 2019 at 13:03
  • Fair enough but for people who don't want to install extra packages this answer helps them I think. Sep 12, 2019 at 15:07

Tut. Sometimes asking the question gets you different search terms.

Arandr is a graphical editor that allows you to move things around, but most pertinently it will save/export xrandr command-lines as scripts:

xrandr \
   --output HDMI-2 --mode 3840x2160 --pos 0x48 --rotate normal \
   --output HDMI-1 --off \
   --output DP-1 --mode 3840x2160 --pos 5760x0 --rotate left \
   --output eDP-1 --primary --mode 1920x1080 --pos 3840x1528 --rotate normal \
   --output DP-2 --off

I don't need the 'off' outputs, but they're harmless.


I don't know such software but here's a quick script that does the logical counterpart of iptables -S, that is generate commands that result in current config:

# Emit current X configuration as xrandr commands (similar to iptables -S)
# (does not currently support custom modelines or axis or panning)
# Copyright 2003 Mikko Rantalainen <[email protected]>
# License: MIT

function create_flags()
    if [ "$status" = "disconnected" ]; then
        printf "%s" "--output '$port' --off"
    if [ "$status" != "connected" ]; then
        echo "Error: port=$port: unknown status=$status" 1>&2
        exit 1

    if [ "$1" = "primary" ]; then
    printf "%s\n" "$mode" | sed 's/+/ /; s/+/x/' | while read mode position
        printf "%s" "--output '$port' --mode '$mode' --pos '$position'"
    if [ "$PRIMARY" = "yes" ]; then
        printf "%s" " --primary"
    if [ "$rotation" = "left" ]; then
        printf " --rotation left"
    elif [ "$rotation" = "right" ]; then
        printf " --rotation right"
    elif [ "$rotation" = "inverted" ]; then
        printf " --rotation inverted"

printf "xrandr"
LC_ALL=C xrandr -q | grep connected | while read line
    test "$1" = "--debug" && printf "\nProcessing line [%s]\n" "$line" 1>&2
    printf " %s" "$(create_flags $line)"
printf "\n"

Save that as a text file, give it executable bit and run it to emit the current X screen configuration as xrandr command. Pass a command line argument --debug to show how each line is interpreted.

I wish xrandr -S emitted this by default. Parsing textual output is always error-prone if the output is not designed to be parsed in the future, too. This doesn't try to preserve the selected refresh rate because output syntax of the query is that problematic. Instead, the xrandr will default to highest supported refresh rate for each GPU and monitor combination.

Note that connector / output names may change so the emitted commands cannot be used if xrandr renames the outputs after reboot. See https://www.baeldung.com/linux/primary-monitor-x-wayland for details.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .