Can i add date time for sublime snippet?

2019-03-07 18:22发布

I want to create a snippet that will add a file comment, but I want the snippet to create the DateTime automatically. Can a sublime snippet do that?

<snippet>
    <content><![CDATA[
/**
 * Author:      $1
 * DateTime:    $2
 * Description: $3
 */

]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <tabTrigger>/header</tabTrigger>
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <scope>source.css,source.js,source.php</scope>
</snippet>

5条回答
聊天终结者
2楼-- · 2019-03-07 18:53

Tools > New Plugin

Paste this:

import datetime, getpass
import sublime, sublime_plugin
class AddDateCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.run_command("insert_snippet", { "contents": "%s" %  datetime.date.today().strftime("%d %B %Y (%A)") } )

class AddTimeCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.run_command("insert_snippet", { "contents": "%s" %  datetime.datetime.now().strftime("%H:%M") } )

Save it as ~/Library/Application Support/Sublime Text 2/Packages/User/add_date.py

Then, in Preferences > Key Bindings - User , add:

{"keys": ["ctrl+shift+,"], "command": "add_date" },
{"keys": ["ctrl+shift+."], "command": "add_time" },

You can customize the argument passed to strftime to your liking.

查看更多
Luminary・发光体
3楼-- · 2019-03-07 18:56

You can use the SMART Snippets plugin for Sublime Text 2.

With SMART Snippets, You can now use Python to dynamically create snippets

I did some research for another question and I am pretty sure this plugin could solve your question.

查看更多
太酷不给撩
4楼-- · 2019-03-07 18:59

Nachocab, that was a GREAT answer - and helped me VERY much. I created a slightly different version for myself

~/Library/Application Support/Sublime Text 2/Packages/User/datetimestamp.py:

import datetime, getpass
import sublime, sublime_plugin

class AddDateTimeStampCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.run_command("insert_snippet", { "contents": "%s" %  datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") } )

class AddDateStampCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.run_command("insert_snippet", { "contents": "%s" %  datetime.datetime.now().strftime("%Y-%m-%d") } )

class AddTimeStampCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.run_command("insert_snippet", { "contents": "%s" %  datetime.datetime.now().strftime("%H:%M:%S") } )

Preferences > Key Bindings - User:

{"keys": ["super+alt+ctrl+d"], "command": "add_date_time_stamp" },
{"keys": ["super+alt+d"], "command": "add_date_stamp" },
{"keys": ["super+alt+t"], "command": "add_time_stamp" }

I wouldn't have been able to do this without your help! I scoured google for about an hour now and finally was graced by your answer! Thank's so much!

查看更多
放荡不羁爱自由
5楼-- · 2019-03-07 19:03

This post on the official ST forum answers your question and provides a close alternative.

In summary, no, you cannot currently insert datetime from a ST snippet.

查看更多
Animai°情兽
6楼-- · 2019-03-07 19:16

You might want to check the InsertDate package: https://github.com/FichteFoll/InsertDate

In the readme you can find an example of how you can use macros to insert timestamps into snippets.

查看更多
登录 后发表回答