見出し画像

yabaiとskhdで爆速ウィンドウ操作環境を構築してみた

はじめに

こんにちは。
メディアラボチームで新規事業に取り組んでいる市野です。

PCを利用する上で避けられないウィンドウ操作 (アプリケーションを開いたときに表示されるあれです) 。表示されたあとに、適切なサイズに変更したり、他のウィンドウと被らないように位置を変更することも多いのではないでしょうか。

これって地味に時間がかかる細かい作業ですよね、、、

もし、マウスを使わずにキーボードショートカットだけで適切な位置・サイズに変更できたら、、、

もし、なんならキーボードすらも使わず、表示されているウィンドウ群を自動で適切なサイズ・位置に変更できたら、、、

実はそのようなことを可能にしてくれるツールが開発・公開されています。自称効率厨の私としては、頻繁に行うウィンドウ操作の効率化は見逃せません。大きな可能性を秘めている "ウィンドウマネージャー" について今回はご紹介します。

本記事では私が主に利用しているMacOS向けのウィンドウマネージャーについて取り扱います。ただし、この類のツールはMacOS以外にも、各OS向けに様々なものが公開されています。MacOS以外を愛用されている方も、この機会にぜひウィンドウ操作ツールを検討してみてはいかがでしょうか。

実際どんな感じなの?

見てみないことにはイメージが掴めないですよね。まずはこちらのデモ動画をご覧ください。

様々なウィンドウ操作をキーボードショートカットだけで行っていますね。
- 複数ウィンドウが画面全体に表示されるように各ウィンドウのサイズ・位置変更
- アクティブウィンドウの変更
- ウィンドウのサイズ・位置の変更
- ワークスペースの変更
などなど。

yabaiとskhdとは

雰囲気もわかったところでツールのご紹介です。ウィンドウマネージャーといっても、MacOS用だけでも様々なものがあります。今回は無料で手軽に利用できるyabaiskhdをご紹介します。上のデモもこのツールを利用しています。

yabai

yabai is a window management utility that is designed to work as an extension to the built-in window manager of macOS. yabai allows you to control your windows, spaces and displays freely using an intuitive command line interface and optionally set user-defined keyboard shortcuts using ↗ skhd and other third-party software.

https://github.com/koekeishiya/yabai

今回のメインとなるウィンドウマネージャーです。コマンドを実行することで、デモにあったようなウィンドウやワークスペースの操作を可能にします。

余談ですが、なぜyabaiって名前なんでしょうね。日本語の"やばい"からきているのか、、? 確か元となった先代ツールは全然違った普通の名前だった記憶があります。

Amethystとの比較

公式サイトでは類似のAmethystというツールとの比較も掲載されています。yabaiの高性能さがみてとれます。ただ、多機能が故に操作が複雑になるというデメリットも。それをフォローするツールを次にご紹介します。

比較テーブル
https://github.com/koekeishiya/yabai/wiki#comparison-with-other-window-managers

skhd

skhd is a simple hotkey daemon for macOS that focuses on responsiveness and performance. Hotkeys are defined in a text file through a simple DSL. skhd is able to hotload its config file, meaning that hotkeys can be edited and updated live while skhd is running.

https://github.com/koekeishiya/skhd

キーボードのショートカットをカスタマイズするツールです。skhdを使うことで、ショートカットとyabaiのどのウィンドウ操作コマンドを紐付けするか、設定することができます。

yabaiは実行できるウィンドウ操作の種類が多いため、コマンドの丸暗記はハードルが高いです。普段よく使いそうなウィンドウ操作処理を自分が覚えやすいショートカットに設定しておくことを、個人的にはおすすめします。

Install

インストール方法は公式のWikiに記載されています。管理を考えるとhomebrewを使ったインストールが便利です。定番ですがインストールが完了したら、プライバシーとアクセシビリティの許可をそれぞれに対して行います。

環境

  • macOS Catalina 10.15.1

yabai

# install with homebrew
brew install koekeishiya/formulae/yabai

# start yabai
brew services start yabai

(プライバシーとアクセシビリティの許可)

# load the scripting addition
killall Dock

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

skhd

brew install koekeishiya/formulae/skhd
brew services start skhd
 
(プライバシーとアクセシビリティの許可)

https://github.com/koekeishiya/skhd

Setting

公式に設定ファイルのサンプルが用意されています。これをベースに自分好みに設定ファイルを弄ってみてください。skhdでいかに自分自身が使いやすいキーボードショートカットをカスタマイズできるかがyabaiを使いこなすポイントです。

カスタマイズしてOS側ですでに利用されているキーボードショートカットと重複しないように気をつけてください。

yabai

#!/usr/bin/env sh

# 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
#
# see this wiki page for information:
#  - https://github.com/koekeishiya/yabai/wiki/Installing-yabai-(latest-release)
#
# sudo yabai --load-sa
# yabai -m signal --add event=dock_did_restart action="sudo yabai --load-sa"

# global settings
yabai -m config mouse_follows_focus          off
yabai -m config focus_follows_mouse          off
yabai -m config window_origin_display        default
yabai -m config window_placement             second_child
yabai -m config window_topmost               off
yabai -m config window_shadow                on
yabai -m config window_opacity               off
yabai -m config window_opacity_duration      0.0
yabai -m config active_window_opacity        1.0
yabai -m config normal_window_opacity        0.90
yabai -m config window_border                off
yabai -m config window_border_width          6
yabai -m config active_window_border_color   0xff775759
yabai -m config normal_window_border_color   0xff555555
yabai -m config insert_feedback_color        0xffd75f5f
yabai -m config split_ratio                  0.50
yabai -m config auto_balance                 off
yabai -m config mouse_modifier               fn
yabai -m config mouse_action1                move
yabai -m config mouse_action2                resize
yabai -m config mouse_drop_action            swap

# general space settings
yabai -m config layout                       bsp
yabai -m config top_padding                  12
yabai -m config bottom_padding               12
yabai -m config left_padding                 12
yabai -m config right_padding                12
yabai -m config window_gap                   06

echo "yabai configuration loaded.."

skhd

# ################################################################ #
# THE FOLLOWING IS AN EXPLANATION OF THE GRAMMAR THAT SKHD PARSES. #
# FOR SIMPLE EXAMPLE MAPPINGS LOOK FURTHER DOWN THIS FILE..        #
# ################################################################ #

# A list of all built-in modifier and literal keywords can
# be found at https://github.com/koekeishiya/skhd/issues/1
#
# A hotkey is written according to the following rules:
#
#   hotkey       = <mode> '<' <action> | <action>
#
#   mode         = 'name of mode' | <mode> ',' <mode>
#
#   action       = <keysym> '[' <proc_map_lst> ']' | <keysym> '->' '[' <proc_map_lst> ']'
#                  <keysym> ':' <command>          | <keysym> '->' ':' <command>
#                  <keysym> ';' <mode>             | <keysym> '->' ';' <mode>
#
#   keysym       = <mod> '-' <key> | <key>
#
#   mod          = 'modifier keyword' | <mod> '+' <mod>
#
#   key          = <literal> | <keycode>
#
#   literal      = 'single letter or built-in keyword'
#
#   keycode      = 'apple keyboard kVK_<Key> values (0x3C)'
#
#   proc_map_lst = * <proc_map>
#
#   proc_map     = <string> ':' <command> | <string>     '~' |
#                  '*'      ':' <command> | '*'          '~'
#
#   string       = '"' 'sequence of characters' '"'
#
#   command      = command is executed through '$SHELL -c' and
#                  follows valid shell syntax. if the $SHELL environment
#                  variable is not set, it will default to '/bin/bash'.
#                  when bash is used, the ';' delimeter can be specified
#                  to chain commands.
#
#                  to allow a command to extend into multiple lines,
#                  prepend '\' at the end of the previous line.
#
#                  an EOL character signifies the end of the bind.
#
#   ->           = keypress is not consumed by skhd
#
#   *            = matches every application not specified in <proc_map_lst>
#
#   ~            = application is unbound and keypress is forwarded per usual, when specified in a <proc_map>
#
# A mode is declared according to the following rules:
#
#   mode_decl = '::' <name> '@' ':' <command> | '::' <name> ':' <command> |
#               '::' <name> '@'               | '::' <name>
#
#   name      = desired name for this mode,
#
#   @         = capture keypresses regardless of being bound to an action
#
#   command   = command is executed through '$SHELL -c' and
#               follows valid shell syntax. if the $SHELL environment
#               variable is not set, it will default to '/bin/bash'.
#               when bash is used, the ';' delimeter can be specified
#               to chain commands.
#
#               to allow a command to extend into multiple lines,
#               prepend '\' at the end of the previous line.
#
#               an EOL character signifies the end of the bind.

# ############################################################### #
# THE FOLLOWING SECTION CONTAIN SIMPLE MAPPINGS DEMONSTRATING HOW #
# TO INTERACT WITH THE YABAI WM. THESE ARE SUPPOSED TO BE USED AS #
# A REFERENCE ONLY, WHEN MAKING YOUR OWN CONFIGURATION..          #
# ############################################################### #

# focus window
# alt - h : yabai -m window --focus west

# swap managed window
# shift + alt - h : yabai -m window --swap north

# move managed window
# shift + cmd - h : yabai -m window --warp east

# balance size of windows
# shift + alt - 0 : yabai -m space --balance

# make floating window fill screen
# shift + alt - up     : yabai -m window --grid 1:1:0:0:1:1

# make floating window fill left-half of screen
# shift + alt - left   : yabai -m window --grid 1:2:0:0:1:1

# create desktop, move window and follow focus - uses jq for parsing json (brew install jq)
# shift + cmd - n : yabai -m space --create && \
#                   index="$(yabai -m query --spaces --display | jq 'map(select(."native-fullscreen" == 0))[-1].index')" && \
#                   yabai -m window --space "${index}" && \
#                   yabai -m space --focus "${index}"

# fast focus desktop
# cmd + alt - x : yabai -m space --focus recent
# cmd + alt - 1 : yabai -m space --focus 1

# send window to desktop and follow focus
# shift + cmd - z : yabai -m window --space next; yabai -m space --focus next
# shift + cmd - 2 : yabai -m window --space  2; yabai -m space --focus 2

# focus monitor
# ctrl + alt - z  : yabai -m display --focus prev
# ctrl + alt - 3  : yabai -m display --focus 3

# send window to monitor and follow focus
# ctrl + cmd - c  : yabai -m window --display next; yabai -m display --focus next
# ctrl + cmd - 1  : yabai -m window --display 1; yabai -m display --focus 1

# move floating window
# shift + ctrl - a : yabai -m window --move rel:-20:0
# shift + ctrl - s : yabai -m window --move rel:0:20

# increase window size
# shift + alt - a : yabai -m window --resize left:-20:0
# shift + alt - w : yabai -m window --resize top:0:-20

# decrease window size
# shift + cmd - s : yabai -m window --resize bottom:0:-20
# shift + cmd - w : yabai -m window --resize top:0:20

# set insertion point in focused container
# ctrl + alt - h : yabai -m window --insert west

# toggle window zoom
# alt - d : yabai -m window --toggle zoom-parent
# alt - f : yabai -m window --toggle zoom-fullscreen

# toggle window split type
# alt - e : yabai -m window --toggle split

# float / unfloat window and center on screen
# alt - t : yabai -m window --toggle float;\
#           yabai -m window --grid 4:4:1:1:2:2

# toggle sticky(+float), topmost, picture-in-picture
# alt - p : yabai -m window --toggle sticky;\
#           yabai -m window --toggle topmost;\
#           yabai -m window --toggle pip

おわりに

今回は、MacOS向けのウィンドウ操作ツールyabaiと併用されるキーボードショートカットカスタマイズツールskhdのご紹介でした。

ウィンドウ操作は地味だけど頻繁に行っているチリツモ作業です。この類のツールを使えば効率化に繋がるかもしれません。yabaiは多機能が故に癖が強いかもしれませんが、強力なツールですのでぜひ活用してみてください。

ちなみにMac向けのウィンドウ操作ツールにMoomというものがあります。有料ではありますが、複数ウィンドウのレイアウトやウィンドウの位置・サイズ変更に特化していて、こちらも非常におすすめです。以下の記事で紹介していますので、もしご興味があればご覧になってください。

(メディアラボ・市野晴之)