article thumbnail
Custom Plugins in Sublime Text
How to create your own plugins in Python to automate anything
5 min read
#

Supercharge Sublime: Writing Your First Custom Plugin

Sublime Text is one of those editors that just works. It's fast, flexible, and delightfully minimal -- until you realize you can make it do even more. That's where custom plugins come in.

Let's explore how to turn Sublime into your editor, one Python file at a time.


Why Write a Sublime Plugin?

You know that repetitive task you do every day? Maybe it's inserting a license header, formatting code, or cleaning up TODOs before a commit. Plugins are your secret weapon to automate all of it.

Sublime's plugin API lets you hook into the editor and make it respond to events -- like saving, typing, or opening a file. You can even add new commands, menus, or shortcuts.

Think of it like this: you're not just using an editor anymore -- you're training it to work your way.


Your First Plugin

Every plugin starts as a single Python file inside a package folder in Packages/User/.

Here's the smallest possible example -- a command that inserts a friendly header at the top of a file:

import sublime
import sublime_plugin

class InsertHeaderCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.insert(edit, 0, "# Hello from my first Sublime plugin!\n")

Save that file as insert_header.py under Packages/User/, then open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P on macOS) and run Insert Header.

Boom. You just wrote a plugin.

Want to go deeper? Check the official Sublime plugin API reference.


Hooking into Events

Commands are great, but Sublime can also react to what you do -- like saving or editing a file.

class HelloOnSave(sublime_plugin.EventListener):
    def on_post_save(self, view):
        sublime.status_message("File saved -- great job!")

This EventListener fires every time you save a file and posts a status message at the bottom. Try it out -- it feels surprisingly rewarding.


Keep Things Smooth

Sublime runs plugin code in a helper process called plugin_host. That means a bad plugin won't crash your editor -- but it can still make things sluggish. To keep things smooth, avoid doing heavy work directly in run() or on_modified(). For long-running tasks, offload them with:

sublime.set_timeout_async(my_function)

Share It

Once your plugin works, you can package it up with menus, key bindings, and settings. If you're feeling confident, publish it on GitHub and submit it to Package Control so others can use it too.


Wrapping Up

Sublime plugins aren't just for power users -- they're for anyone who wants their tools to bend to their workflow.

Start small: automate one little annoyance. Then watch your editor evolve into something that feels tailor‑made for you.


Want to go further? Your editor is more powerful than you think.