Good to use with Gnome 40+ in combination with keyboard shortcuts.
sudo apt install xdotool wmctrl
make bash script executable
chmod u+x win_to_workspace_left.sh
chmod u+x win_to_workspace_right.sh
./win_to_workspace_left.sh
#!/bin/bash
# xdotool needed
# generate desktop number for the leftward monitor
DESKTOPLEFTWARD="$(($(xdotool getactivewindow get_desktop_for_window)-1))"
DESKTOPEND="$(($(xdotool get_num_desktops)-1))"
# change desktop to the left
if [[ $DESKTOPLEFTWARD -eq -1 ]]
then
xdotool getactivewindow set_desktop_for_window $DESKTOPEND && xdotool set_desktop $DESKTOPEND
else
xdotool getactivewindow set_desktop_for_window $DESKTOPLEFTWARD && xdotool set_desktop $DESKTOPLEFTWARD
fi
./win_to_workspace_right.sh
#!/bin/bash
# xdotool and wmctl needed
# generate desktop number for the rightward monitor
DESKTOPRIGHTWARD="$(($(xdotool getactivewindow get_desktop_for_window)+1))"
DESKTOPEND="$(($(xdotool get_num_desktops)-1))"
DESKTOPSTART=0
# count open windows to know the end of the workspaces
LASTWORKSPACE="$(($(xdotool get_num_desktops)-2))"
CURRENTWORKSPACE=$(wmctrl -d | grep '*' | cut -d ' ' -f1)
OPENWINDOWS=$(wmctrl -l | cut -d ' ' -f3 | grep $CURRENTWORKSPACE | wc -l)
# change desktop to the right
if [[ $OPENWINDOWS -eq 1 ]] && [[ $CURRENTWORKSPACE -eq $LASTWORKSPACE ]]
then
xdotool getactivewindow set_desktop_for_window $DESKTOPSTART && xdotool set_desktop $DESKTOPSTART
else
xdotool getactivewindow set_desktop_for_window $DESKTOPRIGHTWARD && xdotool set_desktop $DESKTOPRIGHTWARD
fi
xdotool set_desktop --relative -- -1
xdotool set_desktop --relative 1