Chapter 4. Customization and Theme Development

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:

  • Overriding and maintaining key bindings
  • Understanding Sublime's base settings
  • Beautifying Sublime with colors and themes
  • Mastering Split Windows

Overriding and maintaining key bindings

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.

Platform-specific key bindings

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.

Key map file structure

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.

Bindable keys

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:

Up

down

right

left

insert

browser_back

home

end

pageup

pagedown

backspace

browser_forward

delete

tab

enter

pause

escape

browser_refresh

space

keypad0

keypad1

keypad2

keypad3

browser_search

keypad4

keypad5

keypad6

keypad7

keypad8

browser_stop

keypad9

clear

f1

f2

f3

browser_home

f4

f5

f6

f7

f8

browser_favorites

f9

f10

f11

f12

f13

keypad_period

f14

f15

f16

f17

f18

keypad_divide

f19

f20

sysreq

break

shift

keypad_multiply

ctrl

alt

super

context_menu

keypad_plus

keypad_minus

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.

Advanced key bindings

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.

Keeping our key bindings organized

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.

Tip

Don't be afraid to read the preceding information twice. It's important to know how Sublime handles key bindings.

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.

Keeping our key bindings organized

After installing it, we simply need to press Shift + F10 to get a full, detailed list of all the BoundKeys and conflicts, if any.

Tip

The FindKeyConflicts plugin is also recommended, and can be found at https://github.com/skuroda/FindKeyConflicts.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset