macOS平铺窗口神器yabai安装笔记

简介

yabai 是一个窗口管理实用程序,旨在作为 macOS 内置窗口管理器的扩展工作。yabai 允许您使用直观的命令行界面自由控制窗口、空间和显示,并可选择使用设置用户定义的键盘快捷键↗ skhd和其他第三方软件。

yabai的主要功能是平铺窗口管理;使用二进制空间分区算法自动修改窗口布局,让您专注于窗口的内容而不会分心。yabai 的其他功能包括焦点跟随鼠标、禁用切换空间的动画、创建超过 16 个空间限制的空间等等。

在这里插入图片描述

github主页:
https://github.com/koekeishiya/yabai

安装过程

禁用SIP

禁用系统完整性保护(Disable System Integrity Protection)
yabai 的以下功能需要(部分)禁用系统完整性保护:

  • 在没有动画的情况下聚焦/创建/破坏空间
  • 向左、向右或向另一个显示器移动空间(及其窗口)
  • 去除窗口阴影
  • 启用窗口透明度
  • 控制窗口层(使窗口出现在最上面)
  • 粘性窗口(使窗口出现在所有空间)
  • 通过单击其框架中的任意位置来移动窗口
  • 为任何给定窗口切换画中画
  • 聚焦和非活动窗口的边框

白苹果禁用方法请参考:
https://github.com/koekeishiya/yabai/wiki/Disabling-System-Integrity-Protection

OpenCore黑果禁用方法:
https://blog.csdn.net/lxyoucan/article/details/111984354

安装过程

我的环境是macOS Big Sur 11.4

以下命令逐个执行一遍。

brew install koekeishiya/formulae/yabai
# install the scripting addition
sudo yabai --install-sa
# if macOS Big Sur, load the scripting addition manually; follow instructions below to automate on startup
sudo yabai --load-sa

运行yabai

要运行 yabai,只需启动它。

# 启用 yabai
brew services start yabai

首次启动会申请权限,允许它即可。
在这里插入图片描述

macOS Big Sur - 启动时自动加载脚本添加

在 macOS Big Sur 中,我们不得不改用 mach API 来注入脚本添加。注入现在必须以提升的(root)权限运行,这意味着 yabai 不再能够在启动期间自动加载脚本添加。但是,您可以使用以下解决方法使其与以前一样无缝。诀窍是允许您的用户以root 用户身份执行yabai --load-sa,而无需输入密码。为此,我们添加了一个由/etc/sudoers加载的新配置条目。

# create a new file for writing - visudo uses the vim editor by default.
# go read about this if you have no idea what is going on.
sudo visudo -f /private/etc/sudoers.d/yabai

# input the line below into the file you are editing.
# replace <user> with your username (output of: whoami). 
# change the path to the yabai binary if necessary  (output of: which yabai)
<user> ALL = (root) NOPASSWD: /usr/local/bin/yabai --load-sa

记得把<user>修改成你的用户名就行了,我这里修改成itkey

完成上述编辑后,只需在 yabairc 配置文件的顶部添加加载脚本的命令

# the scripting-addition must be loaded manually if
# you are running yabai on macOS Big Sur. Uncomment
# the following line to have the injection performed
# when the config is executed during startup.
#
# for this to work you must configure sudo such that
# it will be able to run the command without password

sudo yabai --load-sa
yabai -m signal --add event=dock_did_restart action="sudo yabai --load-sa"

# .. more yabai startup stuff

更新到最新版本

要将 yabai 更新到最新版本,只需使用 Homebrew 升级它并重新安装脚本添加:

# stop, and upgrade yabai
brew services stop yabai
brew upgrade yabai

# uninstall the scripting addition
sudo yabai --uninstall-sa

# installing the scripting addition will restart Dock.app
sudo yabai --install-sa

# finally, start yabai
brew services start yabai

配置

每个用户的 yabai 配置文件必须是可执行的;它只是一个在 yabai 启动之前运行的 shell 脚本。它必须放置在以下位置之一(按顺序):

  • $XDG_CONFIG_HOME/yabai/yabairc
  • $HOME/.config/yabai/yabairc
  • $HOME/.yabairc
# create empty configuration file and make it executable
touch ~/.yabairc
chmod +x ~/.yabairc

我的配置如下:

#窗口平铺or浮动 bsp or float (default: bsp)
yabai -m config layout bsp

# Override default layout for space 2 only
#yabai -m config --space 2 layout float

# New window spawns to the left if vertical split, or top if horizontal split
yabai -m config window_placement first_child

# New window spawns to the right if vertical split, or bottom if horizontal split
yabai -m config window_placement second_child
# Set all padding and gaps to 20pt (default: 0)
# 顶部填充
yabai -m config top_padding    0 
# 底部填充
yabai -m config bottom_padding 5
yabai -m config left_padding   5
yabai -m config right_padding  5
yabai -m config window_gap     5

# Override gaps for space 2 only
yabai -m config --space 2 window_gap 0
# on or off (default: off)
yabai -m config auto_balance off
# Floating point value between 0 and 1 (default: 0.5)
yabai -m config split_ratio 0.5
# set mouse interaction modifier key (default: fn)
yabai -m config mouse_modifier fn

# set modifier + left-click drag to resize window (default: move)
yabai -m config mouse_action1 move

# set modifier + right-click drag to resize window (default: resize)
yabai -m config mouse_action2 resize
# set focus follows mouse mode (default: off, options: off, autoraise, autofocus)
yabai -m config focus_follows_mouse autoraise
# set mouse follows focus mode (default: off)
yabai -m config mouse_follows_focus on
# floating windows are always on top (default: off)
yabai -m config window_topmost on

# modify window shadows (default: on, options: on, off, float)
# example: show shadows only for floating windows
yabai -m config window_shadow float

# window opacity (default: off)
# example: render all unfocused windows with 90% opacity
yabai -m config window_opacity on
yabai -m config active_window_opacity 1.0
yabai -m config normal_window_opacity 0.9
#窗口与状态栏之间的间距设置
# add 20 padding to the top and 0 padding to the bottom of every space located on the main display
yabai -m config external_bar main:5:0
# add 20 padding to the top and bottom of all spaces regardless of the display it belongs to
yabai -m config external_bar all:5:5

窗口焦点切换

能否可以像vim中那样窗口快速的切换呢?我研究了一下文档是可以的。
窗口的焦点移动,使用如下的命令完成。

# swap window position and size with window in direction of focused window
#   (options: north, east, south, west)
yabai -m window --swap north

只要在命令行输入以下命令就可实现窗口的移动了。
输入命令还是比较麻烦的,能否把命令绑定到快捷键上呢?
答案是肯定的。
官方推荐使用https://github.com/koekeishiya/skhd来绑定快捷键与命令。

配合Karabiner-Elements

因为我已经在使用Karabiner-Elements了,而且很顺手。Karabiner-Elements功能与skhd很类似。我又不想两个都安装。所以我就简单研究了一下。使用Karabiner-Elements完全是可以搞定的。

我的配置分享一下:

{
    
    
  "title": "yabai window focus",
  "rules": [
    {
    
    
      "description": "left_command h/j/k/l",
      "manipulators": [
        {
    
    
          "type": "basic",
          "from": {
    
    
            "key_code": "h",
            "modifiers": {
    
    
              "mandatory": [
                "left_command"
              ]
            }
          },
          "to": [
            {
    
    
              "shell_command": "/usr/local/bin/yabai -m window --focus west"
            }
          ]
        },{
    
    
          "type": "basic",
          "from": {
    
    
            "key_code": "l",
            "modifiers": {
    
    
              "mandatory": [
                "left_command"
              ]
            }
          },
          "to": [
            {
    
    
              "shell_command": "/usr/local/bin/yabai -m window --focus east"
            }
          ]
        },{
    
    
          "type": "basic",
          "from": {
    
    
            "key_code": "j",
            "modifiers": {
    
    
              "mandatory": [
                "left_command"
              ]
            }
          },
          "to": [
            {
    
    
              "shell_command": "/usr/local/bin/yabai -m window --focus south"
            }
          ]
        },
        {
    
    
          "type": "basic",
          "from": {
    
    
            "key_code": "k",
            "modifiers": {
    
    
              "mandatory": [
                "left_command"
              ]
            }
          },
          "to": [
            {
    
    
              "shell_command": "/usr/local/bin/yabai -m window --focus north"
            }
          ]
        }
      ]
    }
  ]
}

配置文件看着长一些,其他也是非常好理解的。这里有一个小坑要注意一下。
shell_command中的值,要使用绝对路径。
正确✅ 写法

/usr/local/bin/yabai -m window --focus north

错误❌ 写法

yabai -m window --focus north

可能是yabai的环境变量与用户环境变量不同,无法正确识别用户的环境变量。所以要使用绝对路径。

关于窗口大小调整

#当前窗口高度放大
yabai -m window south --resize top:0:+20 2> /dev/null || yabai -m window north --resize bottom:0:-20
#当前窗口高度缩小
yabai -m window south --resize top:0:-20 2> /dev/null || yabai -m window north --resize bottom:0:+20
#当前窗口宽度放大
yabai -m window east --resize left:+20:0 2> /dev/null || yabai -m window west --resize right:-20:0
#当前窗口宽度缩小
yabai -m window east --resize left:-20:0 2> /dev/null || yabai -m window west --resize right:+20:0

备注:
这个目前不是完美的解决方案,在一个Space窗口少的情况下适用。窗口多的时候,无法调整夹在中间的窗口大小。当然这个窗口超多的情况,我的场景里非常少见。我基本一屏也就2个3个最多4个(我的显示器比较小)。真遇到这种情况就用鼠标调整大小吧。

cannot focus space due to an error with the scripting-addition

在配置文件~/.yabairc首行增加:

sudo yabai --load-sa

解决办法见:
https://github.com/koekeishiya/yabai/issues/787

参考

https://github.com/koekeishiya/yabai/wiki/Installing-yabai-(latest-release)

https://github.com/koekeishiya/yabai/wiki/Configuration#configuration-file

猜你喜欢

转载自blog.csdn.net/lxyoucan/article/details/121202575
今日推荐