Update for desgua's answer - seems wmctrl -d
outputs differently for Ubuntu 18.04.
This provides shortcuts for first, last and new workspaces too, or as a function you can call __ws_switch N
where N
is the workspace number.
0 * DG: 1920x1080 VP: 0,0 WA: 0,28 1920x1052 Workspace 1
1 - DG: 1920x1080 VP: N/A WA: 0,28 1920x1052 Workspace 2
2 - DG: 1920x1080 VP: N/A WA: 0,28 1920x1052 Workspace 3
3 - DG: 1920x1080 VP: N/A WA: 0,28 1920x1052 Workspace 4
4 - DG: 1920x1080 VP: N/A WA: 0,28 1920x1052 Workspace 5
5 - DG: 1920x1080 VP: N/A WA: 0,28 1920x1052 Workspace 6
6 - DG: 1920x1080 VP: N/A WA: 0,28 1920x1052 Workspace 7
7 - DG: 1920x1080 VP: N/A WA: 0,28 1920x1052
Note the final blank workspace that isn't currently used. Assuming by the last workspace we mean the last workspace that is in use, the following could work:
cat << EOF >> "$script_dir"/ws.sh
#!/bin/sh
__ws_count() {
__ws_counter="$(wmctrl -d | wc -l)"
echo "$(( __ws_counter - 1 ))"
}
__ws_switch() {
# Sanity test if "$1" is an int
case "$1" in ''|*[^0-9]*)
echo "Input '$1' is not an integer"
return 1 ;;
esac
if test "$1" -gt "$(__ws_count)"; then
echo "Workspace #$1 doesn't exist"
return 1
fi
wmctrl -s "$1"
}
__ws_first() {
__ws_switch 0
}
__ws_last() {
__ws_counter="$(__ws_count)"
__ws_switch "$(( __ws_counter - 1 ))"
}
__ws_new() {
__ws_counter="$(__ws_count)"
__ws_switch "$(( __ws_counter ))"
}
EOF
To add as a shortcut using xbindkeys
, create the following shortcut files:
cat << EOF >> "$script_dir"/ws_first.sh
#!/bin/sh
. ./ws.sh
__ws_first
EOF
cat << EOF >> "$script_dir"/ws_last.sh
#!/bin/sh
. ./ws.sh
__ws_last
EOF
cat << EOF >> "$script_dir"/ws_new.sh
#!/bin/sh
. ./ws.sh
__ws_new
EOF
Then chmod +x ./ws.sh ./ws_first.sh ./ws_last.sh ./ws_new.sh
to make them executable. Finally, to add to .xbindkeysrc
:
cat << EOF >> ~/.xbindkeysrc
"$script_dir/ws_first.sh"
Alt + Alt_L + w + f
"$script_dir/ws_last.sh"
Alt + Alt_L + w + l
"$script_dir/ws_new.sh"
Alt + Alt_L + w + n
EOF
EDIT: Don't forget to restart xbindkeys
for changes to take effect:
killall xbindkeys && xbindkeys