I’m using my Laptop at home about as much as I use it elsewhere. Since I got the port replicator, it has pretty much replaced my desktop PC. When the laptop’s docked, I use the big monitor on my desk alongside the internal LCD of the laptop. When it’s not docked, I mainly use the internal screen, and occasionally a projector or television.
Until now, when I wanted to switch between video output configurations, I typed some xrandr commands into the console, every time thinking, “I gotta make a hotkey for this.” There even is a nice label on one of the laptop’s F-keys for this. I first thought about making a script that just cycles through the different monitor configurations when the button is pushed. But that would have often made me have to press the button several times, probably each time waiting for the internal screen to come back on. I decided to make a little prompt so I can choose which configuration I want each time I push the button.
I implemented the prompt using i3-nagbar, the tool which is normally used to notify the user when there’s an error in i3’s config file. It is also the tool which greets you with the message “Please do not run this program.” when you dare to start it.
Here’s the script I made:
#!/bin/sh xrandr --output VGA-0 --off --output DVI-0 --off --output LVDS --auto i3-nagbar -m "EBRND'S SUPER-COOL I3WM SCREEN CONFIG UTILITY" -t warning \ -b "LVDS + DVI" "xrandr --output VGA-0 --off --output LVDS --auto --output DVI-0 --auto --right-of LVDS" \ -b "LVDS + VGA" "xrandr --output DVI-0 --off --output LVDS --auto --output VGA-0 --auto --right-of LVDS" \ -b "VGA ONLY" "xrandr --output LVDS --off --output DVI-0 --off --output VGA-0 --auto" sh ~/.fehbg
First I switch off all external screens, making sure the prompt is visible even though I may have disconnected a monitor before switching it off. Then the nagbar comes up, providing three buttons. The actions for the button presses are xrandr commands establishing the chosen screen config. There’s no “LVDS only” button: clicking the “X” button of the i3-nagbar does just that. After the monitor configuration changes, the wallpaper is often tiled or distorted, and the last line calls feh to fix that after the nagbar is closed.
I bound that script to the Fn-F7 keycombo (since my laptop’s F7 key has a monitor-and-laptop icon printed on it and the combo sends the keycode XF86Display) in the i3 config:
bindsym XF86Display exec ~/.i3/screens.sh
Of course I could have put the whole i3-nagbar command after the exec (and the fehbg command into each of the nagbar’s actions), but having it in it’s own file feels a bit cleaner to me.