Move to the next / previous workspace

This Bash script allows you to move the focused window to the next or previous i3 workspace. This is very useful, as it allows you to get a tiled windo>

In your i3 configuration file, put the following two lines:

  • bindsym $super+period exec ~/scripts/i3/window.sh
  • bindsym $super+comma exec ~/scripts/i3/window.sh back
#!/bin/bash

OLD_IFS="$IFS"
IFS=$'\n' win_array=($(i3-msg -t get_config | grep 'set $workspace' | sed 's/^.\+ .\+\(".\+\)$/\1/'))
IFS="$OLD_IFS"

win_length=${#win_array[@]}

#echo $win_array
#echo $win_length

move=1
max="$((win_length-1))"
next_max=-1

if [ "$1" == 'back' ]
then
  move=-1
  max=0
  next_max="$(win_length)"
fi

wsCurrent=$( i3-msg -t get_workspaces | jq '.[] | select(.focused).name' )

#echo $wsCurrent

for i in "${!win_array[@]}"; do
   if [[ "${win_array[$i]}" = "${wsCurrent}" ]]; then
       myWin=${i};
   fi
done

#echo $myWin

if [ $myWin -eq $max ]
then
    myWin=$next_max
fi

newWin_index=$(($myWin + $move))

newWin=${win_array[$newWin_index]}

i3-msg move container workspace $newWin, workspace $newWin

exit 0