By josh / February 20, 2014

How to execute a command every time Sublime launches

Maybe you like to work with Sublime in full screen or distraction free mode, and the first thing you do when you open it is switch to your preferred display style.

Maybe you share your settings between several computers and wish that Sublime was smart enough to change a few settings depending on your operating system.

Or maybe you just want a freakin’ panel to stay open.

That’s the one that got me recently. I was writing and testing plugin code for my upcoming book, [Writing Sublime Plugins][1].

When you’re writing a Sublime plugin in Sublime, restarting the editor frequently is the name of the game.

For the most part, it’s painless, since Sublime remembers your open tabs. But I also needed the Sublime console open so I could see errors, and there’s no way to keep that panel visible across restarts. I was wearing out the Ctrl+` combo on my keyboard. It got old.

I finally decided that I could probably fix this plugin-writing annoyance with a plugin (how meta), and it turned out to be even simpler than I’d guessed.

Better yet, you can use this same approach to run just about any arbitrary command when Sublime launches.

Let’s see how it works.

To get started, open a new tab in Sublime, then enter the following code:

import sublime, sublime_plugin

def plugin_loaded():
    window = sublime.active_window()
    window.run_command("show_panel", {"panel": "console", "toggle": True})

In case you don’t recognize it, this is Python, which is the language Sublime uses for plugins. I’d never used Python before I started working on my own plugins, but it turns out to be an easy language to read and write. If you have some basic programming knowledge and understand a language like JavaScript, Ruby or PHP, you’ll pick up Python easily. (It’s one of my favorite languages to work in now.)

In the code above, we’re defining a plugin_loaded method. When Sublime loads our file, it will see this method and automatically execute any code it contains.

In plugin_loaded we’re getting a reference to the current Sublime window and using its run_command method to execute the show_panel command. This command is what displays panels like the find and replace panels. In this case, we’re letting Sublime know that we want the console panel.

Note: This plugin only works in Sublime Text 3. The plugin_loaded API method didn’t exist in Sublime Text 2.

That’s all there is to it. Now we just need to install it …

To install the plugin, first launch the command palette and run Preferences : Browse Packages. This opens your file browser to the Packages directory. Create a new folder in this directory folder called PinConsole.

Now switch back to Sublime and click File | Save to open the file save dialog. Browse to the Packages/PinConsole folder you just created, then save the plugin file as

As soon as you save the file, Sublime will load the plugin and call plugin_loaded. The console should pop up immediately, and it’ll open every time you launch Sublime until you delete the plugin. (To make this plugin more sophisticated, we could create a settings file that allows the user to toggle the plugin off and on as desired.)

The plugin_loaded hook is extremely useful–the possibilities are endless. You could use it to automatically update your git repository. Or if you use Dropbox to sync settings between computers, you might use it to set a different font size on your Windows desktop (with it’s giant 4K monitor) than you use on your MacBook Air.

Here’s another example that sets a different font depending on whether the machine is running OS X or Windows:

import sublime, sublime_plugin

def plugin_loaded():

    s = sublime.load_settings("Preferences.sublime-settings")

    if sublime.platform() == "osx":
        s.set("font_face", 'Courier')
        s.set("font_face", 'Consolas')


This is just a small example of what you can do with a basic knowledge of Sublime’s plugin system and APIs. Understanding these principles unlocks a whole universe of power and flexibility.

Want to see how you can take charge of Sublime by writing your own plugins? Join the Sublime Text Tips newsletter for more info:

About the author


Adam - September 7, 2015

Could you use a different trigger to run commands? E.g. When first switching to a project?

Click here to add a comment

Leave a comment: