Key Bindings¶
Key bindings map key presses to commands.
File Format¶
Key bindings are stored in .sublime-keymap files
and defined in JSON.
Keymap files may be located anywhere in a package.
Naming Keymap Files¶
Any keymap named Default.sublime-keymap
will always be applied in all platforms.
Additionally, each platform can optionally have its own keymap:
Default (Windows).sublime-keymapDefault (OSX).sublime-keymapDefault (Linux).sublime-keymap
Sublime Text will ignore any .sublime-keymap file
whose name doesn’t follow the patterns just described.
Structure of a Key Binding¶
Keymaps are arrays of key bindings. These are all valid elements in a key binding:
keys- An array of case-sensitive keys.
Modifiers can be specified
with the
+sign. You can build chords by adding elements to the array (for example,["ctrl+k","ctrl+j"]). Ambiguous chords are resolved with a timeout. command- Name of the command to be executed.
args- Dictionary of arguments
to be passed to
command. Keys must be names of parameters tocommand. context- Array of conditions that determine a particular context. All conditions must evaluate to true for the context to be active. See Structure of a Context below for more information.
Here’s an example:
{ "keys": ["shift+enter"], "command": "insert_snippet", "args": {"contents": "\n\t$0\n"}, "context":
[
{ "key": "setting.auto_indent", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\{$", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\}", "match_all": true }
]
}
Structure of a Context¶
key- Name of the context whose value you want to query.
operator- Type of test to perform against
key’s value. Defaults toequal. operand- The result returned by
keyis tested against this value. match_all- Requires the test to succeed
for all selections.
Defaults to
false.
Context Keys¶
Arbitrary keys may be provided by plugins. Thus, this section only features keys provided by Sublime Text itself.
auto_complete_visible- Returns
trueif the autocomplete list is visible. has_next_field- Returns
trueif a next snippet field is available. has_prev_field- Returns
trueif a previous snippet field is available. num_selections- Returns the number of selections.
overlay_visible- Returns
trueif any overlay is visible. panel_visible- Returns
trueif any panel is visible. following_text- Test against the selected text and the text following it until the end of the line.
preceding_text- Test against the text on the line up to and including the selection.
selection_empty- Returns
trueif the selection is an empty region. setting.x- Returns the value of the
xsetting.xcan be any string. text- Restricts the test to the selected text.
selector- Returns the name of the current scope.
panel_has_focus- Returns
trueif a panel has input focus. panel- Returns
trueif the panel given asoperandis visible.
Context Operators¶
equal,not_equal- Test for equality.
regex_match,not_regex_match- Match against a regular expression (full match).
regex_contains,not_regex_contains- Match against a regular expression (partial match).
Command Mode¶
Sublime Text provides a command_mode setting
to prevent key presses
from being sent to the buffer.
This is useful, for example,
to emulate Vim’s modal behavior.
Key bindings not intended for command mode (generally, all of them) should include a context like this:
{"key": "setting.command_mode", "operand": false}
This way, plugins legitimately using command mode will be able to define appropriate key bindings without interference.
Bindable Keys¶
Keys in key bindings may be specified literally or by name. If using a name doesn’t work in your case, try a literal value.
Here’s the list of all valid names:
updownrightleftinserthomeendpageuppagedownbackspacedeletetabenterpauseescapespacekeypad0keypad1keypad2keypad3keypad4keypad5keypad6keypad7keypad8keypad9keypad_periodkeypad_dividekeypad_multiplykeypad_minuskeypad_pluskeypad_enterclearf1f2f3f4f5f6f7f8f9f10f11f12f13f14f15f16f17f18f19f20sysreqbreakcontext_menubrowser_backbrowser_forwardbrowser_refreshbrowser_stopbrowser_searchbrowser_favoritesbrowser_home
Modifiers¶
shiftctrlaltsuper(Windows key, Command key…)
Warning about Bindable Keys¶
If you’re developing a package, keep this in mind:
Ctrl+Alt+<alphanum>should never be used in any Windows key bindings.Option+<alphanum>should never be used in any OS X key bindings.
In both cases, the user’s ability to insert non-ASCII characters would be compromised otherwise.
End-users are free to remap any key combination.
Order of Preference for Key Bindings¶
Key bindings in a keymap file are evaluated from the bottom to the top. The first matching context wins.
Keeping Keymaps Organized¶
Sublime Text ships with default keymaps
under Packages/Default.
Other packages may include
keymap files of their own.
The recommended storage location
for your personal keymap files is Packages/User.
See Merging and Order of Precedence for more information.
International Keyboards¶
Due to the way Sublime Text maps key names to physical keys, key names may not correspond to physical keys in keyboard layouts other than US English.