Sublime Forum

Snippet - execute python code or a command?

#1

Hi,

I had a look on http://docs.sublimetext.info/en/latest/extensibility/snippets.html but did not find any way how to execute python code in a snippet. Or even execute a command.

I would like to have a snippet

<block name="${1:name}>
    <anything>${2:$currentdate}</anything>
</block>   

where $currentdate should be replaced with current date in a special format. (i.e. 2018-02-07T14:37:00)

Any idea? Or not possible in a snippet?

Regards,
Markus

0 Likes

Macro in Sublime Text - specific line number
#2

You cannot evaluate Python code (or similar things) within snippets. You can, however, utilize macros together with snippets to auto-insert some more dynamic text, such as the current date.

There is an example on the InsertDate package readme that explains how you can do it:

Alternatively, you can of course create a custom command and use Python as you wish, which you then use to fill the contents for the insert_snippet command.

2 Likes