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:
keysAn 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.commandName of the command to be executed.
argsDictionary of arguments to be passed to
command. Keys must be names of parameters tocommand.contextArray of conditions that determine a particular context. All conditions must evaluate to
truefor 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
keyName of the context whose value you want to query.
operatorType of test to perform against
key's value. Defaults toequal.operandThe result returned by
keyis tested against this value.match_allRequires 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_visibleReturns
trueif the autocomplete list is visible.eol_selectSelector to match scope name at end of current line
following_textTest against the selected text and the text following it until the end of the line.
group_has_multiselectReturns
trueif group has multi-selecthas_next_fieldReturns
trueif a next snippet field is available.has_prev_fieldReturns
trueif a previous snippet field is available.is_javadocReturns
trueif caret(s) is (are) in a comment that starts with/**is_recording_macroIs user currently recording a macro?
last_commandReturns the name of the last command run.
last_modifying_commandName of last command run that modified a buffer
num_selectionsReturns the number of selections.
overlay_has_focusReturns
trueif any overlay has focus.overlay_visibleReturns
trueif any overlay is visible.panelReturns
trueif the panel given asoperandis visible.panel_has_focusReturns
trueif a panel has input focus.panel_visibleReturns
trueif any panel is visible.popup_visibleIs a popup currently being displayed?
preceding_textTest against the text on the line up to and including the selection.
read_onlyIs buffer in read-only state?
selection_emptyReturns
trueif the selection is an empty region.selectorName of scope for current selection.
setting.xReturns the value of the
xsetting.xcan be any string.textRestricts the test to the selected text.
Context Operators
equal,not_equalTest for equality.
regex_match,not_regex_matchMatch against a regular expression (full match).
regex_contains,not_regex_containsMatch against a regular expression (partial match).
Bindable Keys
Keys in key bindings may be specified literally by symbol or by a name for a special key. Symbols cannot be combined with modifiers. For example, B will catch any key sequence inserting a B glyph, but ctrl+B is invalid and needs to be written as ctrl+shift+b instead.
Here's the list of the names for special keys:
| Keys | ||
|---|---|---|
up | keypad0 | f1 |
down | keypad1 | f2 |
right | keypad2 | f3 |
left | keypad3 | f4 |
insert | keypad4 | f5 |
home | keypad5 | f6 |
end | keypad6 | f7 |
pageup | keypad7 | f8 |
pagedown | keypad8 | f9 |
backspace | keypad9 | f10 |
delete | keypad_period | f11 |
tab | keypad_divide | f12 |
enter | keypad_multiply | f13 |
pause | keypad_minus | f14 |
escape | keypad_plus | f15 |
space | keypad_enter | f16 |
clear | f17 | |
sysreq | browser_back | f18 |
break | browser_forward | f19 |
context_menu | browser_refresh | f20 |
browser_stop | f21 | |
browser_search | f22 | |
browser_favorites | f23 | |
browser_home | f24 |
Modifiers
shiftctrlorcontrolaltsuper(Windows: Windows key, MacOS: Command Key)primary(Windows: Control key, MacOS: Command Key)command(MacOS only)option(MacOS only: same asalt)
The Any Character Binding
Adding a binding for <character> (with the angled brackets and no modifiers) causes Sublime Text to bind the given command for all glyphs provided to it. You should thus only use this binding with an accompanying context filter.
The specified command will then receive an additional character argument containing the glyph that was captured.
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 macOS 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.
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.
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 Also
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.
Troubleshooting
To enable logging related to keymaps, see the documentation for:
- sublime.log_commands(flag)
- sublime.log_input(flag)
These may help with debugging keymaps. When a key chord does not trigger an input log, another application or your operating system is likely grabbing the key before it can reach Sublime Text.