Skip to content

Instantly share code, notes, and snippets.

@ralphholz
Last active March 27, 2018 22:31
Show Gist options
  • Save ralphholz/342637ec2325df0f375acc1635f9bbf8 to your computer and use it in GitHub Desktop.
Save ralphholz/342637ec2325df0f375acc1635f9bbf8 to your computer and use it in GitHub Desktop.

Using chunkwm with khd on macOS High Sierra

To use chunkwm, you have to go to considerably more trouble than you will be used to if you are coming from the older (but very nice) kwm version 3. If you are coming from kwm 4, you are probably right at home.

Note: these instructions have now been added to the chunkwm wiki: https://github.com/koekeishiya/chunkwm/wiki/Instructions:-chunkwm-with-(s)khd-on-macOS-High-Sierra

Understanding the daemon and plugin concept

chunkwm is heavily modularised. It consists of a daemon, chunkwm, and a command-line control tool, chunkc. Neither will directly interact with the user: you need to use khd for that. chunkc is used to send commands to chunkwm, which is listening on a socket (and chunkc sends to that socket).

khd is a second daemon/control tool set designed to interact with the user. It intercepts key presses and translates them to an action. This action is often an execution of chunkc with parameters; chunkc will then send commands to the chunkwm daemon. As a result, the chunkwm daemon is controlled via a chain of khd -> chunkc -> chunkwm.

chunkwm consists of plugins, which it loads automatically if they are located in the respective directory (see below).

Installing chunkwm and configuring

chunkwm itself is relatively nicely documented. A simple

brew install chunkwm

will install it. You can find my own .chunkwmrc in my repo: https://github.com/ralphholz-usyd/chunkwm-khd-config

Plugins are loaded from the directory specified in the .chunkwmrc.

chunkc

chunkc takes parameters that describe an action to be taken; this action is then executed by sending the corresponding control command via the socket to chunkwm. chunkc takes parameters for every supported plugin. The one that we are interested in is tiling. The documentation is a bit harder to find as it is hidden behind an inconspicuous link, but it is there: https://github.com/koekeishiya/chunkwm/blob/master/src/plugins/tiling/README.md

If you scroll through the documentation, you see that chunkc chooses the plugin to interact with and takes parameters that correspond to the plugin. E.g. to swap a window east-ward, it does this:

chunkc tiling::window --swap east

As you see, chunkc takes the plugin name, then the desired sub-functionality (here: window control) and then a switch and a parameter. It does a lot more - it also has sub-functionality referring to desktop and monitor control, for example, and also sets global parameters for the tiling layout etc.

If you are interested in other plugins, e.g., the border settings: they have their own command syntax, e.g.:

chunkc border::color 0xAARRGGBB

The link for border control that you want is this one: https://github.com/koekeishiya/chunkwm/blob/master/src/plugins/border/README.md

chunkwm is under heavy development, so expect the above to change. I will update this here when I become aware of changes.

On khd

khd, finally, is responsible for interaction with the user. I could not find any documentation for its use together with chunkwm, but once you understand the above concept of chunkc controlling chunkwm, it is fairly straight-forward. Essentially, khd has a configuration file that defines key mappings and binds them to an action to be executed. That action can be either a call to chunkc or to khd itself (in which case it acts like a client to the daemon). Calling chunkc allows you to trigger the control action over windows or borders. Calling khd allows you to remap keys if you need that. The config file is fairly powerful because it even allows you to define applications that are to be exempt from a given mapping (or the mapping may refer only to particular applications). This is an example mapping in .khdrc:

cmd + shift - h : chunkc tiling::window --swap west

The example will swap a window west-ward.

Installing khd and configuring

khd can be installed via brew, too. However, do not go and take the provided sample config file from its github repo: that one refers to kwm 4. You need to rewrite it for use with chunkwm. However, it does a neat job of explaining the modes: khd supports a variety of modes - similar to vi, these can be activated by a key combination and then further key combinations determine an action. It is not necessary to make use of this functionality if all you want is window control - but it can be a powerful feature, from what I see there.

Configuration of khd consists of editing .khdrc. You define the desired mapping and bind it to, e.g., a call to chunkc.

Testing features

I found it extremely useful to test an action before defining it as a key mapping in .khdrc. You can simply call chunkc with the necessary parameters in a terminal to see whether the action it executes is the one you want. E.g.,

chunkc tiling::window --swap west

will work fine from the command line and swap the window.

Note that this has an important implication: you can just replace khd with any other daemon or tool that can execute a command and run chunkc. You can even write bash scripts that quickly rearrange your windows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment