Skip to content

Instantly share code, notes, and snippets.

@tuxdna
Created November 17, 2011 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuxdna/1373278 to your computer and use it in GitHub Desktop.
Save tuxdna/1373278 to your computer and use it in GitHub Desktop.
Script to enable multiple monitors using Xrandr
#!/bin/sh
# Author: siddhesh.in
# Here’s a script I’ve written to do this with any external display (monitor, projector, etc.).
# Execute it after connecting the display to your box:
typeset -a resx
typeset -a resy
typeset -a screen
typeset -i name=1
typeset -i count=0
tmpfile=$(mktemp)
cmd="xrandr --fb "
xrandr | grep -A 1 " connected " | grep -v "^--$" | awk '{print $1}' > $tmpfile
count=0
for line in $(cat $tmpfile); do
if [ $name -eq 1 ]; then
screen[$count]=$line
name=0
else
line=$(echo $line|sed 's/[Xx]/ /g')
resx[$count]=$(echo $line | awk '{print $1}')
resy[$count]=$(echo $line | awk '{print $2}')
count=$((count+1))
name=1
fi
done
total_width=0
prev_scr=
max_height=0
for i in $(seq 0 $((count-1))); do
if [ $max_height -lt ${resy[$i]} ]; then
max_height=${resy[$i]}
fi
cmd_ext="$cmd_ext --output ${screen[$i]} --mode ${resx[$i]}x${resy[$i]}"
if [ -n "$prev_scr" ]; then
cmd_ext="$cmd_ext --right-of $prev_scr"
fi
prev_scr=${screen[$i]}
total_width=$((total_width+${resx[$i]}))
done
cmd="xrandr --fb ${total_width}x${max_height} $cmd_ext"
echo "Running command: $cmd"
eval $cmd
rm -f tmpfile
@currix
Copy link

currix commented Dec 21, 2013

Nice script. There is a typo. > in line 15 should be >.

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