Sublime Forum

Using Keymapped Command as part of snippet

#1

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 Date 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.

0 Likes

#2

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

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!' )

0 Likes

#3

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.

0 Likes