Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i add date time for sublime snippet?

Tags:

sublimetext2

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> 
like image 359
aisensiy Avatar asked Aug 09 '12 08:08

aisensiy


People also ask

How do I edit a sublime snippet?

Select Package Resource Viewer: Open Resource, navigate down the list to LaTeX, then open the section-.. -(section). sublime-snippet file. You should now be able to edit this file and save it, which will create a new file Packages/LaTeX/section-..

How do I use sublime snippets?

To create a new snippet, select Tools | New Snippet…. Sublime Text will present you with an skeleton for a new snippet. Snippets can be stored under any package's folder, but to keep it simple while you're learning, you can save them to your Packages/User folder.

How do I create a custom snippet in Sublime Text 3?

To create a new snippet in Sublime Text 3, go to: Tools -> Developer -> New Snippet... This opens a new window containing a new snippet template, which looks like this: There are four parts to a snippet.


1 Answers

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.

like image 105
nachocab Avatar answered Sep 23 '22 08:09

nachocab