Skip to content

Instantly share code, notes, and snippets.

@m-bartlett
Created December 14, 2021 21:59
Show Gist options
  • Save m-bartlett/b0915fb08f2216fe64fea7f9f883500f to your computer and use it in GitHub Desktop.
Save m-bartlett/b0915fb08f2216fe64fea7f9f883500f to your computer and use it in GitHub Desktop.
i3 ipc script to allow breadth-first movement for moving focus between highly nested containers.

i3-focus-skip-tabs

This parses the i3 workspace tree from the i3 IPC to execute focus commands on the outermost tabbed container that has 1+ siblings. If such a container is not found in the hierarchy up from the focused window to the root container then this will call a regular focus command. In theory this script can take any valid arguments that the i3 command focus takes (e.g. prev and next, etc.), however it has only been tested with leftandright.

The idea is this will move focus between the highest level containers without requiring several focus parent commands if working with highly nested containers.

Demo

The following video demo is running with the following keybinds, i.e. the "normal" keybind to change focus instead runs this script with super+alt+(left/right) being a fallback to normal focus (left/right.

set $super Mod4
set $alt Mod1
bindsym $super+Left exec i3-focus-skip-tabs left
bindsym $super+$alt+Left focus left
bindsym $super+Right exec i3-focus-skip-tabs right
bindsym $super+$alt+Right focus right

i3-focus-skip-tabs demo Keystrokes recorded with the screenkey utility

#!/usr/bin/python3
import i3ipc # pip install i3ipc or pacman -S python-i3ipc
import json
import sys
i3 = i3ipc.Connection()
try:
direction = sys.argv[1]
except IndexError:
direction = 'right'
focused = i3.get_tree().find_focused()
primogeniture = focused.id
while focused.type == 'con':
p = focused.parent
if len(p.nodes) > 1 and 'tabbed' in [n.layout for n in p.nodes]:
primogeniture = focused.id
focused = p
i3.command(f'[con_id={primogeniture}] focus; focus {direction}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment