Sublime Forum

Easiest way to insert date/time with a single keypress?

#1

I want something like notepad.exe does when I press F5.

0 Likes

Way to monitor/log time(seconds) plugin is run in console for noobs
#2

save this in your Packages/User folder (with the .py extension):

import sublime, sublime_plugin, time

class InsertDatetimeCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        sel = self.view.sel();
        for s in sel:
            self.view.replace(edit, s, time.ctime())

and add this to your user keybindings file:

{ "keys": "f5"], "command": "insert_datetime"}
5 Likes

#3

Thank you! It works.

0 Likes

#4

How do I add this to a snippet? For example:

<snippet>
	<content><![CDATA[
<!--- Remove after testing - $insert_datetime --->
]]></content>
	<tabTrigger>r</tabTrigger>
</snippet>
0 Likes

#5

Thank you - this is very cool.
How can I modify the code so that I can have just the date or a choice of date formats?

0 Likes

#6

maybe this will help, just a small insert date/time/version plugin by me

the towebu_InsertDateTimeVersion.py:

[code]import sublime
import sublime_plugin
import time

date_format = “%Y-%m-%d” # 2013-10-02
time_format = “%H:%M:%S” # 10:24:01
stamp_format = “%y%m%d%H%M%S” # 131002102355
version_format = “v%Y.%m.%d.%H.%M.%S” # v2013.10.02.10.23.52
version_pattern = “v[0-9]{4}.[0-9]{2}.[0-9]{2}.[0-9]{2}.[0-9]{2}.[0-9]{2}”

class towebu_insert_versionCommand(sublime_plugin.TextCommand):
def run(self, edit):
sels = self.view.sel()
for sel in sels:
if not self.view.find_all(version_pattern):
self.view.replace(edit, sel, time.strftime(version_format));
return

        region = sublime.Region(0, self.view.size())
        results = self.view.find_all(version_pattern)
        for result in results:
            self.view.replace(edit, result, time.strftime(version_format))

class towebu_insert_stampCommand(sublime_plugin.TextCommand):
def run(self, edit):
sels = self.view.sel()
for sel in sels:
self.view.replace(edit, sel, time.strftime(stamp_format));

class towebu_insert_dateCommand(sublime_plugin.TextCommand):
def run(self, edit):
sels = self.view.sel()
for sel in sels:
self.view.replace(edit, sel, time.strftime(date_format));

class towebu_insert_timeCommand(sublime_plugin.TextCommand):
def run(self, edit):
sels = self.view.sel()
for sel in sels:
self.view.replace(edit, sel, time.strftime(time_format));

[/code]

the towebu_InsertDateTimeVersion.sublime-commands:

  {
    "caption": "towebu: Insert Version",
    "command": "towebu_insert_version"
  },
  {
    "caption": "towebu: Insert Stamp",
    "command": "towebu_insert_stamp"
  },
  {
    "caption": "towebu: Insert Date",
    "command": "towebu_insert_date"
  },
  {
    "caption": "towebu: Insert Time",
    "command": "towebu_insert_time"
  }
]

the towebu_InsertDateTimeVersion.sublime-keymap:

    { "keys": "ctrl+i", "ctrl+v"], "command": "towebu_insert_version" },
    { "keys": "ctrl+i", "ctrl+s"], "command": "towebu_insert_stamp" },
    { "keys": "ctrl+i", "ctrl+d"], "command": "towebu_insert_date" },
    { "keys": "ctrl+i", "ctrl+t"], "command": "towebu_insert_time" }
]
2 Likes

#7

Using my Evaluate plugin you can type datetime.date.today() and evaluate it to give you a formatted date.

1 Like

#8

[quote=“tobi73de”]maybe this will help, just a small insert date/time/version plugin by me

[/quote]

Worked for me - thank you!

0 Likes

#9

[quote=“tobi73de”]maybe this will help, just a small insert date/time/version plugin by me

[/quote]

Thanks again, it works well. I was wondering though, when I insert the date or time, it is highlighted. If I press the space bar the inserted text disappears.
Is there a way to get the text to stay aside from hitting an arrow key?

0 Likes

#10

[quote=“GrouchyGaijin”] I was wondering though, when I insert the date or time, it is highlighted. If I press the space bar the inserted text disappears.
Is there a way to get the text to stay aside from hitting an arrow key?[/quote]

There is. Replace the final 3 classes with these:

class TowebuInsertStampCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        sels = self.view.sel()
        for sel in sels:
            if sel.empty():
                self.view.insert(edit, sel.a, time.strftime(stamp_format))
            else:
                self.view.replace(edit, sel, time.strftime(stamp_format))


class TowebuInsertDateCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        sels = self.view.sel()
        for sel in sels:
            if sel.empty():
                self.view.insert(edit, sel.a, time.strftime(date_format))
            else:
                self.view.replace(edit, sel, time.strftime(date_format))


class TowebuInsertTimeCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        sels = self.view.sel()
        for sel in sels:
            if sel.empty():
                self.view.insert(edit, sel.a, time.strftime(time_format))
            else:
                self.view.replace(edit, sel, time.strftime(time_format))

Basically, what I did (aside from remove some unnecessary semi-colons and change the class names to reflect accepted coding style) was to add a test to see if the selection is empy (just a plain cursor) or not. If it is empty, you insert the formatted string, and if the selection contains something, you replace its contents with that same string.

Let me know if this still works for you!

0 Likes

#11

Just putting this up here: github.com/FichteFoll/sublimetext-insertdate
Only works on ST2 at the moment due to some recent ST3 regressions. However, I want to get an updated version out ASAP with some additional features.

If you need to embed this in a snippet, you can do so with a macro: github.com/FichteFoll/sublimete … t-17020468

0 Likes

#12

Hi

@FichteFoll: Nice extension of the script ! I’m not that Pythonista, so i have one question: what is “sel.a” ?

mfg Tobias

0 Likes

#13

Actually, the wonderful InsertDate predates your plugin, but it’s not compatible with ST3. Grumble, grumble :wink:

selection is a Region. The a property is the starting point of the region. See the API for more: sublimetext.com/docs/3/api_ … ime.Region

0 Likes

#14

Hi,
I’ve taken the liberty to edit the weslly’s code to my needs, so I’ll post it here, maybe someone will find it useful.

import sublime, sublime_plugin, time

class InsertDatetimeCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        format = "%Y-%m-%d %H.%M:%S"
        sel = self.view.sel();
        for s in sel:
            if s.empty():
            	self.view.insert(edit, s.a, strftime(format, localtime()))
            else:
            	self.view.replace(edit, s, strftime(format, localtime()))
0 Likes

#15

How am I supposed to use these plugins? I saved them as .py files in the proper directory, but I am not seeing any options anywhere to actually run the plugins or use them to insert a date. Google is not helping either.

0 Likes

#16

You need to manually bind a key to the command:

  • Menu Preferences -> Key Binding User
  • Add something like {“keys”: “f9”], “command”: “insert_datetime”}

You can check “Key binding - Default” for the format of the file/key.

If you want it to appear in the panel (ctrl+shift+p), you need to add a file named Default.sublime-commands in the same directory at the python file with something like:

{ "caption": "Insert Date/Time", "command": "insert_datetime" } ]

Just one note: those scripts were written for ST2, you might have issue with ST3 (maybe)

1 Like

#17

Just follow below 2 steps

  1. Create one python file with name ex : ‘add_date.py’ and save it in path(MAC)
    /Users/sgumgeri/Library/Application Support/Sublime Text 2/Packages/User/add_date.py
    ----- add_date.py ----
    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.datetime.today().strftime("-------------------------------\n %A %d %B %Y, %H:%M \n------------------------------- \n") } )

  2. Add the file name ‘add_date’ with shortcut key in file
    Menu Preferences -> Key Binding User
    --------------- Default (OSX).sublime-keymap --------------
    [ { "keys": "myd", "command": "InsertDatetimeCommand"}, {"keys": ["ctrl+/"], "command": "add_date" } ]

Save both the files and just use the shortcut keys in any new file.
You will see the changes thats it

1 Like

#18

Hello I have same question. Any solution please?

1 Like

#19

related:

1 Like

#20

Sorry I am a little bit confused. I must add this code into my snippet or into my “file.py”?

snippet = "Packages/User/test.sublime-snippet"
view.run_command("insert_snippet", {"name": snippet, "DATE": "Today"})

here is my plugin and test snippet:
Data\Packages\User\datum-a-cas.py

import sublime, sublime_plugin, time
class InsertDatetimeCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        sel = self.view.sel();
        for s in sel:
            self.view.replace(edit, s, time.strftime("<%d-%m-%Y  %H:%M>"))

Data\Packages\User\testdatumu.sublime-snippet

<snippet>
	<content><![CDATA[
Date - ${DATE}
]]></content>
	<tabTrigger>date</tabTrigger>
</snippet>

I tried this by my own strengths, but I can’t solve it. :frowning:

1 Like