Switch monitors from the command line

22

10

Since I found a different method to achieve my goal, and since no answer was posted to my previous question, I've changed the question to match the answer I found.

Is there a way to turn off my laptop's monitor and turn on the external monitor(and vice-versa) entirely from the command line?

Malabarba

Posted 2010-06-21T14:05:19.640

Reputation: 7 588

I was just googling for the exact same thing and I stumbled on this question which you asked only 7 minutes ago. That's friggin amazing. I do hope you get an answer. – JD Long – 2010-06-21T14:13:27.833

I know that you can drop a basic xorg.conf into the root (/root/yorg.conf.new) directory, if you boot into recovery mode and execute X --configure (or similar). – Bobby – 2010-06-21T14:24:05.460

the problem (at least for me) is that I don't want to write out an xorg.conf by hand. I want to dump my current settings into xorg.conf format then change settings with the GUI and then dump those into a file. Then I can write a script to change between the two settings really easy. Right now I have to go into the GUI and click half a dozen times to change my settings. – JD Long – 2010-06-21T14:42:28.160

@JD Long: That is exactly what I'm trying to do as well. Make sure to write here if you ever find a way. The only two things missing for my script are the xorg.conf files, and a command to refresh the current displays (without having to do gdm restart). – Malabarba – 2010-06-21T15:18:34.220

I've been experimenting with disper today (http://willem.engen.nl/projects/disper/). The help file shows a -p option which is supposed to export. Only that switch is not implemented. sad trombone

– JD Long – 2010-06-21T16:44:43.567

Answers

28

With the commands

xrandr --output VGA-0 --auto
xrandr --output LVDS --off 

The screen automatically transfers to the external display. It doesn't even need sudo powers. To find out the name of the displays just do:

xrandr -q

Which should give something like:

VGA-0 connected 1280x1024+0+0 (normal left inverted right x axis y axis) 338mm x 270mm
...
LVDS connected (normal left inverted right x axis y axis)
...

Extending the displays can probably be achieved in a similar manner.

Malabarba

Posted 2010-06-21T14:05:19.640

Reputation: 7 588

4If xrandr -q gives LVDS1 & VGA1:

External only: xrandr --output VGA1 --auto --output LVDS1 --off.

Extended left, internal primary: xrandr --output VGA1 --auto --left-of LVDS1 --output LVDS1 --auto --primary.

Extended left, external primary: xrandr --output VGA1 --auto --left-of LVDS1 --primary --output LVDS1 --auto.

Internal only: xrandr --output VGA1 --off --output LVDS1 --auto. – None – 2017-01-23T10:23:02.230

nice answer! Maybe worth explaining for beginners which one usually will be the internal and external between VGA and LVDS? – Matifou – 2019-10-05T18:39:02.990

I get the message "Can't open display" when running xrandr (with any flags). What can be the reason? – brandizzi – 2013-04-30T12:17:01.883

4

This is most certainly NOT a direct answer to your question. But I found it helpful in my use case. This is not an export of the config file, but it does show how to automate disper in a shell script. I'm setting this up to run every time I dock/un-dock and it seems to be fixing my display issues when docking and undocking my laptop:

You have to have disper and Python installed.

#!/bin/sh
#
# Detect displays and move panels to the primary display
#

PYTHON=python2.6
DISPER=/usr/bin/disper

# disper command will detect and configure monitors
$PYTHON $DISPER --displays=auto -e -t left

# parse output from disper tool how many displays we have attached
# disper prints 2 lines per displer
lines=`$PYTHON $DISPER -l|wc -l`

display_count=$((lines / 2))

echo $display_count

echo "Detected display count:" $display_count

# Make sure that we move panels to the correct display based
# on the display count
if [ $display_count = 1 ] ; then
    echo "Moving panels to the internal LCD display"
    gconftool-2 \
    --set "/apps/panel/toplevels/bottom_panel_screen0/monitor" \
    --type integer "0"
    gconftool-2 \
    --set "/apps/panel/toplevels/top_panel_screen0/monitor" \
    --type integer "0"
    sleep 5
    pkill gnome-panel
else
    echo "Moving panels to the external display"
    gconftool-2 \
    --set "/apps/panel/toplevels/top_panel_screen0/monitor" \
    --type integer "1"
    gconftool-2 \
    --set "/apps/panel/toplevels/bottom_panel_screen0/monitor" \
    --type integer "1"
    sleep 5
    pkill gnome-panel
fi

JD Long

Posted 2010-06-21T14:05:19.640

Reputation: 139

Disper seems like a useful tool. I'll look into it and see if I can make it fit my case. The only difference is that I want to disable the laptop's monitor, instead of extending it. It's a little smoother on my low-end laptop, and it deals with the panels automatically. – Malabarba – 2010-06-21T18:53:20.287

On second thought, my ati video card might not be supported, as they only claim to support nvidia. – Malabarba – 2010-06-21T18:54:40.427

Ok, the diper page led me to this page: http://www.thinkwiki.org/wiki/Sample_Fn-F7_script

Turns out it's pretty easy to switch monitors with the xrandr command.

– Malabarba – 2010-06-21T19:17:37.057

thanks for posting the xrandr link. I'll investigate that. I'm bumbling around with the same stuff. FWIW, my laptop has an Intel card and I'm using disper with no problems. – JD Long – 2010-06-21T19:32:12.583