Setting active monitors using xrandr
Posted: August 15th, 2009 | By Amitay Dobo | Filed under: Technology | Tags: Linux, Ubuntu, x server, xrandr | No Comments »This post is (or might be) relevant for people who are running X Server with xrandr (1.2 and up) enabled (meaning, linux users mostly).
I have 2 displays connected to computer: The primary computer monitor and a television I sometime use to watch movies. Ubuntu doesn’t recognize when I turn on and off a monitor, but provides a way of enabling and disabling a monitor. This can be accessed in Gnome through System/Preferences/Display, and there’s even a shortcut for it in the system tray (is that how it’s called on Gnome?)
However, it can be quite a nuisance to click/drag/click/click just for turning on a monitor, and more so when gnome doesn’t remember my exact display setup (i.e. which monitor is left of which monitor).
Xrandr to the rescue!
Xrandr is the underlying protocol and utility that X Server uses to dynamically manage the screens, displays and outputs (monitors). It has many features which might be interesting for people who want to build, for example, an interesting video art exhibit with 64 displays. However, I just want it to enable/disable my monitors. All the information was clearly available by issuing a “man xrandr”.
The commands I was looking for were all along something like:
xrandr --output DVI-0 --off --output DVI-1 --mode 1360x768
DVI-0 and DVI-1 are my two monitors. each –output followed by the output name indicates which output the following commands refer to. As you see, you can send commands to multiple outputs at the same time. So in this example, DVI-0 is turned off, and DVI-1 is turned on, on the preconfigured mode of 1360*768 resolution. –auto command is suppose to select the optimal mode and run on the monitor, but it’s kinda quirky in my setup, so i explicitly set the mode.
Another example, to turn both montiors, DVI-1 right of DVI-0:
xrandr --output DVI-0 --mode 1280x1024 --output DVI-1 --mode 1360x768 --right-of DVI-0
Clearer than English, isn’t it?
But still, typing all this isn’t really going to save me any time, so I made a small shell script file to automate it, and moved the output names and modes to constants so they could be easily changed. Usage is simple: execute it with -l, -r, or -lr for the 3 different modes.
setscreen.sh
#!/bin/bash SCR_LEFT=DVI-0 SCR_RIGHT=DVI-1 SCR_LEFT_MODE=1280x1024 SCR_RIGHT_MODE=1360x768 arg=$1 if [ $arg == '-lr' ] then xrandr --output $SCR_LEFT --mode $SCR_LEFT_MODE --output $SCR_RIGHT --mode $SCR_RIGHT_MODE --right-of $SCR_LEFT elif [ $arg == '-l' ] then xrandr --output $SCR_RIGHT --off --output $SCR_LEFT --auto elif [ $arg == '-r' ] then xrandr --output $SCR_LEFT --off --output $SCR_RIGHT --mode $SCR_RIGHT_MODE else echo "Valid arguments are: -lr, -l, -r (left+right screens, left screen only, right screen only." exit fi exit
Now I can execute it from the terminal, make a shortcut launcher for it, or just forget about it and keep clicking buttons and menus in good old windows style…








Leave a Reply