Skip to content

Instantly share code, notes, and snippets.

Last active May 25, 2023 20:01
What would you like to do?
Easily change between laptop and external displays in i3 + dmenu
# This script is intended to make switching between laptop and external displays easier when using i3+dmenu
# To run this script, map it to some shortcut in your i3 config, e.g:
# bindsym $mod+p exec --no-startup-id $config/
# IMPORTANT: run chmod +x on the script to make it executable
# The result is 4 options appearing in dmenu, from which you can choose
# This is your default laptop screen, detect by running `xrandr`
# choices will be displayed in dmenu
# Your choice in dmenu will determine what xrandr command to run
chosen=$(echo -e $choices | dmenu -i)
# This is used to determine which external display you have connected
# This may vary between OS. e.g VGA1 instead of VGA-1
if [ `xrandr | grep VGA-1 | grep -c ' connected '` -eq 1 ]; then
if [ `xrandr | grep DVI-1 | grep -c ' connected '` -eq 1 ]; then
if [ `xrandr | grep HDMI-1 | grep -c ' connected '` -eq 1 ]; then
if [ `xrandr | grep HDMI-2 | grep -c ' connected '` -eq 1 ]; then
if [ `xrandr | grep HDMI-3 | grep -c ' connected '` -eq 1 ]; then
if [ `xrandr | grep DP1 | grep -c ' connected '` -eq 1 ]; then
if [ `xrandr | grep DP-2 | grep -c ' connected '` -eq 1 ]; then
if [ `xrandr | grep DP-3 | grep -c ' connected '` -eq 1 ]; then
# xrander will run and turn on the display you want, if you have an option for 3 displays, this will need some modifications
case "$chosen" in
external) xrandr --output $INTERNAL_OUTPUT --off --output $EXTERNAL_OUTPUT --auto --primary ;;
laptop) xrandr --output $INTERNAL_OUTPUT --auto --primary --output $EXTERNAL_OUTPUT --off ;;
clone) xrandr --output $INTERNAL_OUTPUT --auto --output $EXTERNAL_OUTPUT --auto --same-as $INTERNAL_OUTPUT ;;
dual) xrandr --output $INTERNAL_OUTPUT --auto --output $EXTERNAL_OUTPUT --auto --right-of $INTERNAL_OUTPUT --primary ;;
Copy link

Thank you for this script. I found that you made a mistake on line 42. Just replace "DP-2" to "DP-3" to fix it.

Copy link

amanusk commented Jan 16, 2020

Thanks for the fix notice! Glad this helps.

Copy link

Thanks for the fix notice! Glad this helps.

also I found misspelling in 'wihch' word))

Copy link

amatgil commented Nov 28, 2020

Quick question, why did you write "chosen=$(echo -e $choices | dmenu -i)" instead of "chosen=$(echo $choices | dmenu -i) in line 16? The "-e" makes the laptop option not work. Just wondering.
Amazing script tho, thanks

Copy link

benjaminvdb commented Apr 9, 2021

My external screen is called DP-1, and my internal screen e-DP-1 is matched by grep DP-1. Therefore I needed to make the expression more specific: grep '^DP-1', so it matches only my external screen. I modified the other expressions for HDMI-1, VGA-1, etc. in the same way, just in case.

Thanks for the script! 🙏 It works really great.

Copy link

xhlar commented Jun 7, 2022

Thanks for the script!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment