Sublime Forum

Creating a simple plugin 101 help / feedback

#1

I’m using Sublime 3. I’m trying to make a plugin that modifies some selected text. I’ve found this is a lot harder to figure out than I expected.

Following I’ve created a file ~/Library/Application Support/Sublime Text 3/Packages/User/pivot_table.py with the following:

import sublime, sublime_plugin

class PivotTable(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.insert(edit, 0, "Hello, World!")

I’d like to register this to run via the command palette. To do this I’ve tried creating the files ~/.sublime-commands and ~/Library/Application Support/Sublime Text 3/Packages/User/Default.sublime-commands with the following contents:

{ "caption": "Table: pivot", "command": "PivotTable" }, ]

This does nothing. Could someone suggest what I did wrong?

The docs I have found are lacking. For example,

sublimetext.com/docs/3/api_reference.html appears to be incorrect in stating “Several pre-made plugins come with Sublime Text, you can find them in the Default package” because ~/Library/Application Support/Sublime Text 3/Packages/ only has a “User” folder and no “Default”.

sublime-text-unofficial-document … lette.html references a .sublime-commands file which is not noted anywhere else, and appears to not do anything.

0 Likes

#2

renaming PivotTable to pivot_table in the default.sublime-command will work

0 Likes

#3

Thanks for the response.

Tried that, it didn’t work. Opening the Command Palette doesn’t show the command at all, so I think it might be something else going on, as well.

Should the Default.sublime-commands file have existed in ~/Library/Application Support/Sublime Text 3/Packages/User/ ? I had to create it. Perhaps it should go somewhere else?

Also, shouldn’t I be referencing pivot_table.PivotTable somewhere, or something akin to it?

0 Likes

#4

The command will show up if the class was loaded and mappend in a Default.sublime-commands file.

Sublime will scan the user and packages folders for plugins and you can reference them by snake case, so you have to use pivot_table instead of PivotTable in the sublime-commands file.

I’ve just created the pivot_table.py and the Default.sublime-commands files with your content, changed PivotTable into pivot_table in the sublime-commands File, restarted Sublime Text and everything worked as expected. Please check the console for any errors.

0 Likes

#5

Got it. I had somehow named the file Default.sublime-commands.py by accident. Thanks for the help!

0 Likes

#6

you’re welcome! btw, you don’t have to restart the client, sublime will check for modifications and reloads the plugins/config files for you.

Yeah, this is misleading. There is a Default folder in Sublime Text 2.

This is a file type (not a file), you can name the file pivottable.sublime_commands and sublime text should be able to load it aswell.

0 Likes