This chapter will give us the ability to fully customize our Sublime Text's look and feel. We will also customize our own color theme. And as a bonus, we will check out the Split Windows feature.
In this chapter we will cover the following topics:
In the previous chapter, we bound as key combination to our macro. Sublime also gives us the option to bind keys to any command by adding custom key bindings; but what are key bindings exactly? In one sentence:
"Key bindings map key presses to commands."
All of Sublime's key bindings are configurable by JSON-formatted .sublime-keymap
files.
Key bindings can be different per platform; their filename has to be one of the following:
Default (Windows).sublime-keymap
Default (OSX).sublime-keymap
Default (Linux).sublime-keymap
These file names are platform dependent; this means that the key bindings defined in the Windows keymap
file will only work if we are on Windows or other OSes. It is important to know that user-specified key bindings need to be placed in Packages/User/Default (<platform>).sublime-keymap
.
A key map is an array of key bindings. Each key binding contains the following elements:
keys
: This contains an array of case-sensitive keys that needs to be pressed to trigger the key binding. We can make chords by using an array, for example, ["ctrl+k", "ctrl+b"]
.command
: This contains the command to be executed.args
(optional): This contains a dictionary of parameters to be passed to the command
element.context
(optional): This contains an array of contexts that will enable the key binding. All contexts must be true
for the key binding to be enabled.Here's an example from the Windows default key map:
[ { "keys": ["ctrl+n"], "command": "new_file" }, { "keys": ["ctrl+shift+n"], "command": "new_window" } ]
The first key binding will open a new tab when Ctrl + N is pressed, and the second key binding will open a new window when Ctrl + Shift + N is pressed.
For a list of all available commands, visit either http://gist.github.com/danpe/7189451 or http://docs.sublimetext.info/en/sublime-text-3/reference/commands.html.
Sublime supports almost all keyboard keys as bindable keys. Here is the full list of all the keyboard keys that can be used with key bindings:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
We have some restrictions though. On Windows, we should not use Ctrl + Alt + an alphanumeric key, while on OS X, we should not use Option + an alphanumeric key Number keys cannot be bound. For example, we cannot use Ctrl+7.
Simple key bindings include only BoundKeys and a command. However, we can also make more advanced key bindings by passing arguments to the command using the args
key; for example:
{ "keys": ["enter"], "command": "insert", "args": {"characters": " "} }
In this key binding, we pass
to the insert
command when we press Enter. More advanced key bindings can be achieved using contexts. A context determines if the command will be executed based on the caret's position or some other state. For example:
{ "keys": ["escape"], "command": "hide_auto_complete", "context": [ { "key": "auto_complete_visible", "operator": "equal", "operand": true } ] }
This key binding will hide autocomplete when Esc is pressed, but only if autocomplete is visible; if not, this command won't get triggered.
For a list of all available contexts, visit either http://gist.github.com/danpe/7189722 or http://docs.sublimetext.info/en/sublime-text-3/reference/key_bindings.html.
A big problem is that Sublime keeps track of all the key bindings we have. So first, let's understand how Sublime knows when a key binding needs to override another key binding.
Sublime will start loading all the key bindings located in Packages/Default
; then, it will sort all the installed packages in an alphabetic order and load them one after another. The last one to be loaded will always be Packages/User
. Each keymap
file that is being loaded will override any other key bindings that have been loaded before it in case of a key conflict. This means that Packages/User
will override all the key bindings because it is being loaded last.
Lucky for us, we have an awesome plugin that can help us manage our key bindings and detect collisions and conflict. It is called BoundKey, and can be installed using our favorite Package Control! Let's open up the command palette by pressing Ctrl + Shift + P in Windows or Linux and Command + Shift + P in OS X. Then, we'll choose Install Package and install the BoundKeys plugin.
After installing it, we simply need to press Shift + F10 to get a full, detailed list of all the BoundKeys and conflicts, if any.
The FindKeyConflicts plugin is also recommended, and can be found at https://github.com/skuroda/FindKeyConflicts.