Skip to content

Instantly share code, notes, and snippets.

@maddievision
Created January 17, 2012 04:43
Show Gist options
  • Save maddievision/1624759 to your computer and use it in GitHub Desktop.
Save maddievision/1624759 to your computer and use it in GitHub Desktop.
Insert current date and/or time, for Sublime
import sublime, sublime_plugin, datetime
#supports multiple selections/cursors
def replace_sel(view,edit,text):
s = set()
for r in view.sel():
view.replace(edit, r, text)
e = r.begin()+text.__len__()
s.add(sublime.Region(e,e))
view.sel().clear()
for r in s:
view.sel().add(r)
def cur_date():
return datetime.datetime.now().strftime("%Y-%m-%d")
def cur_time():
return datetime.datetime.now().strftime("%H:%M")
class InsertDateTimeCommand(sublime_plugin.TextCommand):
def run(self, edit):
replace_sel(self.view,edit,"[%s] [%s] " % (cur_date(), cur_time()))
class InsertDateCommand(sublime_plugin.TextCommand):
def run(self, edit):
replace_sel(self.view,edit,"[%s] " % cur_date())
class InsertTimeCommand(sublime_plugin.TextCommand):
def run(self, edit):
replace_sel(self.view,edit,"[%s] " % cur_time())
@maddievision
Copy link
Author

Some key bindings if you want:
[
{ "keys": ["ctrl+alt+d"], "command": "insert_date" },
{ "keys": ["ctrl+alt+e"], "command": "insert_date_time" },
{ "keys": ["ctrl+alt+f"], "command": "insert_time" }
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment