Home Download Buy Blog Forum Support

[SOLVED] How to create a plugin?

[SOLVED] How to create a plugin?

Postby trebitzki on Wed Aug 12, 2009 11:36 pm

I know people must have done this hundreds of times, but without instructions I just don't get it. It would seem that a simple walkthrough for newbies is not too much to ask?

The whole point of this little exercise is to be able to write the current date in a file. In Notepad2 the shortcut is Ctrl+F5. Being just a teeny bit more ambitious, I wanted to have the date at the top of the file with the cursor in a blank line underneath - sort of a LOG file system. And my keyboard shortcut would be Ctrl+Shift+F5. So here goes:

Using the menu command 'Tools - New Plugin' I created a plugin and saved it under 'C:\Documents and Settings\username\Application Data\Sublime Text\Packages\User\InsertDateAtTop.py'. Here's the code:

Code: Select all
import sublime, sublimeplugin
from datetime import date

# This simple plugin will insert the current date/time at top of the buffer when run.
# To run it, save it within the User/ directory, then open the console (Ctrl+~),
# and type: view.runCommand('InsertDateAtTop')
#
# See http://www.sublimetext.com/docs/plugin-basics for more information

class InsertDateAtTop(sublimeplugin.TextCommand):
   def run(self, view, args):
      now = date.today()
      view.insert(0, now.strftime("%Y.%m.%d %a %H:%M") + "\n\n")
      view.runCommand("move line -1")


This is my first try at python, so there's bound to be some bugs in it!

Anyway, I saved the file, and the console panel says:

Writing file C:\Documents and Settings\username\Application Data\Sublime Text\Packages\User\InsertDateAtTop.py with encoding UTF-8
Reloading plugin C:\Documents and Settings\username\Application Data\Sublime Text\Packages\User/InsertDateAtTop.py


So that sounds good.

Following the instructions in the HelloWorld sample, I opened the console and typed:
>>> view.runCommand('InsertDateAtTop')

Nothing. No output. Now I assume if there's an error in the code it would spit it out in the console. But the console is helpfully silent.

OK, that's one issue. The other is setting up the keybinding.

The Plugin Examples page says:
Duplicate
This defines a simple command, "duplicate" that duplicates the current line. To run, add this line to Default.sublime-keymap:
<binding key="ctrl+alt+d" command="duplicate"/>
(or type view.runCommand('duplicate') in the console.)


This information is also very terse. It doesn't say in which 'Default.sublime-keymap' file to add the binding. It could be Packages/Default or Packages/User. I assumed since I put the code in the User folder, that's where the keybinding would go. So I wrote this code there:

Code: Select all
<bindings>
   <binding key="ctrl+shift+f5" command="InsertDateAtTop"/>
</bindings>


You guessed it, nothing happens, no text in the console. I tried putting the binding in Packages/Default, same result.

What is wrong here? And why is a $60 product not being documented?

I don't consider the 'Support' section to live up to that name. Everywhere on the site I see 'documentation is in progress ;).' Opensource projects for the most part are very lively with screenshots, tutorials and even screencasts. I guess they have to try harder.

Looking through the forum I saw one of you guys ('sublimator') is busy creating screencasts with camtasia. Kudos to you! Unfortunately I can't make head or tails of what you are trying to say. There's no narration and the mouse and letters just rush all over the place. I tried installing the 'Persistent Shell' package and type 'ls -lah' in a file, as seen in the screencast http://blogdata.akalias.net/console3/console3.htm. An excercise in futility. Mainly because I don't know how sublimetext works!!

I know this post sounds ranty, but I really would like to use the product. It has the greatest interface on the market (Monokai!), and with just a little more effort with the docs people will be really happy and make you rich.

So pullease Jon, Nick et al. write a simple help file for people to get started, OK?

Oh yeah, what's wrong with my code?
Last edited by trebitzki on Thu Aug 13, 2009 12:00 pm, edited 1 time in total.
trebitzki
 
Posts: 5
Joined: Wed Aug 12, 2009 10:16 pm

Re: How to create a plugin?

Postby samkerr on Thu Aug 13, 2009 12:23 am

I actually ran into a similar problem the other day.

Your command name can only have 1 capital letter in it. So 'MycoolpluginCommand' is fine but 'MyCoolPluginCommand' is not fine.

Kind of a silly point and should be easily addressable with a patch.

Agreed, the documentation isn't all that great, so why not play with it and write a tutorial? That'd help everyone out! Or post it on the wiki at http://sublimetextwiki.com/
samkerr
 
Posts: 51
Joined: Mon Aug 03, 2009 11:20 pm

Re: How to create a plugin?

Postby trebitzki on Thu Aug 13, 2009 7:06 am

OK, I got it to run. I changed the class name to 'insertDateAtTop' and changed the file name and key binding entry to match. Now the keyboard shortcut works. The code:

Code: Select all
import sublime, sublimeplugin
from datetime import date

# insert current date/time at top of file
# followed by two blank lines,
# placing cursor in first blank line

class insertDateAtTop(sublimeplugin.TextCommand):
   def run(self, view, args):
      now = date.today()
      
      # Move the cursor to the beginning of the file
      view.runCommand('moveTo', ['bof'])

      # --- my try ---
      # Insert the date and two blank lines, then move up one line
      #view.insert(0, now.strftime("%Y.%m.%d %a %H:%M") + "\n\n\n")
      
      # why doesn't this work??
      #view.runCommand("move lines -1")
      
      # --- sublimator ---
      # insert snippet - this works, but what's the $1 doing there?
      view.runCommand (
         'insertInlineSnippet',
         [now.strftime("%Y.%m.%d %a %H:%M") + "\n$1\n\n"] )


I don't understand why my code to move the cursor doesn't work.

When I finally figure this out, I'd like to put it in the wiki. Is there an easy way for me to do that, Jon?
trebitzki
 
Posts: 5
Joined: Wed Aug 12, 2009 10:16 pm

Re: [SOLVED] How to create a plugin?

Postby samkerr on Thu Aug 13, 2009 12:12 pm

Interesting that names can have more than 1 capital letter in them. I ran into no end of trouble the other day with a plugin and changing the name was the problem, at least for me.

Oh well.
samkerr
 
Posts: 51
Joined: Mon Aug 03, 2009 11:20 pm

Re: [SOLVED] How to create a plugin?

Postby trebitzki on Sat Aug 15, 2009 8:07 am

Another question on creating plugins...

I want to make one that saves the current file and exits the app.

saveAndExit.py
Code: Select all
import sublime, sublimeplugin

class saveAndExit(sublimeplugin.TextCommand):
   def run(self, view, args):
      # Save the current file
      view.runCommand('save')

      # Quit Sublime
      view.runCommand('exit')


Doesn't work. I thought you can use commands like that?

http://www.sublimetext.com/docs/commands:
save Saves the current buffer, prompting for a file name if it doesn't have one yet

exit Closes all windows


BTW can anyone make a new forum category like 'tutorials'? I'd like to write this up so that people can find it right on the forum page without searching...
Would be a good place to put tips and stuff scattered around the forum and the user blogs (sublimator, EJ12N) too, eh?
trebitzki
 
Posts: 5
Joined: Wed Aug 12, 2009 10:16 pm

Re: [SOLVED] How to create a plugin?

Postby trebitzki on Sat Aug 15, 2009 9:44 am

I'll get back to you in a week, I'm offline on vakay... ta ta
trebitzki
 
Posts: 5
Joined: Wed Aug 12, 2009 10:16 pm

Re: [SOLVED] How to create a plugin?

Postby EJ12N on Mon Aug 17, 2009 7:59 pm

trebitzki wrote:Another question on creating plugins...

I want to make one that saves the current file and exits the app.

saveAndExit.py
Code: Select all
import sublime, sublimeplugin

class saveAndExit(sublimeplugin.TextCommand):
   def run(self, view, args):
      # Save the current file
      view.runCommand('save')

      # Quit Sublime
      view.runCommand('exit')


Doesn't work. I thought you can use commands like that?

http://www.sublimetext.com/docs/commands:
save Saves the current buffer, prompting for a file name if it doesn't have one yet

exit Closes all windows


BTW can anyone make a new forum category like 'tutorials'? I'd like to write this up so that people can find it right on the forum page without searching...
Would be a good place to put tips and stuff scattered around the forum and the user blogs (sublimator, EJ12N) too, eh?


That doesn't work is because you have sublimeplugin.TextCommand
you are using windows commands so you have 2 ways of accomplishing what you want...

replace:sublimeplugin.TextCommand with: sublimeplugin.WindowCommand
in the above code and instead of view use now the arg is window.

Code: Select all
import sublime, sublimeplugin

class saveAndExit(sublimeplugin.WindowCommand):
   def run(self, window, args):
      # Save the current file
      window.runCommand('save')

      # Quit Sublime
      window.runCommand('exit')

it should work =] haven't tested it yet but it should.
---
The other solution instead of doing all those changes is to use view.window().runCommand instead of view.runCommand, this is what I personally use when doing window commands :)

** I have added saveAndExit to my PowerUser package :) **
PS: as a bonus, if you use hotExit instead of exit command you preserve the session when quitting sublime :)
EJ12N
 
Posts: 281
Joined: Tue Apr 21, 2009 5:23 pm

Re: [SOLVED] How to create a plugin?

Postby trebitzki on Tue Aug 25, 2009 7:14 pm

EJ12N wrote:The other solution instead of doing all those changes is to use view.window().runCommand instead of view.runCommand, this is what I personally use when doing window commands :)


Great!! That did it! Now I have a simple Notetaking system:

  • Open Sublime Text using a windows keyboard shortcut (Ctrl+Shift+S).
  • Session data is automatically restored with the last open file, in my case the 'notes' file.
  • Ctrl+Shift+F5 starts the 'insertDateAtTop' plugin, ready to type another note.
  • Ctrl+Shift+F4 saves the file and quits the app.

Whew! [wipesweat /] That was quite a journey! At every step another nuance was presented by the helpful gurus, showing me how to get it done.

Again, there is absolutely no information on the website, explaining these intricate ins and outs of writing a plugin.

I sure would like to write this up so people can have it all in one place. Could an admin please create a 'Tutorials' section in the forum?
trebitzki
 
Posts: 5
Joined: Wed Aug 12, 2009 10:16 pm

Re: [SOLVED] How to create a plugin?

Postby EJ12N on Wed Sep 02, 2009 8:48 pm

Good job on the notetaking implementation :)

trebitzki wrote:
EJ12N wrote:The other solution instead of doing all those changes is to use view.window().runCommand instead of view.runCommand, this is what I personally use when doing window commands :)


Great!! That did it! Now I have a simple Notetaking system:

  • Open Sublime Text using a windows keyboard shortcut (Ctrl+Shift+S).
  • Session data is automatically restored with the last open file, in my case the 'notes' file.
  • Ctrl+Shift+F5 starts the 'insertDateAtTop' plugin, ready to type another note.
  • Ctrl+Shift+F4 saves the file and quits the app.

Whew! [wipesweat /] That was quite a journey! At every step another nuance was presented by the helpful gurus, showing me how to get it done.

Again, there is absolutely no information on the website, explaining these intricate ins and outs of writing a plugin.

I sure would like to write this up so people can have it all in one place. Could an admin please create a 'Tutorials' section in the forum?
EJ12N
 
Posts: 281
Joined: Tue Apr 21, 2009 5:23 pm

Next

Return to Technical Support

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 20 guests