Enabling laptop external display

I want my laptop (running Arch Linux, of course) to run on its own display and use an external display as a second screen if one is connected.

Telling X to use a second screen turns out to be very easy:

$ xrandr --output VGA-0 --auto --right-of LVDS --output LVDS --auto

where VGA-0 is the external display and LVDS is the internal display. These identifiers can be discovered like this:

$ xrandr -q | grep '\sconnected' | awk '{print $1}'

LVDS describes the interface of a typical laptop display.

The example sets up the external display (VGA-0) to be to the right of the internal one (LVDS). It can be executed to reconfigure the screens after connecting or disconnecting the external one.

To automate screen configuration during startup, the above command can be entered into the startup scripts for X, such as ~/.xinitrc or as a snippet in /etc/X11/xinitrc.d:

# #!/bin/bash
# /etc/X11/xinit/xinitrc.d/15-xrandr
[[ -x $(which xrandr 2> /dev/null) ]] && xrandr --output VGA-0 --auto --right-of LVDS --output LVDS --auto

The other time when it's useful is whenever the external display is connected or disconnected. Unfortunately, there isn't a consisrtent way to detect this but a hot-key can be configured to conveniently rerun xrandr. The xbindkeys utility can bind a key on the keyboard to a command. My laptop has an LCD icon on the F5 key

$ sudo  pacman -S xbindkeys
$ echo -e "\"/etc/X11/xinit/xinitrc.d/15-xrandr\"\n    Mod4 + F5" >  ~/.xbindkeysrc

The bound key, described as Mod4 + F5, is the F5 function key pressed with the Super key (commonly called the Windows Key). To see the modifiers for a key run:

$ xbindkeys -k
Press combination of keys or/and click under the window.
You can use one of the two lines after "NoCommand"
in $HOME/.xbindkeysrc to bind a key.
"(Scheme function)"
    m:0x40 + c:71
    Mod4 + F5

My laptop, a Sony VGN-BX197XP, has a Fn modifier that is what ought be used with the LCD icon on the F5 key. However, my Linux install doesn't recognise it (i.e xev, acpi_listen or showkey --scancodes don't respond to it) and I have insufficient time to investigate further.

To activate xbindkeys, place it in the startup scripts. For example. by adding the below to `/etc/X11/xinit/xinitrc.d/10-xresources:

[[ -x $(which xbindkeys 2> /dev/null) && -f ~/.xbindkeysrc ]] && xbindkeys

xrandr can do lots of clever things such as changing the display resolution or orientation on the fly. Try

$ xrandr -o inverted

which will invert the display (turn it upside down - revert this with -o normal). Or try

$ xrandr --size 800x600