Sublime Forum

Insert command output in Snippet?

#1

Hello all - just learning Sublime - using v2… looks lovely…

In TextMate there’s a way to include the output of a command in a snippet expansion - a timestamp, in my particular case.

Can I do that in Sublime? I’ve worked out how to create a snippet, and how to create a command that can be brought up with a keystroke, but I’d like to embed a timestamp in the text created by a snippet.

Ta in advance,
Quentin

0 Likes

#2

This would be an idea:

[code]import sublime, sublime_plugin

import re
import datetime

class AddTimeStampCommand(sublime_plugin.TextCommand):
def run(self, edit, contents):
# Call like this:
# Hey it’s $$%d-%m-%Y %H:%M:%S$$ ${1:now}.$0
format = re.search(r"$$(.?)$$", contents).groups()[0]
print format
result = datetime.datetime.now().strftime(format)
contents = re.sub(r"$$.
?$$", result, contents)
print contents
self.view.run_command(‘insert_snippet’, {‘contents’: contents})
[/code]

It would probably be a better idea to create a insert_snippet_through_shell command that would extract interpolated shell code within the snippet, run it in a subprocess and do substitutions as needed with the results.

0 Likes

#3

Ah, yes, that works quite nicely, and I could bind it to a keystroke, though I’ve got more used to typing ‘ts{tab}’ than remembering complex key combinations.

Is it possible to bind this command to that kind of sequence, so as to be more like Textmate/Textexpander?

Many thanks,
Q

0 Likes

#4

Have you tried “t”,“s”,“tab”] as key binding?

More: sublimetext.info/docs/en/referen … dings.html

0 Likes

#5

Ha! Splendid! Thanks…

For some reason I assumed that key combinations bound that way needed to be simultaneous. Silly me.

OK. I’m sold. Will go and buy a licence…

0 Likes

#6

Sorry, but, can someone explain what to put it in the sublime-snippet file?

If I want to create a Todo snippet, then execute toogle_comment and add “TODO”

<snippet>
	<content>
         /* I want to execute the toogle comment, and after add TODO
        "command": "toggle_comment", "args": { "block": false }
        <![CDATA[TODO]]>
</content>
	<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
	<tabTrigger>todo</tabTrigger>
	<!-- Optional: Set a scope to limit where the snippet will trigger -->
	<!-- <scope>source.python</scope> -->
 <description>Todo</description>
</snippet>

Thanks

0 Likes

#7

@Albert, sounds like you don’t really want to execute the toggle_comment command, but to insert a comment marker followed by TODO:

<snippet>
    <content><![CDATA[
${TM_COMMENT_START}TODO: ${1}${TM_COMMENT_END}
]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <tabTrigger>todo</tabTrigger>
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <!-- <scope>source.python</scope> -->
</snippet>

if you did want to execute the toggle_comment command, and then insert some text, a snippet is not the right tool for the job.

2 Likes

#8

Yes, thanks a lot, that’s what I wanted.

I have to learn more about variables, like TM_COMMENT_START, I didn’t know about them

1 Like