baumi's blog

baumi's personal blog … Linux, OS X, Windows, Random things, …

Elementary OS: switch active desktop spaces from command line (bash, shell)

apt-get install wmctrl

list available desktop spaces:

wmctrl -d

switch:

wmctrl -s 0
wmctrl -s 1
.
.
.

get current desktop space id:

wmctrl -d | grep "*" | cut -d ' ' -f 1

get number of active desktop spaces:

wmctrl -d | tail -1 | cut -d ' ' -f 1

swipe left

wmctrl -s $((`wmctrl -d | grep "*" | cut -d ' ' -f 1` - 1))

swipe right

wmctrl -s $((`wmctrl -d | grep "*" | cut -d ' ' -f 1` + 1))

toggle between two spaces:
wmctrl -s `wmctrl -d | grep -v “*” | cut -d ‘ ‘ -f 1`
or put this into /home/youruser/toggle.sh:

#!/bin/bash
wmctrl -s `wmctrl -d | grep -v "*" | cut -d ' ' -f 1`

and put this as a “run custom command” on the desktop corners for quick switching between spaces….

cycle through multiple spaces (excluding blank space):

#!/bin/bash
lockfile=/tmp/cycle_desktop_space.lock
if [ -f $lockfile ]
then
	exit 1
fi
touch $lockfile
trap 'rm "$lockfile"' EXIT
num_spaces=`wmctrl -d | tail -1 | cut -d ' ' -f 1`
next_space=`wmctrl -d | grep "*" -A 1 | tail -1 | cut -d ' ' -f 1`
if [ -z next_space ] ; then
next_space=0
fi
wmctrl -s $(($next_space % $num_spaces))
sleep .3

cycle through multiple spaces (including blank space):

#!/bin/bash
lockfile=/tmp/cycle_desktop_space.lock
if [ -f $lockfile ]
then
        exit 1
fi
touch $lockfile
trap 'rm "$lockfile"' EXIT
cur_space=`wmctrl -d | grep "*" | cut -d ' ' -f 1`
num_spaces=`wmctrl -d | tail -1 | cut -d ' ' -f 1`
wmctrl -s $((($cur_space + 1) % ($num_spaces+1)))
sleep .3

cycle through 2 spaces (including blank space):

#!/bin/bash
lockfile=/tmp/cycle_desktop_space.lock
if [ -f $lockfile ]
then
        exit 1
fi
touch $lockfile
trap 'rm "$lockfile"' EXIT
cur_space=`wmctrl -d | grep "*" | cut -d ' ' -f 1`
num_spaces=`wmctrl -d | tail -1 | cut -d ' ' -f 1`
if [ $num_spaces -eq 2 ] && [ $cur_space -eq 1 ] ; then
        cur_space=-1
fi
wmctrl -s $((($cur_space + 1) % ($num_spaces+1)))
sleep .3

Comments are currently closed.