i3wm: How to Switch Workspaces on Multiple Monitors with Single Binding

i3window-managerworkspaces

With Unity, both of my monitors are always on the same workspace. If I start at workspace 1 and then press ctrl+alt+right, both of my monitors shift to the new workspace to the right. This behavior is important to my workflow.

I'd like to switch to i3, but it looks like the 2 monitors are not always required to point to the same workspace. Is there a way to change this behavior so that, in i3, when I press $mod+Shift+num, BOTH screens change instead of just the one that has focus?

Best Answer

It is (somewhat) possible to emulate this "feature" (For me, the workspace behaviour of i3 is one of the main features, so I do not really recommend to do this unless you really cannot work without workspaces spanning all monitors while still wanting to use i3).

You can bind multiple commands to shortcuts, so you just can switch workspaces on both monitors at once. For example:

# Default modifier key
set $mod Mod4

# variables for left and right monitor 
# (assuming names DP-1 and DP-3 with DP-1 being left of DP-3)
set $monL DP-1
set $monR DP-3

# variables for workpacenames; 
set $ws1L 1:A
set $ws1R 11:A
set $ws2L 2:B
set $ws2R 12:B
set $ws3L 3:C
set $ws3R 13:C

# assign workspaces to specific outputs
workspace $ws1L output $monL
workspace $ws1R output $monR
workspace $ws2L output $monL
workspace $ws2R output $monR
workspace $ws3L output $monL
workspace $ws3R output $monR

# do not toggle automatically between the current and previous 
# workspace on repeated calls to `workspace NAME` (with identical NAME)
workspace_auto_back_and_forth no

# Toggle between both monitors 
# (focus output left wraps from the leftmost to the rightmost output)
bindsym $mod+Tab focus output left

# switch two workspaces at once (the need to be on different outputs)
bindsym $mod+1 workspace $ws1L, workspace $ws1R
bindsym $mod+2 workspace $ws2L, workspace $ws2R
bindsym $mod+3 workspace $ws3L, workspace $ws3R

# move a window to a specific workpace; 
# $mod+Shift for left side, $mod+Control for right side
bindsym $mod+Shift+1 move workspace $ws1L
bindsym $mod+Control+1 move workspace $ws1R
bindsym $mod+Shift+2 move workspace $ws2L
bindsym $mod+Control+2 move workspace $ws2R
bindsym $mod+Shift+3 move workspace $ws3L
bindsym $mod+Control+3 move workspace $ws3R

bar {
    # [...]
    # Do not show workspace numbers (optional)
    strip_workspace_numbers yes
}

This uses variables to keep the workspace and output names consistent. The workspace names for the left and right monitor need to be different. In this case I chose to use different numbers because they can be hidden, if so desired. Whith these settings Super+1 will switch to workspace 1:A on the left monitor and 11:A on the right monitor. workspace_auto_back_and_forth should be disabled to avoid possibly confusing and undesired behavior.

Caveats:

  • This works only for the keyboard shortcuts, if you click on a workspace button on the i3bar, only the workspace on one monitor will change and not its companion.
  • Any command that focuses a window on a currently not visible workspace ([criteria] focus) will only focus that workspace without the one on the other output
  • When switching two workspaces with one binding, the second workspace will always be the focused one (in the above example the right one).
  • Focusing and movement across display borders may require different commands/keybindings in some cases. (While this is also the case with non-synchronized workspaces, it may be more noticeable/disrupting when expecting one big workspace)

Some if not all of the caveats could probably be solved by utilizing the IPC interface, for example to automatically switch to the companion workspace, too.


Personally, I would suggest to at least adopt a hybrid approach: Use synchronized workspaces where you absolutely need them for your workflow, but use the default (for i3) single display approach anywhere else. Otherwise, if you absolutely need/want synchronized workspaces all the time, it might be that i3 is just not the window manager you are looking for.

Related Question