Home Download Buy Blog Forum Support

Beginner question: how is the "command" name set?

Beginner question: how is the "command" name set?

Postby 4141done on Fri May 06, 2011 11:29 pm

Hello everyone,

I am new to SublimeText as well as Python programming so forgive me if I am missing something that should be obvious. I am trying to write a plugin for Sublime Text 1 that inserts a timestamp using this as a reference.

My point of confusion is, how do I bind my newly-created plugin to a key command. The samply plugin runs using
Code: Select all
<binding key="ctrl+alt+t" command="sample"/>

However the word "sample" is not included in the filename nor within the code. Simularly, in the link I'm trying to emulate, "insert_timestamp" is not referenced anywhere in the code.

If my code looks like this:
Code: Select all
import datetime
import sublimeplugin

class InsertTimestampCommand(sublimeplugin.TextCommand):
  def run(self, edit):

    #grab the active view
    view = sublime.activeWindow().activeView()

    #generate the timestamp
    timestamp_str = datetime.datetime.now().isoformat(' ')

    #for region in the selection
    #(i.e. if you have multiple regions selected,
    # insert the timestamp in all of them)
    for r in view.sel():
      #put in the timestamp
      #(if text is selected, it'll be
      # replaced in an intuitive fashion)
      view.erase(edit, r)
      view.insert(edit, r.begin(), timestamp_str)


Then what would my key binding look like and how do I know this information?

Thank you very much and I look forward to learning a lot from all of you.

Javier
4141done
 
Posts: 2
Joined: Fri May 06, 2011 11:20 pm

Re: Beginner question: how is the "command" name set?

Postby adzenith on Sun May 08, 2011 3:34 pm

As far as I've been able to tell, the process is this:
- remove the word "Command"
- insert an underscore before every capital letter but the first (not sure how it deals with multiple consecutive caps)
- convert to lower case

so:
InsertTimestampCommand becomes insert_timestamp.
I'm not sure why this conversion is done... I agree it's a bit confusing.
adzenith
 
Posts: 1217
Joined: Mon Oct 19, 2009 9:12 pm

Re: Beginner question: how is the "command" name set?

Postby jps on Mon May 09, 2011 2:52 am

In Sublime Text 1.x, the command name will be "insertTimestamp", in Sublime Text 2, it'll be "insert_timestamp"
jps
Site Admin
 
Posts: 3071
Joined: Wed Mar 19, 2008 12:33 pm

Re: Beginner question: how is the "command" name set?

Postby 4141done on Mon May 09, 2011 5:03 pm

Thank you very much for your answers! I was poring over the plugin API reference trying to find the solution for quite some time!

Everything is working perfectly now and I look forward to jumping in to plugins for sublime text.
4141done
 
Posts: 2
Joined: Fri May 06, 2011 11:20 pm


Return to Technical Support

Who is online

Users browsing this forum: Yahoo [Bot] and 18 guests