Using Karabiner For Application Window Management


Introduction

Karabiner-Elements is a great tool for manipulating keybindings on MacOS. Let's use it to turn your Capslock key into a hyper key, and use it for application window management.

Turning your Capslock into a Hyper key gives you another modifier key to map just about anything you want to it. Including running/switching apps, and even moving the mouse with your keyboard! If you love your Capslock key, you could still choose a different key. However, this post is primarily for the easy setup with the capslock key.

Setup Karabiner-Elements

In order to get started you will need to install Karabiner. If you have Homebrew installed, and if you don't, you should; then you can simply run

brew install --cask karabiner-elements

The other option is to head over to https://karabiner-elements.pqrs.org and download karabiner from there.

If you're interested, karabiner is open source, you can check out the project, or even sponsor it!

Complex Modifications

Once you have installed and started Karabiner, the next step is to download and add complex modifications. You can do this by going to the Complex Modifications tab and clicking Add Rule. Followed by clicking Import more rules from the Internet (open a web browser).

This will open up a browser tab to https://ke-complex-modifications.pqrs.org. Feel free to explore and try other modifications that sound interesting to you.

For the purpose of this guide, we are going to search for "Capslock Enhancement" by Vonng. Then click import, and allow it to open Karabiner.

Now you have imported your first complex modification!

Once you've imported complex modifications you can add the individual pieces as rules in Karabiner. When you click Add Rule now, you should have a subsection called "Capslock Enchancement".

If you want a more visual guide to this, check out the documentation for Karabiner Complex Rules.

Adding Rules

The first rule you will want to enable is CapsLock to Hyper/Escape. Once enabled, your Capslock key will now count as CMD + OPT + CTRL + SHIFT. Specifically the right side modifiers. This allows even further layering, by combining the hyper key with another left side modifier.

Another effect this rule adds is pressing and releasing the Capslock key sends <Esc> instead of the hyper combination.

You can find specifics for all these rules in the README.md on the modification's github https://github.com/Vonng/Capslock.

Next, we will want to add the Hyper Application rule as this is the base for application launching and switching. The default mapping for applications is as follows(stolen from GitHub):

Key\Mod
E Safari Finder Mail
R iTerm2 Preview Terminal
T Visual Studio Code Typora Note
Y Siri Karabiner Amphetamine
F Alfred 4 Dash Dictionary
G Intellij IDEA Chrome Calender

This means if you press Hyper(Capslock) + E, it will launch, and/or switch to your open Safari browser. Additionally, if you press Hyper(Capslock) + CMD + E, it will launch or focus the Finder. Pretty cool right?

In the next section, I will explain how to customize these applications.

Customizing Application Mappings

Customizing the application mappings aren't exactly user friendly, but let's walk through it. First you will want to open your Karabiner configuration file located at ~/.config/karabiner/karabiner.json. You can also find this file by going to the Misc tab in Karabiner and clicking Open config folder.

When you open karabiner.json you will be greeted by a large json file. You can find the beginning of the "Hyper Application" modifications by searching for "Hyper Application" and you will find something that looks like this:

...
{
	"description": "Hyper Application",
	"manipulators": [
	{
		"from": {
			"key_code": "e",
			"modifiers": {
				"mandatory": [
					"left_command",
					"right_command",
					"right_control",
					"right_shift",
					"right_option"
				]
			}
		},
		"to": [
			{
				"shell_command": "open -a 'finder'"
			}
		],
		"type": "basic"
	},
	...
	]
}
...

In the example above this is 1 mapping. Let's break this down a little.

The array of manipulators is all the individual application mappings. The from object contains the key, and modifiers needed to trigger the action. The to object describes what should happen when the conditions in from are met. In the example, it will execute a shell command.

It is important to remember at this point that our Hyper key(Capslock) counts as "right_command" + "right_control" + "right_shift" + "right_option"

Using the example above, we can see that this mapping requires us to press Hyper(Capslock) + CMD + E to run the shell command open -a 'finder'". This launches or switches to an active Finder window. Now there is no need to CMD + TAB all over, or use spotlight and type "Finder".

To customize this, we can change the key_code and/or the shell_command. For example if we wanted Hyper(Capslock) + CMD + E to launch or switch to Slack, we just replace 'finder' with 'Slack'. Then, if you want Slack to be Hyper(Capslock) + CMD + S then change key_code to "s". Keeping in mind the keys that other rules you've enabled might already be using, so they don't end up clashing.

That's all there is to it! Last thing to mention is if you disable, then re-enable the "Hyper Application" rule, it will overwrite your changes and set them back to the defaults. If you want to prevent this, you can modify the full Capslock Enhancement modification's json located at: karabiner/assets/complex_modifications/

Demo

Here is a quick demo of me using these mappings to switch applications and do random things.

Conclusion

Karabiner-Elements is a powerful tool and it is not the purpose of this post to cover everything it can do. This was just to get you started and specifically using it for application window management. If you are wanting learn more, the Documentation is quite extensive.

Hopefully you find some value in this guide, and save some time by not having to spotlight or CMD + TAB to your most used applications. As always If you found this helpful or have any questions feel free to dm, or tweet @natsirtremraf.

tristanfarmer.dev by Tristan Farmer