Home Download Buy Blog Forum Support

Insert Date (?) Function

Insert Date (?) Function

Postby tsyroid on Sun Jan 30, 2011 3:19 am

Greetings all. First post here, so go gentle :-)

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.
tsyroid
 
Posts: 15
Joined: Sun Jan 30, 2011 3:02 am

Re: Insert Date (?) Function

Postby adzenith on Sun Jan 30, 2011 6:40 am

There's no built-in command, but here's how I'd do it:
Make a new file, and paste in this code:
Code: Select all
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:
Code: Select all
  { "keys": ["super+alt+t"], "command": "insert_timestamp" },

Let me know if you run into any problems.
adzenith
 
Posts: 1215
Joined: Mon Oct 19, 2009 9:12 pm

Re: Insert Date (?) Function

Postby jps on Sun Jan 30, 2011 7:06 am

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.

Code: Select all
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)
jps
Site Admin
 
Posts: 3067
Joined: Wed Mar 19, 2008 12:33 pm

Re: Insert Date (?) Function

Postby adzenith on Sun Jan 30, 2011 7:12 am

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!
adzenith
 
Posts: 1215
Joined: Mon Oct 19, 2009 9:12 pm

Re: Insert Date (?) Function

Postby jps on Sun Jan 30, 2011 7:24 am

Ah, yeah, that is what view.replace does, the plugin should indeed be using erase..insert as you originally had it
jps
Site Admin
 
Posts: 3067
Joined: Wed Mar 19, 2008 12:33 pm

Re: Insert Date (?) Function

Postby tsyroid on Mon Jan 31, 2011 4:09 pm

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.
tsyroid
 
Posts: 15
Joined: Sun Jan 30, 2011 3:02 am

Re: Insert Date (?) Function

Postby agend on Fri Nov 25, 2011 7:11 am

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
agend
 
Posts: 4
Joined: Fri Nov 11, 2011 8:01 pm

Re: Insert Date (?) Function

Postby Strom on Mon Apr 02, 2012 10:15 pm

agend wrote: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


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

Code: Select all
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)
Strom
 
Posts: 3
Joined: Mon Apr 02, 2012 10:11 pm


Return to Technical Support

Who is online

Users browsing this forum: dubeg, Yahoo [Bot] and 8 guests