I've been using Karabiner Elements as my Mac's key remapping tool. My personal preference is to swap left-command and left-control, which works well for most applications. However, there are some applications where I don't want this swap to happen. This is where Karabiner Elements' advanced rules come in.
The Problem
Simple key remapping works globally across all applications. But what if you need:
- Different mappings for specific applications
- Exclude certain apps from your remapping rules
- Complex conditional key behaviors
Karabiner Elements solves this with Complex Modifications - JSON configuration files that define advanced remapping rules with conditions.
Creating Complex Modifications
You can use the online editor Karabiner Complex Modification Generator to create complex configurations. Here's an example that swaps left-command and left-control, but excludes specific applications:
{
"title": "basic key-remapping",
"rules": [
{
"description": "Exchange Left-Command Left-Control",
"manipulators": [
{
"type": "basic",
"from": {
"key_code": "left_gui"
},
"conditions": [
{
"type": "frontmost_application_unless",
"bundle_identifiers": [
"^com\\.adobe\\.AfterEffects{{CONTENT}}quot;
]
}
],
"to": [
{
"repeat": true,
"modifiers": [],
"key_code": "left_control"
}
]
},
{
"type": "basic",
"from": {
"key_code": "left_control"
},
"conditions": [
{
"type": "frontmost_application_unless",
"bundle_identifiers": [
"^com\\.adobe\\.AfterEffects{{CONTENT}}quot;
]
}
],
"to": [
{
"repeat": true,
"modifiers": [],
"key_code": "left_gui"
}
]
}
]
}
]
}Understanding the Configuration
Key elements of this configuration:
from.key_code: The key to remap (left_guiis Command,left_controlis Control)conditions: When to apply the rulefrontmost_application_unless: Apply everywhere EXCEPT the listed appsbundle_identifiers: Regex patterns matching app bundle IDsto.key_code: The key to map to
Finding an App's Bundle Identifier
To get any application's bundle identifier, use this script:
osascript -e 'id of app "After Effects"'
# Output: com.adobe.AfterEffectsSome common bundle identifiers:
| Application | Bundle Identifier |
|---|---|
| After Effects | com.adobe.AfterEffects |
| Premiere Pro | com.adobe.PremierePro |
| Terminal | com.apple.Terminal |
| VS Code | com.microsoft.VSCode |
Installing the Configuration
- Save your JSON file with a
.jsonextension - Place it in
~/.config/karabiner/assets/complex_modifications/ - Open Karabiner Elements Preferences
- Go to "Complex Modifications" tab
- Click "Add rule" and enable your new rule
Alternatively, you can import configurations directly from the online editor or paste your JSON into the Karabiner Elements preferences.
Other Useful Condition Types
frontmost_application_if: Only apply to specific appsdevice_if: Only apply to specific keyboardskeyboard_type_if: Based on keyboard type (ANSI, ISO, JIS)variable_if: Based on custom variables (for modal behavior)