28

ファイル コメントを追加するスニペットを作成したいのですが、スニペットで DateTime を自動的に作成する必要があります。崇高なスニペットはそれを行うことができますか?

<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>
4

7 に答える 7

101

ツール > 新しいプラグイン

これを貼り付けます:

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") } )

~/Library/Application Support/Sublime Text 2/Packages/User/add_date.py として保存します。

次に、 Preferences > Key Bindings - User に以下を追加します。

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

に渡される引数をstrftime 好みに合わせてカスタマイズできます。

于 2012-12-14T16:40:23.173 に答える
13

ナチョキャブ、それは素晴らしい答えでした-そして私をとても助けてくれました。少し違うバージョンを自分で作成しました

〜/ 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") } )

環境設定>キーバインディング-ユーザー:

{"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" }

私はあなたの助けなしにはこれを行うことができなかっただろう!私は今グーグルを約1時間精査し、ついにあなたの答えに恵まれました!本当にありがとう!

于 2013-01-28T17:34:17.557 に答える
2

Sublime Text 2 にはSMART Snippetプラグインを使用できます。

SMART Snippet を使用すると、Python を使用して動的にスニペットを作成できるようになりました

別の質問について調査しましたが、このプラグインがあなたの質問を解決できると確信しています。

于 2012-10-07T13:17:25.447 に答える
-4

公式の ST フォーラムのこの投稿は、あなたの質問に答え、近い代替案を提供します。

要約すると、いいえ、現在、ST スニペットから日時を挿入することはできません。

于 2012-08-10T00:02:52.287 に答える