Xcode自定义键绑定命令完整列表及官方API咨询
Hey there! Let's break down what you need to build that handy { } shortcut and unlock more complex key bindings.
1. How to Get the Complete List of Usable Commands
Check the System's Default DETextKeyBindingSet.plist First
You can directly inspect the system file to see pre-defined bindings using Terminal:
plutil -p /System/Library/Frameworks/AppKit.framework/Versions/Current/Resources/DETextKeyBindingSet.plist
Or navigate to that path manually (right-click AppKit.framework > "Show Package Contents" to get into Resources) and open the plist with a text editor. But heads up—this only shows a subset of available commands. The real full list comes from the underlying AppKit framework.
Dig Into NSTextView's Methods for All Possible Commands
Every command you can use in key bindings is actually an Objective-C selector (method name) from NSTextView (and its subclasses, which power almost all text input fields on Mac). Any NSTextView method that takes no arguments or a single id parameter works for key bindings.
To see the full list, look up the NSTextView class reference—you'll find tons of useful commands beyond the ones you've already spotted, like:
moveToEndOfLine:selectToBeginningOfParagraph:pasteAsPlainText:deleteBackward:insertTab:
2. Official API Context
Apple doesn't have a standalone API doc just for text key bindings, but these commands are rooted in AppKit's Text System. The official resources you want are:
- The
NSTextViewclass reference: This is the source of all your bindable commands—every method listed here that fits the signature is fair game. - Apple's Text System Programming Guide: It explains how Mac's text handling works under the hood, including how key bindings map to these methods.
3. Example: Build Your { } Shortcut
To make a shortcut that inserts { } and drops the cursor right in the middle, edit your custom key bindings file (usually ~/Library/KeyBindings/DefaultKeyBinding.dict) and add this:
"^$[" = ( insertText:"{", insertText:"}", moveBackward:, );
^$[is the shortcut forCtrl+Shift+[—swap this for any combo you prefer.- The array runs commands in order: insert
{, insert}, then callmoveBackward:to jump the cursor between them.
For something fancier, like wrapping the current word in brackets, try this:
"^$]" = ( selectWord:, insertText:"{", yank:, insertText:"}", moveBackward:, );
内容的提问来源于stack exchange,提问作者Charles Bentley

