Mac: Create Custom Keyboard Layout
Create Custom Keyboard Layout
MacOS has a system-wide way to let you define or modify shortcut keys for text editing operations.
You can create keys to:
- Insert math symbols, emoji, or any template text.
- Move cursor by word, page up/down.
- Copy, cut, paste, undo, redo.
- select word, line, paragraph, select all.
- upcase word, lowercase word.
- new, open, save, etc.
- and more.
Key Config File
Create a file at
~/Library/KeyBindings/DefaultKeyBinding.dict
Create the “KeyBindings” folder if you don't already have it.
Copy and past the following:
/* -*- coding: utf-8 -*- */
{
/* insert Unicode character with Option key down */
"~h" = ("insertText:", "♥");
"~t" = ("insertText:", "😄");
"~c" = ("insertText:", "😠");
"~s" = ("insertText:", "😮");
/* move cursor with i j k l keys while Ctrl key down */
"^i" = ("moveUp:");
"^k" = ("moveDown:");
"^j" = ("moveLeft:");
"^l" = ("moveRight:");
}
Note: if you use Unicode Characters such as ♥ directly in the file , you must save the file using Unicode: UTF-8 Encoding . See Set Text Editor File Encoding .
Launch or restart TextEdit or other app to start to use your changes.
The syntax in general is this:
/* this is comment */
{
keycode1 = actionCode1;
keycode2 = actionCode2;
}
- keycode is a string that represents key press.
- actionCode represents what to do.
Keycode Syntax
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 is example file for inserting Unicode characters with the ⌥ option key.
/* -*- coding: utf-8 -*- */
{
/* insert Unicode characteres with Option key down */
"~#8" = ("insertText:", "↑");
"~#2" = ("insertText:", "↓");
"~#4" = ("insertText:", "←");
"~#6" = ("insertText:", "→");
"~a" = ("insertText:", "α");
"~h" = ("insertText:", "θ");
"~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:", " John\n http://example.com/\n\n☄");
}
Here is 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/Xcode.app/Contents/Resources/PBKeyBinding.dict.
You can view it here: osx_keybinding_xcode.dict.txt
.
Emacs Keybinding
Mac OS X by default support emacs keybindings. They are:
- Ctrl+f
- Move forward
- Ctrl+b
- Move backward
- Ctrl+n
- Move down a line
- Ctrl+p
- Move up a line
- Ctrl+a
- Beginning of line
- Ctrl+e
- End of line
- Ctrl+k
- Delete current position to end of line
- Ctrl+y
- Paste
You can add more of emacs's
- Ctrl+space
- Set mark
- Ctrl+w
- Cut
- Ctrl+x Ctrl+x
- Swap cursor position to last mark
However, i don't recommend it. Emacs's keys is very inefficient and ergonomically painful. See: Why Emacs Keyboard Shortcuts are Painful. If you like a efficient keybinding for text editing, try: Emacs: Xah Fly Keys 📦 .
Problems
Something this cannot do. For example:
- It cannot remap keys.
- It can't set a key such as F8 to type other keys such as ⌘ command+c.
- It can't set a key to launch a app or script.
There are many solutions to these. See: Mac: Keyboard Software Guide
Best is to get a programable keyboard. See: Programable Keyboards with Onboard Memory
See also: Problems of Mac OS X DefaultKeyBinding.dict .
Reference
- Text System Defaults and Key Bindings By Apple. @ https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/EventOverview/TextDefaultsBindings/TextDefaultsBindings.html
- Technical Note TN2056: Installable Keyboard Layouts @ https://developer.apple.com/library/mac/technotes/tn2056/_index.html