Alberto Álvarez
16 Jul 2020

How to move to the next or previous workspace in i3

I have been trying to find default keybindings in i3 to move to the next and previous workspaces for some time now without any luck. Recently I found an alternative way to do it that works for my setup. Here it is:

First we need to install jq, which is a JSON parser. This can be done by calling this in a terminal (assuming that you are using any flavour of Ubuntu):

sudo apt install jq

Once this was installed, I created bash scripts called .i3-move-next.sh and .i3-next.sh, and placed them under my home directory. Both of them must have the necessary permissions to allow the execution as programs. The contents of the files are as follows:

.i3-move-next.sh

wsNext=$(( $( i3-msg -t get_workspaces | jq '.[] | select(.focused).num' ) + $1))
i3-msg move container to workspace $wsNext
i3-msg workspace $wsNext

.i3-next.sh

wsNext=$(( $( i3-msg -t get_workspaces | jq '.[] | select(.focused).num' ) + $1))
i3-msg workspace number $wsNext

Finally I used these keybindings in my i3 configuration file:

bindsym $mod+bracketright exec ~/.i3-move-next.sh 1
bindsym $mod+bracketleft exec ~/.i3-move-next.sh -1
bindsym $mod+equal exec ~/.i3-next.sh 1
bindsym $mod+minus exec ~/.i3-next.sh -1

Now I can use the mod key and [ to move a container to the previous workspace and the mod key with ] to send it to the next one. Similarly, if I just want to go to these workspaces without bringing a container I can just type the mod key and = to go to next and - to go to the previous one. This was done based on the type of keyboard I was using, it might not be as efficient for other configuration. The important thing here is that you can just change the key to whatever you like to achieve the same thing. I didn't test this solution extensively, still have to see if it works for a multiple monitor setup.

The solution came from the comments in this Reddit thread.

Tags: i3
alberto.am by Alberto Álvarez is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.