Sublime can generate a plugin template for us. To generate a plugin, navigate to Tools | New Plugin…. Then we should see a screen similar to that shown in the following screenshot:
The previous screenshot is what a "Hello, World!" plugin looks like. Before starting to write our own code, let's test the following code by saving the file by pressing Ctrl + S on Windows or Linux and Command + S on OS X. The Save dialog will open in the Packages/User
folder. We don't have to save the file there. We will browse one folder up and create a new folder named RelationsFinder
. Now let's save the file as RelationsFinder.py
. The filename doesn't really matter, but the convention is that the file name should be the same as the plugin name. After we've saved the plugin, let's try running it. To run the file, we'll need to open the console by pressing Ctrl + ` on Windows or Linux and Control + ` on OS X. Enter the following line in the console to test your new plugin:
view.run_command('example')
Pressing Enter will insert Hello World! where the cursor is positioned in our currently open file. Let's try it again, but this time, we'll change the command name from ExampleCommand
to RelationsFinderCommand
, as shown in the following screenshot:
As you can see in the preceding screenshot, 'relations_finder'
will run RelationsFinderCommand
. This is Sublime's naming convention for commands. Sublime also provides three different types of commands:
View
object.Window
objectSince we will be opening views with this plugin, we will use the sublime_plugin.WindowCommand
class as the base of our command. To do that, we will change our class definition to the following:
class RelationsFinderCommand(sublime_plugin.WindowCommand):
Then remove the edit
parameter from our run
function:
def run(self): pass
Now we want our plugin command to show up in the command palette. Let's create a new file named RelationsFinder.sublime-commands
and save it in the same folder with the following content:
[ { "caption": "RelationsFinder: Find Relations", "command": "relations_finder" } ]
This file will determine which commands we want to expose to the command palette. We will expose our relations_finder
command by choosing the option RelationsFinder: Find Relations
. We can now open the command palette by pressing Ctrl + Shift + P in Windows or Linux and Command + Shift + P in OS X and look for the RelationsFinder: Find Relations command, as shown in the following screenshot:
Clicking on the RelationsFinder: Find Relations command will execute our run
command, which currently does nothing.