Home Download Buy Blog Forum Support

Using Keymapped Command as part of snippet

Using Keymapped Command as part of snippet

Postby mickbw on Mon Jun 18, 2012 5:43 pm

What I am trying to do is have a snippet automatically populate a date and time as part of the documentation for new functions.

I use the <a href="https://github.com/paulollivier/sublimetext-date">Date </a> plugin with a keymapping to Ctrl + F5 to add the current date and time.

Is there a way to make this command call when a snippet is generating code.
mickbw
 
Posts: 10
Joined: Mon Mar 12, 2012 6:20 pm

Re: Using Keymapped Command as part of snippet

Postby agibsonsw on Tue Jun 19, 2012 12:09 am

I haven't looked at this in detail but, FWIW: A snippet cannot call a command, and I don't believe there is a tm-variable that retrieves the current date.

I believe the date could be set as an environment variable, and then referred to in a snippet, but you might still need to set the variable (once a day) manually.

You could use the key-map Ctrl-F5 at the appropriate point within your snippet. Alternatively, you could copy and modify the date code so that you have a version that inserts your additional snippet-text as well:

Code: Select all
import sublime, sublime_plugin
from datetime import datetime

class DateCommand(sublime_plugin.TextCommand):
   """Prints Date + H:M"""
   def run(self, edit):
      self.view.insert(
         edit,
         self.view.sel()[0].begin(),
         datetime.now().strftime("%d/%m/%Y %H:%M")
         )

class HourCommand(sublime_plugin.TextCommand):
   """Prints only H:M"""
   def run(self, edit):
      self.view.insert(
         edit,
         self.view.sel()[0].begin(),
         datetime.now().strftime("%H:%M")
         )

Code: Select all
class DateCommand(sublime_plugin.TextCommand):
   """Prints Date + H:M"""
   def run(self, edit):
      self.view.insert(
         edit,
         self.view.sel()[0].begin(),
         'Hi,' + datetime.now().strftime("%d/%m/%Y %H:%M") + '\n\tthere!'
         )
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Using Keymapped Command as part of snippet

Postby atomi on Tue Jun 19, 2012 1:49 am

Textmate has $TM_DATE but it's not working with Sublime Text.
It sure would be nice to have though!

Edit: $TM_YEAR works
Edit2: It would be great if we had an api hook that allowed filling in these variables.
atomi
 
Posts: 342
Joined: Thu Jan 20, 2011 5:06 pm
Location: Los Angeles CA US


Return to Technical Support

Who is online

Users browsing this forum: Google [Bot] and 16 guests