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

Hooks

by twolfson ST2/ST3

Run Sublime commands on common event hooks (e.g. on_new, on_post_save).

Labels events, hooks

Details

Installs

  • Total 2K
  • Win 759
  • OS X 1K
  • Linux 485
Oct 21 Oct 20 Oct 19 Oct 18 Oct 17 Oct 16 Oct 15 Oct 14 Oct 13 Oct 12 Oct 11 Oct 10 Oct 9 Oct 8 Oct 7 Oct 6 Oct 5 Oct 4 Oct 3 Oct 2 Oct 1 Sep 30 Sep 29 Sep 28 Sep 27 Sep 26 Sep 25 Sep 24 Sep 23 Sep 22 Sep 21 Sep 20 Sep 19 Sep 18 Sep 17 Sep 16 Sep 15 Sep 14 Sep 13 Sep 12 Sep 11 Sep 10 Sep 9 Sep 8 Sep 7 Sep 6
Windows 0 0 1 1 1 0 0 1 0 2 1 0 1 0 0 1 4 1 2 1 2 0 1 1 0 1 0 1 0 1 2 0 2 0 0 0 0 1 1 2 0 0 0 0 0 0
OS X 3 0 0 0 0 1 0 0 1 1 3 1 1 0 0 0 1 2 3 3 1 1 2 1 0 0 1 6 1 1 1 2 5 1 0 0 0 0 1 2 0 2 0 0 0 1
Linux 0 1 2 0 0 2 1 1 0 2 0 0 0 0 1 0 0 0 2 0 0 1 0 1 1 1 0 1 1 0 0 0 1 0 0 0 0 0 3 0 0 0 0 0 1 0

Readme

Source
raw.​githubusercontent.​com

sublime-hooks

Run Sublime commands on common event hooks (e.g. on_new, on_post_save).

This was designed to give event level bindings to other Sublime plugins.

My use case was to make a request (via sublime-request to a server when a save occurs. The result was:

"on_post_save_user": [
  {
    "command": "request",
    "args": {
      "open_args": ["http://localhost:7060/"]
    },
    "scope": "window"
  }
]

Getting started

Installation

This package is available under hooks inside of Package Control, a Sublime Text plugin that allows for easy management of other plugins.

If you prefer the manual route, you can install the script via the following command in the Sublime Text terminal (ctrl+`) which utilizes git clone.

import os; path=sublime.packages_path(); (os.makedirs(path) if not os.path.exists(path) else None); window.run_command('exec', {'cmd': ['git', 'clone', 'https://github.com/twolfson/sublime-hooks', 'hooks'], 'working_dir': path})

Packages can be uninstalled via “Package Control: Remove Package” via the command pallete, ctrl+shift+p on Windows/Linux, command+shift+p on Mac.

Creating a new hook

For this exercise, we will be creating a binding that selects all text after a save occurs.

A hook can be added at the User, Project, or Language level. We will add a User level hook.

To edit User settings, open the command pallete, and select “Preferences: Settings - User”.

In the opened preferences, create a new key/value pair for on_post_save_user with the following:

"on_post_save_user": [
  {
    "command": "select_all"
  }
]

Then, save twice (once to save settings, another to trigger the plugin).

At this step, all text will be selected, demonstrating the hook and command were run.

Examples of user, project, and language hooks can be found below.

Documentation

Hooks are stored in the User, Project, or Language settings. Each of these expects a list of dictionaries. Each of those dictionaries satisfies the following:

  • command (required), Command for Sublime to run via run_command at the listed scope.
  • args (optional), Dictionary of arguments to be passed to . Comparable to args in “Key Bindings”.
  • scope (optional), String indicating where to run command. By default, commands are run in the view. Other options are window and app which run at the window and sublime levels respectively.
"on_post_save_user": [
  {
    // Runs `request` command
    "command": "request",

    // Invokes `request` with `open_args=["http://...:7060/"]`
    "args": {
      "open_args": ["http://localhost:7060/"]
    },

    // Runs `request` via `window.run_command`
    "scope": "window"
  }
]

Accessing settings

User settings are accessed via “Preferences: Settings - User” in the command pallete.

Project settings are accessed via “Project -> Edit Project” from the menu bar. You must be in a saved project for this option to be accessible.

Language settings are accessed via “Preferences -> Settings - More -> Syntax Specific - User”. This will open settings specifically for the language in the open file.

Namespacing

Hooks are required to be namespaced at the User, Project, or Language level. The key will be the event_name followed by its _level.

The namespaces are _user, _project, and _language.

For demonstration:

  • An on_new hook at the Project level will be on_new_project
  • An on_load at the Language level will be on_load_language

Events

For both Sublime Text 2 and 3, you will have access to the following events:

  • on_new
  • on_clone
  • on_load
  • on_close
  • on_pre_save
  • on_post_save
  • on_activated
  • on_deactivated

For Sublime Text 3, you gain access to:

  • on_new_async
  • on_clone_async
  • on_load_async
  • on_pre_close
  • on_pre_save_async
  • on_post_save_async
  • on_activated_async
  • on_deactivated_async

Documentation on each hook can be found in the Sublime Text documentation:

Sublime Text 2 - http://www.sublimetext.com/docs/2/api_reference.html#sublime_plugin.EventListener

Sublime Text 3 - http://www.sublimetext.com/docs/3/api_reference.html#sublime_plugin.EventListener

The events not on these lists were excluded due to potential performance issues (e.g. on_modified, on_text_command).

Examples

User

User settings should be defined at the top level in your user's .sublime-settings. This can be accessed either via the Preferences: Settings - User command palette or Preferences -> Settings - User in the menu.

// Inside Packages/User/Preferences.sublime-settings
{
  "ignored_packages": [
  // ...
  ],
  "on_post_save_user": [
    {
      "command": "select_all"
    }
  ]
}

Project

Project settings should be defined under a settings in your current .sublime-project. This can be accessed either via the Project: Edit command palette or Project -> Edit in the menu.

// Inside my-project.sublime-project
{
  "folders": [
  // ...
  ],
  "settings": {
    "on_post_save_project": [
      {
        "command": "select_all"
      }
    ]
  }
}

Language

Language settings should be defined at the top level in your language's .sublime-settings. This can be accessed via Preferences -> Settings - More -> Syntax Specific - User in the menu.

// Inside my-language.sublime-settings
{
  "extensions": [
  // ...
  ],
  "on_post_save_language": [
    {
      "command": "select_all"
    }
  ]
}

Donating

Support this project and others by twolfson via gittip.

Support via Gittip

Unlicense

As of Sep 04 2013, Todd Wolfson has released this repository and its contents to the public domain.

It has been released under the UNLICENSE.