Sublime Forum

New Snippets about envirnment tag

#1

Could I make a new snippet to insert nowTime or nowDate ?
I don’t understand the variable is ? $date ? :unamused:

0 Likes

#2

AFAIK, at present you cannot put interpolated code in snippets, so you can’t do what you want with a snippet only.

What you can do, is create a plugin in Python that will compute the date and insert it in an inline snippet, effectively achieving the same result.

0 Likes

#3

Here’s an example:

[code]# put the following in insertdate.py under %appdata%/Sublime Text/Packages/User
import sublime, sublimeplugin
import datetime

class SimplyInsertDateCommand(sublimeplugin.TextCommand):
def run(self, view, args):
dateAsStr = datetime.datetime.today().strftime("%x")
view.insert(view.sel()[0].begin(), dateAsStr)
[/code]

[code]# put the following in insertdate2.py under %appdata%/Sublime Text/Packages/User
import sublime, sublimeplugin
import datetime
class InsertDateWithSnippetCommand(sublimeplugin.TextCommand):
def run(self, view, args):
dateAsStr = datetime.datetime.today().strftime("%x")
snippet = “Date: $1 abc $2 abc ${3:%s}” % dateAsStr
view.runCommand(“insertInlineSnippet ‘%s’” % snippet)

[/code]

# put this in your user's key bindings (look in the Preferences menu) <binding key="d,a,t,e,tab" command="simplyInsertDate" /> <binding key="d,a,t,e,2,tab" command="insertDateWithSnippet" />

0 Likes

#4

You actually don’t need to put the two plugins in two separate files, but ST was giving me some trouble if I didn’t. After restarting it everything works fine again with a single file.

0 Likes

#5

hi!

I resume this tread to know if is possible to insert some little code in the snippet, for the date or similar function.

For example I’ve this snippets:

[code]
<![CDATA[
##############################################################################

Copyright © 2011 A.C.M.E. Inc.

All Rights Reserved

This program is free software: you can redistribute it and/or modify

it under the terms of the GNU Affero General Public License as published

by the Free Software Foundation, either version 3 of the License, or

(at your option) any later version.

This program is distributed in the hope that it will be useful,

but WITHOUT ANY WARRANTY; without even the implied warranty of

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

GNU General Public License for more details.

You should have received a copy of the GNU Affero General Public License

along with this program. If not, see http://www.gnu.org/licenses/.

##############################################################################
]]>
agpl
source.python
AGPL OpenERP

[/code]

Is pretty to substitute the 2011 on the template with a function that return the year…

Best Regards

Franco Tampieri

0 Likes

#6

I’m using this hack for the last few months and it’s working ok.

code.wuub.net/magicsnippets/

By default, snippets are expanded .on_pre_save(), you can play with “Default (Template).sublime-keymap”, but for me it causes more problems than it solves.

[code]
<![CDATA[

Author : (ms.user_name() or ms.env("USERNAME"))

Date : ms.str_date()

(…)
]]>
(…)

[/code]

0 Likes