yabai icon indicating copy to clipboard operation
yabai copied to clipboard

How to assign specific spaces to their own display?

Open windowsrefund opened this issue 2 years ago • 10 comments

Sorry if I'm missing something obvious but I've read through tons of documentation and have yet to figure out what I consider to be a fairly common thing as far as tilers are concerned.

I have 2 displays and just want each to contain 2 unique spaces. So basically something like:

[1] [2] | [3] [4]

Is there no way to assign them?

windowsrefund avatar Apr 08 '22 18:04 windowsrefund

I have the same issues currently. I have three displays, each with 9 spaces and I want them to act as 9 desktops. That was possible with Total Spaces 2, but that doesn’t run under Monterey. Currently I use skhd to switch all spaces. But it doen’t work too well if one spaces changes because a program gets activated.

24unix avatar Apr 08 '22 18:04 24unix

yabairc: yabai -m space 1 --label one yabai -m space 2 --label two yabai -m space 3 --label three yabai -m space 4 --label four

skhdrc: fn - 1 : yabai -m space --focus 1 fn - 2 : yabai -m space --focus 2 fn - 3 : yabai -m space --focus 3 fn - 4 : yabai -m space --focus 4

System Prefs > Mission Control: Uncheck - Automatically rearrange Check - Displays have separate spaces

PhrantiK avatar Apr 11 '22 08:04 PhrantiK

I have these settings in place but spaces 1-3 are assigned to monitor 1 and only space 4 is assigned to monitor 2.

windowsrefund avatar Apr 11 '22 14:04 windowsrefund

If you launch mission control, at top of the screen the desktop numbers correspond to the space numbers in yabai.

Not sure if full screen apps mess the numbers up but I have 5 spaces set up across 2 monitors like this: [1] [2] [3] | [4] [5]

My mission control looks like:

Screen Shot 2022-04-13 at 21 07 52 Screen Shot 2022-04-13 at 21 11 10

PhrantiK avatar Apr 13 '22 13:04 PhrantiK

RIght but this issue speaks to the inability to assign spaces to particular displays.

windowsrefund avatar Apr 13 '22 16:04 windowsrefund

If you create a space using yabai (yabai -m space --create [<SPACE_SEL>]), you can specify a space belonging to the display that you want the new space to be created for. This should probably be changed to just taking a DISPLAY_SEL directly instead, but you can get around that by using a query in addition to the create command.

You can move existing spaces to other displays using yabai -m space --display <DISPLAY_SEL>

You can also just drag the spaces between displays in mission-control (standard macOS functionality).

koekeishiya avatar Apr 14 '22 11:04 koekeishiya

Feel like we're going in circles a bit. Why would I want or need to create a space when I've already mentioned I've got 4 defined in my yabairc?

Then there's the suggestion to use yabai -m space --display <DISPLAY_SEL> but that command doesn't include any indication of exactly which space should be moved to the targeted display.

Reviewing the man page allowed me to realize the actual way to move a space is yabai -m space <SPACE_SEL> --display <DISPLAY_SEL>

So perhaps I'll just be able to add a few versions of that command into my yabairc

windowsrefund avatar Apr 14 '22 18:04 windowsrefund

Well then you just need to make sure that when you initially create your spaces, that they are created on the correct display. The spaces system in macOS is a part of, and lives in, Dock.app.

The native way of doing this is to open mission-control and use their + button to create a new space, or drag existing spaces between your displays in mission-control. The yabai command simply hooks into this part of the Dock.app code and lets you call it from the CLI instead.

Not sure I really get the issue here.

koekeishiya avatar Apr 14 '22 19:04 koekeishiya

The issue is that the docs either don't do the job or just do a poor job of explaining how to accomplish this. Based on where this thread has gone, it seems like the docs could just say "We really only kind of hook into Mission Control so just go and use that and don't bother trying to establish and maintain this with piece of your tiling experience via Yabai"

windowsrefund avatar Apr 14 '22 19:04 windowsrefund

I couldn't find a way to "assign" spaces on creation, but have resolved to moving them thereafter yabai -m space 1 --display 2 etc. This is covered in the docs.

¯\_(ツ)_/¯

dgrebb avatar Nov 23 '23 20:11 dgrebb

I couldn't find a way to "assign" spaces on creation, but have resolved to moving them thereafter yabai -m space 1 --display 2 etc. This is covered in the docs.

¯_(ツ)_/¯

Are you sure it works? When I do that, all my windows disappear, although they are present in the space.

weeebdev avatar Feb 10 '24 13:02 weeebdev

@weeebdev this has been working for me a few months now — beware display order in the setup_space() function.

Expand `_spaces.sh`
#!/bin/bash

# Space Mapper
setup_space() {
  local idx="$1"
  local name="$2"
  local space=
  echo "setup space $idx : $name"

  space=$(yabai -m query --spaces --space "$idx")
  if [ -z "$space" ]; then
    yabai -m space --create
  fi

  yabai -m space "$idx" --label "$name"
  if [[ "$idx" -gt "3" && "$idx" -lt "7" ]]; then
    yabai -m space "$idx" --display 2
  elif [ "$idx" -lt "4" ]; then
    yabai -m space "$idx" --display 1
  else
    yabai -m space "$idx" --display 3
  fi
}

# Setup or destroy spaces as needed to match 9
for _ in $(yabai -m query --spaces | jq '.[].index | select(. > 9)'); do
  yabai -m space --destroy 10
done

# Set Up Spaces
setup_space 1 music
setup_space 2 web
setup_space 3 work
setup_space 4 code
setup_space 5 maincode
setup_space 6 project
setup_space 7 terminal
setup_space 8 office
setup_space 9 plan

main_display_padding=(
  top_padding 133
  bottom_padding 133
  left_padding 233
  right_padding 233
)

# Set space padding
yabai -m config --space 1 "${main_display_padding[@]}"
yabai -m config --space 2 "${main_display_padding[@]}"
yabai -m config --space 3 "${main_display_padding[@]}"

# Set Always-Floating Spaces
yabai -m config --space 1 layout float
yabai -m config --space 3 layout float

yabai -m rule --add app="^Music$" space=1
yabai -m rule --add app="^(Firefox|Obsidian)$" space=^2
yabai -m rule --add app="^(Notion|Photoshop|Lightroom|Pym|Slack|Discord)$" space=3
yabai -m rule --add app="^iTerm$" space=4
yabai -m rule --add app="^(OmniFocus|Mail|Calendar)$" space=5
yabai -m rule --add app="^Code$" space=8

This runs at the bottom of yabairc:

# set up spaces after applying all configuration
source $HOME/.config/yabai/_spaces.sh

A lot of credit due to @d12frosted for this post: Automatic setup of spaces with yabai.

dgrebb avatar Feb 11 '24 02:02 dgrebb