+4

Is it possible to execute a command instead of inserting content for snippets?

Oktay Acikalin 14 years ago updated by Rodrigo Flores 12 years ago 3
Say I have text like YYYY-MM-DD and want to replace it with the value 2011-03-22. I don't want to do this via a key binding but rather via a tab trigger. The trigger would be YYYY-MM-DD and a TextCommand could return the proper content.
Is that possible?
Whether it's a command, or custom snippet, or some other form of text completion, possibly tied to a key binding, I REALLY REALLY would like to be able to Insert Date (and I suspect others might want to go further and insert time, possibly even format the inserted date and/or time).   With TextMate, there was a Auto-Completion code setup (in TEXT category I think)  isoD (tab) automatically inserted the Date in ISO format - that would be a great start!
+2
That can easily be done with a plugin. Something like this:
import sublime_plugin
import datetime
class DateCompleter(sublime_plugin.EventListener):
    def on_query_completions(self, view, prefix, locations):
        return [
            ("YYYY-MM-DD", str(datetime.date.today())),
            ("YYYY", str(datetime.date.today().year)),
            ("MM", "%2d" % datetime.date.today().month),
            ("DD", "%2d" % datetime.date.today().day)
        ]

I think the best way to achieve this is to allow something like this on a .sublime-completions file:


Is there any way to do it ? 

{ "trigger": "date", "command": "today" },