window-managersi3

i3 - Move container to next/previous ws (also if nonexisting)


I'm running i3 version 4.16

I would like to bind a shortcut to move the current container to the next/previous workspace whether existing or not.

With help from this post using jq, I have a 'sort of' working solution but it is very flaky (sometimes it switches to the workspace without moving the container and sometimes it moves a container from the next ws to the current ws before switching ws)

Can anyone come up with a better solution ???

My i3 config:

set $ws-right "$(( $( i3-msg -t get_workspaces | jq '.[] | select(.focused).num' ) + 1))";
set $ws-left "$(( $( i3-msg -t get_workspaces | jq '.[] | select(.focused).num' ) - 1))";

bindsym shift+control+$mod+Left exec i3-msg move container to workspace $ws-left; exec i3-msg workspace $ws-left
bindsym shift+control+$mod+Right exec i3-msg move container to workspace $ws-right; exec i3-msg workspace $ws-right

Thx for your time ;)


Solution

  • You can achieve this result also by using native i3 messages, such as:

    bindsym $mod+Shift+n move container to workspace next_on_output; workspace next_on_output
    bindsym $mod+Shift+p move container to workspace prev_on_output; workspace prev_on_output
    

    The advantage of this solution is, that it works per display and also if your display names have icons in it or different names.

    Your script could also be improved like this:

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

    By using workspace number, your script also works for different workspace names.