Sublime Forum

Plugins don't work, or I'm stupid

#1

Ok, I have test.py in C:\Documents and Settings[user]\Application Data\Sublime Text\Packages\User.

Contents of this test.py are:

[code]import sublime, sublimeplugin

This simple plugin will add ‘Hello, World!’ to the end of the buffer when run.

To run it, save it within the User/ directory, then open the console (Ctrl+~),

and type: view.runCommand(‘sample’)

See http://www.sublimetext.com/docs/plugin-basics for more information

class Test1Command(sublimeplugin.TextCommand):
def run(self, view, args):
view.insert(view.size(), “Hello, World!\n”)[/code]

I type view.runCommand(‘Test1’) at console and nowt happens, not even an error message.

Things I’ve tried:

  • deleting test.pyc and resaving test.py, which causes test.pyc to be recreated
  • calling Test1Command other things
  • view.runCommand(‘test1’) in case it’s case sensitive
  • shouting, in case it can hear me

I really want to like Sublime but when the documentation says you can do something simple like this and it doesn’t work then there’s no way I’m paying for it. Or I’m stupid.

Assuming cleverer people help me to solve this, how do I use all the plugins in the subdirectories of C:\Documents and Settings[user]\Application Data\Sublime Text\Packages ?

Thank you in anticipation of helpful assistance.

0 Likes

#2

hi, you should expect to see hello world inside your edited file, for example, if the cursor was at the end of the last line of code, you should expect:

[code]import sublime, sublimeplugin

This simple plugin will add ‘Hello, World!’ to the end of the buffer when run.

To run it, save it within the User/ directory, then open the console (Ctrl+~),

and type: view.runCommand(‘sample’)

See http://www.sublimetext.com/docs/plugin-basics for more information

class Test1Command(sublimeplugin.TextCommand):
def run(self, view, args):
view.insert(view.size(), “Hello, World!\n”)Hello, World![/code]

do you see it? the “Hello, World!”?

if it doesn’t work for, you should check the following:

  1. open the console
  2. do a little change on your script (add empty line for example)
  3. now save the file
  4. look at the console do you see errors? it this python parser gives errors at this stage, the script won’t even run so later nothing will happen

if you want (for example for debug) to print to console, use

print "Hello, World!\n"

this example was constructed that way, because most plugins are for help you on your current edited file: do some actions, parsing etc.

good luck

0 Likes

#3

You should be fine if you do view.runCommand(‘test1’) rather than view.runCommand(‘Test1’)

0 Likes

#4

Thanks for your replies.

jps - As I said in my original post, I tried view.runCommand(‘test1’) but that didn’t work either.

vim - The file I was editing (which was the script itself) remained unchanged. I can change the script so it errors on compilation.

Can I turn on enhanced logging so I can see exactly what happens when I call view.runCommand ?

0 Likes

#5

Ah, I missed that part :slight_smile:

There’s no reason the plugin you posted shouldn’t work - certainly, it does for me. Just to narrow things down, can you use this instead:

import sublime, sublimeplugin

print "plugin loaded"

class Test1Command(sublimeplugin.TextCommand):
   def run(self, view, args):
      print "command run"

And see what gets printed in the console. Do you see “plugin loaded” when saving the file?

0 Likes

#6

Brilliant. That works exactly as you say it should. (I’m new to Python too so didn’t know I could put print commands all over the place!)

Also, and quite bizarrely, my other plugin seems to work too.

Now I have two files in C:\Documents and Settings[user]\Application Data\Sublime Text\Packages\User with a class called Test1Command (jps.py and test.py). How does Sublime know which I want?

Also, how do I use all the plugins in the other subdirectories of C:\Documents and Settings[user]\Application Data\Sublime Text\Packages ?

0 Likes

#7

All plugins are put into the same namespace, so you can run them all in the same manner. e.g., you can run the command in Packages/Default/GotoSymbol.py by doing view.runCommand(‘gotoSymbol’).

As to which of your two plugins will be run, I believe it’ll just be whichever one is loaded last.

0 Likes

#8

I worked for hours trying to get the most basic, like this one, plugins to work yesterday.

I’m using ST3, so maybe there are differences in plugin development between that an ST2.

But here are the things I did that seem to have worked. 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 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 at the very top in the import section, as well as in the class line, which I’ll illustrate below… [code]# didn’t work for me…
    class ExampleCommand(sublimePlugin.TextCommand):

DID work for me…

class ExampleCommand(sublime_plugin.TextCommand):[/code]

0 Likes

#9

This thread is from 2009, and deals with Sublime Text 1, when the API was very different. I recommend not referencing it :smile:
Maybe check out docs.sublimetext.info/en/latest/ … ugins.html ?

0 Likes