Mac OS X: How to Create Keyboard Layout and Keybinding ⌨

, , …,

This page tells you how to modify Mac OS X's keybinding.

You can:

mac unicode char
Mac OS X, 【⌥ Opt+key】 in Dvorak Keyboard Layout

Key Config File

Create a file at ~/Library/KeyBindings/DefaultKeyBinding.dict. Create the “KeyBindings” dir if you don't already have it.

The DefaultKeyBinding.dict file is just a text file.

The file's content should be key & action pairs, like this:

/* this is comment */
"keycode1" = actionCode1;
"keycode2" = actionCode2;

Keycode is a string that represent key press.

actionCode represents what to do.

(if you have Unicode Characters in the file such as , you must save it in UTF-8 encoding.)

Simple Example

Create a file at ~/Library/KeyBindings/DefaultKeyBinding.dict, and put the following content.

/* my keybindings */

/* insert Unicode character with Option key down*/
"~a" = ("insertText:", "\U03B1"); /* greek alpha */
"~;" = ("insertText:", "\U2665"); /* heart symbol */

/* move cursor with i j k l keys while Ctrl key down */
"^i" = ("moveUp:");
"^k" = ("moveDown:");
"^j" = ("moveLeft:");
"^l" = ("moveRight:");

Restart a Cocoa application and the new keybinding will take effect in that application. Launch TextEdit to test your changes.

Only applications that uses Cocoa Text System will support this.

Key Syntax

You can define keys with any of {⌘ Cmd, ⌥ Opt, Ctrl, ⇧ Shift} keys held down, or any combination of them, or single keys such as {b, F2}.

See: Mac OS X Keybinding Key Syntax.

Action Code

Action can be inserting a character, a string, moving the cursor by character, word, line, or page up/down, or open file, create new file, ….

See: Mac OS X Keybinding Action Code.

Sample File

Example of defining the {↖ Home, ↘ End} keys to move to the beginning/end of line.

/* make home/end key to move to begin/end of line */

"\UF729" = "moveToBeginningOfLine:"; /* home key */
"\UF72B" = "moveToEndOfLine:"; /* end key */

Here's example file for inserting Unicode characters with the ⌥ Opt key.

/* -*- coding: utf-8 -*- */

/* insert Unicode characteres with Option key down */
"~#8" = ("insertText:", "\U2191"); /* ↑ */
"~#2" = ("insertText:", "\U2193"); /* ↓ */
"~#4" = ("insertText:", "\U2190"); /* ← */
"~#6" = ("insertText:", "\U2192"); /* → */
"~#9" = ("insertText:", "\U2197"); /* ↗ */

"~a" = ("insertText:", "\U03B1"); /* α */
"~h" = ("insertText:", "\U03B8"); /* θ */

"~3" = ("insertText:", "†");
"~7" = ("insertText:", "—");
"~8" = ("insertText:", "•");
"~9" = ("insertText:", "★");

"~&" = ("insertText:", "‣");
"~*" = ("insertText:", "°");

/* insert pairs with Option down */
"~d" = ("insertText:", "«»", "moveBackward:");
"~h" = ("insertText:", "{}", "moveBackward:");
"~t" = ("insertText:", "()", "moveBackward:");
"~n" = ("insertText:", "[]");
"~s" = ("insertText:", "“”", "moveBackward:");
"~-" = ("insertText:", "「」", "moveBackward:");

"~D" = ("insertText:", "‹›", "moveBackward:");
"~S" = ("insertText:", "‘’", "moveBackward:");
"~_" = ("insertText:", "『』", "moveBackward:");

/* insert sig */
"~1" = ("insertText:", "  Xah\n∑\n\n☄");

Here's a example of ErgoEmacs Keybinding, one for QWERTY layout and one for Dvorak Keyboard Layout:

You can look at Xcode's keybinding file at /Developer/Applications/ You can view it here: osx_keybinding_xcode.dict.txt.

Emacs Keybinding

Mac OS X by default support emacs' keybindings. They are:

Mac OS X's default emacs keybindings
Ctrl+fmove forward
Ctrl+bmove backward
Ctrl+nmove down a line
Ctrl+pmove up a line
Ctrl+abeginning of line
Ctrl+eend of line
Ctrl+kdelete current position to end of line

You can add more of emacs's

Ctrl+fmove forward
Ctrl+spaceset mark
Ctrl+x Ctrl+xSwap cursor position to last mark

However, i don't recommend it. Emacs's keys is very inefficient and ergonomically painful. See: Why Emacs's Keyboard Shortcuts are Painful. If you like a efficient keybinding for text editing, you might try: ErgoEmacs Keybinding.


See: Problems of Mac OS X's Keybinding Scheme DefaultKeyBinding.dict.

References and Resources

Here are some references and related sites.

Note: the default keybinding config file used by the system is a XML file at: /System/Library/Frameworks/AppKit.framework/Resources/StandardKeyBinding.dict. You don't have to do anything with that file. This is for your information only.

See also: Mac OS X Keymapping Keybinding Tools.

blog comments powered by Disqus