ctrl+shift+p filters: :st2 :st3 :win :osx :linux
Browse

Programmatic Key Bindings

by VonHeikemen ST3

Declare your keybinding in sublime text using Python

Labels keymap

Details

Installs

  • Total 745
  • Win 624
  • Mac 83
  • Linux 38
Apr 3 Apr 2 Apr 1 Mar 31 Mar 30 Mar 29 Mar 28 Mar 27 Mar 26 Mar 25 Mar 24 Mar 23 Mar 22 Mar 21 Mar 20 Mar 19 Mar 18 Mar 17 Mar 16 Mar 15 Mar 14 Mar 13 Mar 12 Mar 11 Mar 10 Mar 9 Mar 8 Mar 7 Mar 6 Mar 5 Mar 4 Mar 3 Mar 2 Mar 1 Feb 28 Feb 27 Feb 26 Feb 25 Feb 24 Feb 23 Feb 22 Feb 21 Feb 20 Feb 19 Feb 18
Windows 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Mac 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Linux 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
01234Apr 3Mar 31Mar 28Mar 25Mar 22Mar 19Mar 16Mar 13Mar 10Mar 7Mar 4Mar 1Feb 26Feb 23Feb 20Apr 3 Linux: 0 Mac: 0 Windows: 0Apr 2 Linux: 0 Mac: 0 Windows: 0Apr 1 Linux: 0 Mac: 1 Windows: 0Mar 31 Linux: 0 Mac: 0 Windows: 0Mar 30 Linux: 0 Mac: 0 Windows: 0Mar 29 Linux: 0 Mac: 0 Windows: 0Mar 28 Linux: 0 Mac: 0 Windows: 0Mar 27 Linux: 0 Mac: 0 Windows: 0Mar 26 Linux: 0 Mac: 0 Windows: 0Mar 25 Linux: 0 Mac: 0 Windows: 1Mar 24 Linux: 0 Mac: 0 Windows: 0Mar 23 Linux: 0 Mac: 0 Windows: 0Mar 22 Linux: 0 Mac: 0 Windows: 0Mar 21 Linux: 0 Mac: 0 Windows: 0Mar 20 Linux: 0 Mac: 0 Windows: 0Mar 19 Linux: 0 Mac: 0 Windows: 0Mar 18 Linux: 0 Mac: 0 Windows: 0Mar 17 Linux: 1 Mac: 0 Windows: 0Mar 16 Linux: 0 Mac: 0 Windows: 0Mar 15 Linux: 0 Mac: 0 Windows: 0Mar 14 Linux: 0 Mac: 0 Windows: 0Mar 13 Linux: 0 Mac: 0 Windows: 0Mar 12 Linux: 0 Mac: 0 Windows: 0Mar 11 Linux: 0 Mac: 0 Windows: 0Mar 10 Linux: 0 Mac: 1 Windows: 0Mar 9 Linux: 0 Mac: 0 Windows: 0Mar 8 Linux: 0 Mac: 0 Windows: 0Mar 7 Linux: 0 Mac: 0 Windows: 0Mar 6 Linux: 0 Mac: 0 Windows: 0Mar 5 Linux: 0 Mac: 0 Windows: 0Mar 4 Linux: 0 Mac: 0 Windows: 0Mar 3 Linux: 0 Mac: 0 Windows: 0Mar 2 Linux: 0 Mac: 0 Windows: 0Mar 1 Linux: 0 Mac: 0 Windows: 0Feb 28 Linux: 0 Mac: 0 Windows: 0Feb 27 Linux: 0 Mac: 0 Windows: 0Feb 26 Linux: 0 Mac: 0 Windows: 0Feb 25 Linux: 0 Mac: 0 Windows: 0Feb 24 Linux: 0 Mac: 0 Windows: 0Feb 23 Linux: 0 Mac: 0 Windows: 0Feb 22 Linux: 0 Mac: 0 Windows: 0Feb 21 Linux: 0 Mac: 0 Windows: 0Feb 20 Linux: 0 Mac: 0 Windows: 0Feb 19 Linux: 0 Mac: 0 Windows: 0Feb 18 Linux: 0 Mac: 0 Windows: 0Apr 3 Linux: 0 Mac: 0 Windows: 0Apr 2 Linux: 0 Mac: 0 Windows: 0Apr 1 Linux: 0 Mac: 1 Windows: 0Mar 31 Linux: 0 Mac: 0 Windows: 0Mar 30 Linux: 0 Mac: 0 Windows: 0Mar 29 Linux: 0 Mac: 0 Windows: 0Mar 28 Linux: 0 Mac: 0 Windows: 0Mar 27 Linux: 0 Mac: 0 Windows: 0Mar 26 Linux: 0 Mac: 0 Windows: 0Mar 25 Linux: 0 Mac: 0 Windows: 1Mar 24 Linux: 0 Mac: 0 Windows: 0Mar 23 Linux: 0 Mac: 0 Windows: 0Mar 22 Linux: 0 Mac: 0 Windows: 0Mar 21 Linux: 0 Mac: 0 Windows: 0Mar 20 Linux: 0 Mac: 0 Windows: 0Mar 19 Linux: 0 Mac: 0 Windows: 0Mar 18 Linux: 0 Mac: 0 Windows: 0Mar 17 Linux: 1 Mac: 0 Windows: 0Mar 16 Linux: 0 Mac: 0 Windows: 0Mar 15 Linux: 0 Mac: 0 Windows: 0Mar 14 Linux: 0 Mac: 0 Windows: 0Mar 13 Linux: 0 Mac: 0 Windows: 0Mar 12 Linux: 0 Mac: 0 Windows: 0Mar 11 Linux: 0 Mac: 0 Windows: 0Mar 10 Linux: 0 Mac: 1 Windows: 0Mar 9 Linux: 0 Mac: 0 Windows: 0Mar 8 Linux: 0 Mac: 0 Windows: 0Mar 7 Linux: 0 Mac: 0 Windows: 0Mar 6 Linux: 0 Mac: 0 Windows: 0Mar 5 Linux: 0 Mac: 0 Windows: 0Mar 4 Linux: 0 Mac: 0 Windows: 0Mar 3 Linux: 0 Mac: 0 Windows: 0Mar 2 Linux: 0 Mac: 0 Windows: 0Mar 1 Linux: 0 Mac: 0 Windows: 0Feb 28 Linux: 0 Mac: 0 Windows: 0Feb 27 Linux: 0 Mac: 0 Windows: 0Feb 26 Linux: 0 Mac: 0 Windows: 0Feb 25 Linux: 0 Mac: 0 Windows: 0Feb 24 Linux: 0 Mac: 0 Windows: 0Feb 23 Linux: 0 Mac: 0 Windows: 0Feb 22 Linux: 0 Mac: 0 Windows: 0Feb 21 Linux: 0 Mac: 0 Windows: 0Feb 20 Linux: 0 Mac: 0 Windows: 0Feb 19 Linux: 0 Mac: 0 Windows: 0Feb 18 Linux: 0 Mac: 0 Windows: 0Apr 3 Linux: 0 Mac: 0 Windows: 0Apr 2 Linux: 0 Mac: 0 Windows: 0Apr 1 Linux: 0 Mac: 1 Windows: 0Mar 31 Linux: 0 Mac: 0 Windows: 0Mar 30 Linux: 0 Mac: 0 Windows: 0Mar 29 Linux: 0 Mac: 0 Windows: 0Mar 28 Linux: 0 Mac: 0 Windows: 0Mar 27 Linux: 0 Mac: 0 Windows: 0Mar 26 Linux: 0 Mac: 0 Windows: 0Mar 25 Linux: 0 Mac: 0 Windows: 1Mar 24 Linux: 0 Mac: 0 Windows: 0Mar 23 Linux: 0 Mac: 0 Windows: 0Mar 22 Linux: 0 Mac: 0 Windows: 0Mar 21 Linux: 0 Mac: 0 Windows: 0Mar 20 Linux: 0 Mac: 0 Windows: 0Mar 19 Linux: 0 Mac: 0 Windows: 0Mar 18 Linux: 0 Mac: 0 Windows: 0Mar 17 Linux: 1 Mac: 0 Windows: 0Mar 16 Linux: 0 Mac: 0 Windows: 0Mar 15 Linux: 0 Mac: 0 Windows: 0Mar 14 Linux: 0 Mac: 0 Windows: 0Mar 13 Linux: 0 Mac: 0 Windows: 0Mar 12 Linux: 0 Mac: 0 Windows: 0Mar 11 Linux: 0 Mac: 0 Windows: 0Mar 10 Linux: 0 Mac: 1 Windows: 0Mar 9 Linux: 0 Mac: 0 Windows: 0Mar 8 Linux: 0 Mac: 0 Windows: 0Mar 7 Linux: 0 Mac: 0 Windows: 0Mar 6 Linux: 0 Mac: 0 Windows: 0Mar 5 Linux: 0 Mac: 0 Windows: 0Mar 4 Linux: 0 Mac: 0 Windows: 0Mar 3 Linux: 0 Mac: 0 Windows: 0Mar 2 Linux: 0 Mac: 0 Windows: 0Mar 1 Linux: 0 Mac: 0 Windows: 0Feb 28 Linux: 0 Mac: 0 Windows: 0Feb 27 Linux: 0 Mac: 0 Windows: 0Feb 26 Linux: 0 Mac: 0 Windows: 0Feb 25 Linux: 0 Mac: 0 Windows: 0Feb 24 Linux: 0 Mac: 0 Windows: 0Feb 23 Linux: 0 Mac: 0 Windows: 0Feb 22 Linux: 0 Mac: 0 Windows: 0Feb 21 Linux: 0 Mac: 0 Windows: 0Feb 20 Linux: 0 Mac: 0 Windows: 0Feb 19 Linux: 0 Mac: 0 Windows: 0Feb 18 Linux: 0 Mac: 0 Windows: 0

Readme

Source
raw.​githubusercontent.​com

Sublime Text Programmatic Key Bindings

Use the full power of python to declare your key bindings.

Just imaging writing your bindings with this api:

prefix_origami = "ctrl+w"

def keybinding(bind, **kwargs):
  command = kwargs.get('command')

  # Move between selections in overlay
  bind(["alt+k"], "move", overlay_visible, by="lines", forward=False)
  bind(["alt+j"], "move", overlay_visible, by="lines", forward=True)

  # Safe quit
  bind(["ctrl+q"], [command("close_workspace"), command("exit")])

  # Plugin: Origami
  bind([prefix_origami], "noop") # Disable the default behavior of ctrl+w
  bind([prefix_origami, "q"], "close")
  bind([prefix_origami, "c"], "destroy_pane", direction="self")



# Contexts
overlay_visible = {
  "key": "overlay_visible",
  "operator": "equal",
  "operand": True
}

Getting Started

Installation

Recommended

Install Programmatic Key Bindings via Package Control.

  1. Open the Command Palette via Ctrl/+Shift+p
  2. Then select Package Control: Install Package
  3. Search for Programmatic Key Bindings and press ↲ Enter

Manual

  1. Clone or download this repository, (re)name the folder to Programmatic Key Bindings if necessary.
  2. Move the folder inside your sublime /Packages. (Preferences > Browse Packages…)

Usage

Using the default command

This plugin only adds one command to sublime text:

[
  {
    "caption": "Sublime Programmatic Key Bindings - Compile Default",
    "command": "spk_key_binding",
    "args": {
      "bindings": "$packages/User/Programmatic Key Bindings/keybindings.py",
      "destination": "$packages/User/Programmatic Key Bindings/Default ($platform).sublime-keymap"
    }
  }
]

Make sure the file that bindings is pointing to exists and also that the destination can be created in the directory. In here $packages refers to the directory where your sublime text packages live.

Once you have created the file create a function called keybinding. This function will be called with two arguments. The first argument is the function gathers the data of your keybindings. The second is a helper function which should be used only when you need to bind multiple commands to a key.

def keybinding(bind, **kwargs):
  command = kwargs.get('command')

  # Your code...

After this function is executed a .sublime-keymap will be created in the directory Programmatic Key Bindings located in your “user folder”. Sublime text will pick up any changes to that file and will reload your key bindings so the changes take effect (and you should be able to use then inmediately, reset sublime if you want to be sure).

That means, this plugin doesn't do anything at runtime. After the .sublime-keymap is created everything is in the hands of sublime's internal mechanism.

Anyway, once your ready, search the command Sublime Programmatic Key Bindings - Compile Default in the command palette and run it.

Create your own command

If you want to manage your primary keymap, the one in the root of your user folder, you need to create a new command.

First, create a file called Default.sublime-commands (in your user folder). In there you can your custom command.

[
  {
    "caption": "Update root key bindings",
    "command": "spk_key_binding",
    "args": {
      "bindings": "$packages/User/keybindings.py",
      "destination": "$packages/User/Default.sublime-keymap"
    }
  }
]

Now your ready. Make sure keybindings.py exists and has the code you need. Search in the command palette Update root key bindings, run it and enjoy.

User binding API

A.K.A keybindings.py, the only thing you need there is the keybinding function.

Function bind

bind(<keys>, <command>, ...[context], **kwargs)

The first argument is an array of keys you want to bind.

The second argument is the command you want to run. This could be a string or an array of commands. This plugin allows you to bind a sequence of commands if you pass an array, but since this isn't a built-in feature of sublime you need to use the helper function provided in kwargs, which is called command.

command(<name>, **kwargs)

command takes the name of the command as the first argument, and rest of the keyword arguments will be the arguments to the command you want to run.

Going back to bind. The first and second arguments are required, the rest of the positional arguments can be “context objects”.

If your command is not a sequence, the keyword arguments of bind will become the arguments to the command you want to run.

Support

If you find this plugin useful and want to support my efforts, buy me a coffee ☕.

buy me a coffee