Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to focus most recent window in a space after switching spaces #719

Closed
noib3 opened this issue Nov 15, 2020 · 5 comments
Closed

How to focus most recent window in a space after switching spaces #719

noib3 opened this issue Nov 15, 2020 · 5 comments
Labels
question Request for information or help, not an issue

Comments

@noib3
Copy link

noib3 commented Nov 15, 2020

After switching spaces I often have to manually focus a window in that space, which gets really annoying really quickly. Instead I'd like yabai to always focus the most recent window in the space I'm in. I tried the following commands in my skhdrc (here shown for spaces 1 and 2):

alt - 1 : yabai -m space --focus 1 && yabai -m window --focus "$(yabai -m query --windows --space | jq .[0].id)"
alt - 2 : yabai -m space --focus 2 && yabai -m window --focus "$(yabai -m query --windows --space | jq .[0].id)"

but those didn't seem to solve anything.

Btw, I have When switching to an application, switch to a Space with open windows for the application unchecked in System Preferences -> Mission Control.

@koekeishiya
Copy link
Owner

koekeishiya commented Nov 16, 2020

You can use the following signal(s):

# focus window after active space changes
yabai -m signal --add event=space_changed action="yabai -m window --focus \$(yabai -m query --windows --space | jq .[0].id)"

# focus window after active display changes
yabai -m signal --add event=display_changed action="yabai -m window --focus \$(yabai -m query --windows --space | jq .[0].id)"

No need to modify your focus binds.

@koekeishiya koekeishiya added the question Request for information or help, not an issue label Nov 16, 2020
@noib3
Copy link
Author

noib3 commented Nov 16, 2020

Thanks, that solved it!

@badloop
Copy link

badloop commented Feb 14, 2023

For anyone still running into this issue, I found that Microsoft Teams was usurping the 0 spot in the window array on space that it had been seen on (Great job, Microsoft.... :-( ) I tend to have a single space for most of my windows that is labeled with the name of the application that will be open on it, so I wrote a little helper function using the above so that whenever I switch to a space, it will focus the window that has the same name as the space. Hope this helps!

focus_window () {
    SPACE_NAME=$(yabai -m query --spaces --space | jq ".label")
    WINDOW_ID=$(yabai -m query --windows --space | jq ".[] | select (.app=${SPACE_NAME}).id")
    yabai -m window --focus "${WINDOW_ID}"
}

# focus window after active space changes
yabai -m signal --add event=space_changed action="focus_window"

# focus window after active display changes
yabai -m signal --add event=display_changed action="focus_window"

@Arelav
Copy link

Arelav commented Jun 30, 2023

@koekeishiya Thanks a lot.

No need to modify your focus binds.

I'll leave it here just in case it won't work for somebody as in my case. I had a floating MS Teams window and to make it work I changed it to jq .[-1].id to use a last window.

@webavant
Copy link

webavant commented Apr 4, 2024

Using a third-party tool, synergy-core, I had the same problem @badloop mentioned above. A working solution included selecting the first visible window using jq:

yabai -m signal --add event=space_changed action='yabai -m window --focus $(yabai -m query --windows --space | jq -r '\''[.[]|select(."is-visible")][0].id'\'')'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Request for information or help, not an issue
Projects
None yet
Development

No branches or pull requests

5 participants