Home Download Buy Blog Forum Support

New Snippets about envirnment tag

New Snippets about envirnment tag

Postby luxxon on Mon May 10, 2010 6:27 am

Could I make a new snippet to insert nowTime or nowDate ?
I don't understand the variable is ? $date ? :roll:
luxxon
 
Posts: 2
Joined: Thu Nov 19, 2009 12:57 pm

Re: New Snippets about envirnment tag

Postby guillermooo on Mon May 10, 2010 3:09 pm

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

Re: New Snippets about envirnment tag

Postby guillermooo on Mon May 10, 2010 8:55 pm

Here's an example:

Code: Select all
# 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: Select all
# 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: Select all
# 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" />
guillermooo
 
Posts: 729
Joined: Thu Jul 23, 2009 9:06 am

Re: New Snippets about envirnment tag

Postby guillermooo on Mon May 10, 2010 9:22 pm

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

Re: New Snippets about envirnment tag

Postby DrDran on Sun Jan 01, 2012 7:54 pm

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: Select all
<snippet>
    <content><![CDATA[
##############################################################################
#   
#    Copyright (C) 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/>.
#
##############################################################################
]]></content>
    <tabTrigger>agpl</tabTrigger>
    <scope>source.python</scope>
    <description>AGPL OpenERP</description>
</snippet>


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

Best Regards

Franco Tampieri
DrDran
 
Posts: 17
Joined: Sat Aug 13, 2011 7:33 pm
Location: Imola

Re: New Snippets about envirnment tag

Postby wuub on Tue Jan 03, 2012 10:18 am

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

http://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: Select all
<snippet>
   <content><![CDATA[
# Author         : (`ms.user_name() or ms.env("USERNAME")`)
# Date           : `ms.str_date()`
(...)
]]></content>
(...)
</snippet>
SublimeREPL via Package Control & SublimePTY dev/pre-alpha. Donations are appreciated, but not required :) Twitter: @wuub
wuub
 
Posts: 218
Joined: Wed Aug 10, 2011 8:16 am
Location: Poland


Return to Technical Support

Who is online

Users browsing this forum: Google [Bot] and 14 guests