-
-
Save amanusk/6b79d407945ca79caa945ce2658fd987 to your computer and use it in GitHub Desktop.
| #!/bin/bash | |
| # 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/display.sh | |
| # 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` | |
| INTERNAL_OUTPUT="LVDS-1" | |
| # choices will be displayed in dmenu | |
| choices="laptop\ndual\nexternal\nclone" | |
| # 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 | |
| EXTERNAL_OUTPUT="VGA-1" | |
| fi | |
| if [ `xrandr | grep DVI-1 | grep -c ' connected '` -eq 1 ]; then | |
| EXTERNAL_OUTPUT="DVI-1" | |
| fi | |
| if [ `xrandr | grep HDMI-1 | grep -c ' connected '` -eq 1 ]; then | |
| EXTERNAL_OUTPUT="HDMI-1" | |
| fi | |
| if [ `xrandr | grep HDMI-2 | grep -c ' connected '` -eq 1 ]; then | |
| EXTERNAL_OUTPUT="HDMI-2" | |
| fi | |
| if [ `xrandr | grep HDMI-3 | grep -c ' connected '` -eq 1 ]; then | |
| EXTERNAL_OUTPUT="HDMI-3" | |
| fi | |
| if [ `xrandr | grep DP1 | grep -c ' connected '` -eq 1 ]; then | |
| EXTERNAL_OUTPUT="DP-1" | |
| fi | |
| if [ `xrandr | grep DP-2 | grep -c ' connected '` -eq 1 ]; then | |
| EXTERNAL_OUTPUT="DP-2" | |
| fi | |
| if [ `xrandr | grep DP-3 | grep -c ' connected '` -eq 1 ]; then | |
| EXTERNAL_OUTPUT="DP-3" | |
| fi | |
| # 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 ;; | |
| esac |
Thanks for the fix notice! Glad this helps.
Thanks for the fix notice! Glad this helps.
also I found misspelling in 'wihch' word))
Quick question, why did you write "chosen=$(echo -e
Amazing script tho, thanks
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.
Thanks for the script!
Thank you ๐ Amazing script
Hey, thanks for the script man, i've been looking for it for a while now. I used it, and it works very well, but I didn't like how it looks, cause it uses dmenu, so I created another script that uses i3-nagbar which looks good to me, better than dmenu.
#!/bin/bash
INTERNAL_OUTPUT="eDP-1"
# Get all connected outputs except the internal one
mapfile -t EXTERNAL_OUTPUTS < <(xrandr | grep " connected" | awk '{print $1}' | grep -v "^$INTERNAL_OUTPUT$")
# If no external displays found
if [ ${#EXTERNAL_OUTPUTS[@]} -eq 0 ]; then
i3-nagbar -t error -m "No external display detected"
exit 1
fi
# Join all external outputs into a single string
EXT="${EXTERNAL_OUTPUTS[@]}"
# Build commands dynamically
LAPTOP_CMD="xrandr --output $INTERNAL_OUTPUT --auto --primary"
for out in "${EXTERNAL_OUTPUTS[@]}"; do
LAPTOP_CMD+=" --output $out --off"
done
EXTERNAL_CMD="xrandr --output $INTERNAL_OUTPUT --off"
for out in "${EXTERNAL_OUTPUTS[@]}"; do
EXTERNAL_CMD+=" --output $out --auto --primary"
done
CLONE_CMD="xrandr --output $INTERNAL_OUTPUT --auto"
for out in "${EXTERNAL_OUTPUTS[@]}"; do
CLONE_CMD+=" --output $out --auto --same-as $INTERNAL_OUTPUT"
done
DUAL_CMD="xrandr --output $INTERNAL_OUTPUT --auto"
RIGHT_OF="$INTERNAL_OUTPUT"
for out in "${EXTERNAL_OUTPUTS[@]}"; do
DUAL_CMD+=" --output $out --auto --right-of $RIGHT_OF"
RIGHT_OF="$out"
done
DUAL_CMD+=" --primary"
# Launch i3-nagbar
i3-nagbar -t warning -m "Select display mode:" \
-b "Laptop" "$LAPTOP_CMD" \
-b "External" "$EXTERNAL_CMD" \
-b "Clone" "$CLONE_CMD" \
-b "Extend" "$DUAL_CMD"
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.