Sublime Forum

[SOLVED] How to create a plugin?

#1

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:

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:

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:

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:

<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?

0 Likes

#2

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 sublimetextwiki.com/

0 Likes

#3

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:

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?

0 Likes

#4

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.

0 Likes

#5

Another question on creating plugins…

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

saveAndExit.py

[code]import sublime, sublimeplugin

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

	# Quit Sublime
	view.runCommand('exit')

[/code]

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

http://www.sublimetext.com/docs/commands:

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?

0 Likes

#6

I’ll get back to you in a week, I’m offline on vakay… ta ta

0 Likes

#7

[quote=“trebitzki”]Another question on creating plugins…

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

saveAndExit.py

[code]import sublime, sublimeplugin

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

	# Quit Sublime
	view.runCommand('exit')

[/code]

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

http://www.sublimetext.com/docs/commands:

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?[/quote]

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]import sublime, sublimeplugin

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

	# Quit Sublime
	window.runCommand('exit')

[/code]
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 :smile:

**** I have added saveAndExit to my PowerUser package :smile: ****
PS: as a bonus, if you use hotExit instead of exit command you preserve the session when quitting sublime :smile:

0 Likes

#8

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?

0 Likes

#9

Good job on the notetaking implementation :smile:

[quote=“trebitzki”]

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?[/quote]

0 Likes