Sublime Forum

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

#1

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

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

0 Likes

#2

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.

0 Likes

#3

In Sublime Text 1.x, the command name will be “insertTimestamp”, in Sublime Text 2, it’ll be “insert_timestamp”

0 Likes

#4

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.

0 Likes