Sublime Forum

Insert Date (?) Function

#1

Greetings all. First post here, so go gentle :smile:

DL’d Sublime 2 Alpha (20110129) for a test drive and I’m having a blast. Kudos to the developers; very well done.

Short profile: Seasoned Linux user (about 15+ years) and know my way around most distros (used to build new Gentoo installations on weekends for kicks). I know absolutely nothing about programming languages beyond rudimentary basics, although I do recognize and marvel at a nicely written chunk of code. Most of my day-to-day stuff consists of editing config and log files, git commit comments, etc.

What I’d use Sublime for predominantly is to write changelogs for the systems I maintain (I’m kinda anal about writing out what i change when I change it; part of having KRAFT disease I guess). Part of that routine is to “time-stamp” all entries. EG:

<< snip >>
2011-01-29 20:13 HRS TMS

  • change 1
  • change 2
    << ends >>

I’ve read what documentation I could, and scoured the forums, but come up empty. I’m assuming this would need to be done thru a python script tied into Sublime? (ie, there’s no built-in command to do such a thing I’ve missed?) If so, could I edit the insert-date python function gedit uses and drop it into Sublime?

Any direction I can run with would be much appreciated.

0 Likes

Beginner question: how is the "command" name set?
#2

There’s no built-in command, but here’s how I’d do it:
Make a new file, and paste in this code:

import datetime

class InsertTimestampCommand(sublime_plugin.TextCommand):
  def run(self, edit):
    #grab the active view
    view = sublime.active_window().active_view()

    #generate the timestamp
    timestamp_str = datetime.datetime.now().isoformat(' ')

    #for region in the selection
    #(i.e. if you have multiple regions selected,
    # insert the timestamp in all of them)
    for r in view.sel():
      #put in the timestamp
      #(if text is selected, it'll be
      # replaced in an intuitive fashion)
      view.erase(edit, r)
      view.insert(edit, r.begin(), timestamp_str)

Save the file in the User folder in your packages folder, which you can find by going to Preferences>Browse Packages… This folder will probably be ~/.Sublime Text 2/Packages/User/ if you’re on Linux.

Once you’ve got the file, add this to your User Keybindings file:

{ "keys": "super+alt+t"], "command": "insert_timestamp" },
Let me know if you run into any problems.

0 Likes

#3

btw, I’ve made a couple of changes to the script (untested though):

  • Using self.view rather than active_view(). It’s better for text commands to use this, as they won’t always be run on the active view.
  • Using view.replace rather than erase…insert.
import datetime
import sublime_plugin

class InsertTimestampCommand(sublime_plugin.TextCommand):
  def run(self, edit):
    #generate the timestamp
    timestamp_str = datetime.datetime.now().isoformat(' ')

    #for region in the selection
    #(i.e. if you have multiple regions selected,
    # insert the timestamp in all of them)
    for r in self.view.sel():
      #put in the timestamp
      #(if text is selected, it'll be
      # replaced in an intuitive fashion)
      self.view.replace(edit, r, timestamp_str)
0 Likes

#4

Oh, sweet. I thought there must have been a better way to get the view. Thanks for pointing out that a TextCommand has a view element.
I used View.replace originally, but it didn’t have the behavior I desired: when you use replace the inserted text ends up selected, whereas if you use erase and then insert, the text is not selected at the end. Is this how View.replace is supposed to work?
Thanks!

0 Likes

#5

Ah, yeah, that is what view.replace does, the plugin should indeed be using erase…insert as you originally had it

0 Likes

#6

Many thanks to both jps and adzenith. That’s what I love most about Linux: community and extensibility.
I’ll give this a go tonight and if it works, Sublime will be getting my registration later this week.

0 Likes

#7

it works fine, but could anybody tell me how to modify the plugin to clear the selection -
now the stamp is selected and i have to press esc or move cursor to continue editing

i just want the cursor to be placed just after the date/time

thanks in advance
Arek

0 Likes

#8

[quote=“agend”]it works fine, but could anybody tell me how to modify the plugin to clear the selection -
now the stamp is selected and i have to press esc or move cursor to continue editing

i just want the cursor to be placed just after the date/time

thanks in advance
Arek[/quote]

I had the same wish, and I achieved it with this:

[code]import datetime
import sublime_plugin

class InsertTimestampCommand(sublime_plugin.TextCommand):
def run(self, edit):
#generate the timestamp
timestamp_str = datetime.datetime.now().strftime("%d.%m.%Y %H:%M")

#for region in the selection
#(i.e. if you have multiple regions selected,
# insert the timestamp in all of them)
for r in self.view.sel():
  #put in the timestamp
  #(if text is selected, it'll be
  # replaced in an intuitive fashion)
  if r.size() > 0:
    self.view.replace(edit, r, timestamp_str)
  else:
    self.view.insert(edit, r.begin(), timestamp_str)[/code]
0 Likes