Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

view.run_command("example") not working

I created first plugin for sublime text 3 with name relative:

import sublime, sublime_plugin

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

Then save it to path

/pathToSublime3/Packages/relative/relative.py

Then try to test it in command window (cntrl + `):

view.run_command('example') 

I expected to see Hello, World! in the beginning of current opened file, but fail. What I do wrong?

like image 306
Maxim Yefremov Avatar asked Sep 25 '26 06:09

Maxim Yefremov


1 Answers

If your experience was like mine, you may have copied several super basic plugins to try to find one that worked. I think the failure of the others was affecting the Hello World one. For what it's worth, here are the things I did that seem to have worked.

I'm using ST3 on Win7, so maybe there are differences in plugin development between that and ST2.

Note that as I tried many different permutations of these, I don't exactly know which ones are the real fix. But if these get you working, then maybe you can start making incremental changes and see when it breaks.

  • Moved all *.py files that were my plugins into "Packages" folder. (I read many posts that said that plugin files in the "Packages\User" or "Packages\NameOfPlugin" folder should be read, but I haven't tried them yet. I put it in "Packages" to make sure that finding the files wasn't a problem.)
  • Restart Sublime after every change. (The docs say you don't have to, but if you have errors in your plugins, it seems that the auto-reload feature doesn't work. Which kinda makes sense. Once I got the errors out of all my plugins, the auto-reload feature started working correctly.)
  • A few of the simple/tutorial plugins I copied off the web (even some from sublimetext.com!) referenced/imported "sublimePlugin" which errorred on my ST3 Win7 instance. Apparently the correct library to import is "sublime_plugin". The two places this is used, in basic plugins, are 1) at the very top in the import section, as well as 2) in the class line, which I'll illustrate below...

    # didn't work for me...
    class ExampleCommand(sublimePlugin.TextCommand):
    
    # DID work for me...
    class ExampleCommand(sublime_plugin.TextCommand):
    
like image 96
sugardaddy Avatar answered Sep 29 '26 17:09

sugardaddy