Yabai: I really just want one shortcut that allows me to cycle through all open windows in a space, is this possible?

Created on 22 Nov 2019  Â·  5Comments  Â·  Source: koekeishiya/yabai

I don't need to focus to other windows via north, south, east, west, I'd rather just focus to whichever windows are open in the current space so using

alt - s : yabai -m window --focus recent
only gets me the last window (doesn't work for 2+ windows) and I have to have selected the other window for it to work and

alt - s : yabai -m window --focus next
only goes to the next one window and ends when it has gone through all the windows

Most helpful comment

This has always worked quite well for me for switching between all visible windows including floating windows on all visible spaces sorted by their position. Requires jq to be installed.

# cycle forwards
alt - tab : yabai -m query --spaces \
  | jq -re ".[] | select(.visible == 1).index" \
  | xargs -I{} yabai -m query --windows --space {} \
  | jq -sre "add | sort_by(.display, .frame.x, .frame.y, .id) | reverse | nth(index(map(select(.focused == 1))) - 1).id" \
  | xargs -I{} yabai -m window --focus {}

# cycle backwards
shift +  alt - tab : yabai -m query --spaces \
  | jq -re ".[] | select(.visible == 1).index" \
  | xargs -I{} yabai -m query --windows --space {} \
  | jq -sre "add | sort_by(.display, .frame.x, .frame.y, .id) | nth(index(map(select(.focused == 1))) - 1).id" \
  | xargs -I{} yabai -m window --focus {}

All 5 comments

This has always worked quite well for me for switching between all visible windows including floating windows on all visible spaces sorted by their position. Requires jq to be installed.

# cycle forwards
alt - tab : yabai -m query --spaces \
  | jq -re ".[] | select(.visible == 1).index" \
  | xargs -I{} yabai -m query --windows --space {} \
  | jq -sre "add | sort_by(.display, .frame.x, .frame.y, .id) | reverse | nth(index(map(select(.focused == 1))) - 1).id" \
  | xargs -I{} yabai -m window --focus {}

# cycle backwards
shift +  alt - tab : yabai -m query --spaces \
  | jq -re ".[] | select(.visible == 1).index" \
  | xargs -I{} yabai -m query --windows --space {} \
  | jq -sre "add | sort_by(.display, .frame.x, .frame.y, .id) | nth(index(map(select(.focused == 1))) - 1).id" \
  | xargs -I{} yabai -m window --focus {}

I’m sorry, but what is ‘jq’?

It's a a tool for processing JSON on the command line. yabai -m query ... returns JSON-formatted output, and jq makes getting a specific value from it a a breeze.

It's available on Homebrew: brew install jq.

I shall give it a try when I’m next near my computer, thanks much!

For posterity, this works 100% perfectly!

Thank you @dominiklohmann !

Was this page helpful?
0 / 5 - 0 ratings