Home Download Buy Blog Forum Support

Insert command output in Snippet?

Insert command output in Snippet?

Postby quentinsf on Sun Jul 24, 2011 7:41 pm

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
quentinsf
 
Posts: 5
Joined: Sun Jul 24, 2011 7:36 pm

Re: Insert command output in Snippet?

Postby guillermooo on Sun Jul 24, 2011 8:58 pm

This would be an idea:

Code: Select all
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})


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.
guillermooo
 
Posts: 729
Joined: Thu Jul 23, 2009 9:06 am

Re: Insert command output in Snippet?

Postby quentinsf on Mon Jul 25, 2011 6:56 am

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
quentinsf
 
Posts: 5
Joined: Sun Jul 24, 2011 7:36 pm

Re: Insert command output in Snippet?

Postby guillermooo on Mon Jul 25, 2011 7:01 am

Have you tried ["t","s","tab"] as key binding?

More: http://sublimetext.info/docs/en/referen ... dings.html
guillermooo
 
Posts: 729
Joined: Thu Jul 23, 2009 9:06 am

Re: Insert command output in Snippet?

Postby quentinsf on Mon Jul 25, 2011 6:48 pm

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...
quentinsf
 
Posts: 5
Joined: Sun Jul 24, 2011 7:36 pm


Return to Technical Support

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 47 guests