Monday, March 25, 2013

Notepad++ Time-Stamp

Kudos

I’ve been keeping some form of a work journal for years; I’d highly recommend it.  I recently got annoyed with creating timestamps in Notepad++, and ran across a helpful post in a forum.  It was so helpful that I thought I’d do my best to try to give it some additional street cred.

Here is the original link:
http://sourceforge.net/p/notepad-plus/discussion/331753/thread/3458d1da#62f2

What day of the week was 4/22/2006 anyway?

Before I start, if you have the TextFX plugin already setup in Notepad++, it provides 2 static methods of inserting a datetime stamp:
Menu: TextFX > TextFX Insert > I:Date & Time: short format: 3:56 PM 3/22/2013
Menu: TextFX > TextFX Insert > I:Date & Time: long format:  3:56 PM Friday, March 22, 2013
If these look good enough to you, you can skip to the section on how to map a keyboard shortcut to these.  However, in addition to the time and date, I also wanted the day of the week, like this:
03:36 PM 03/22/2013 (Friday)


Steps

It turns out this can easily be done with a simple Python script.  I don’t even know Python that well and I was able to do it.

1. Install the python plugin:
  • The plugin’s name is: Python Script
  • It’s recommend you do this via the NP++ Plugin Manger (Menu: Plugins > Plugin Manger)
  • Here’s a link to the project page:  http://npppythonscript.sourceforge.net/
  • The script includes some basic Python libraries, so you shouldn’t need to install Python or other 3rd party libraries

2. Setup script in Notepad++:
  • Menu:  Plugins > Python Script > New Python Script
  • It will ask you for a new file name, I choose: npp_timedate_stamp.py
  • A new tab will appear in NP++ for you to enter the script.
  • This is the script I used:
    
    # Add date/time stamp in Notepad++ 03:28 PM 03-22-2013 (Friday)
    import time
    editor.addText( time.strftime( '%I:%M %p %m/%d/%Y (%A) ' ) )
    
    
  • Here is a link to the Python documentation which shows what all the format string characters: http://docs.python.org/2/library/time.html
  • Save your script

3. Configure Notepad++ to use the script:
  • Menu: Plugins > Python Script > Configuration
  • You should see your script under the User Scripts section
  • Add it to Toolbar and/or Menu
  • I put it in both, but I only managed to find it under menu
  • It shows up as its own entry under Menu: Plugins > Python Script > npp_timedate_stamp

4. Create a keyboard shortcut (my preferred method):
  • Menu: Settings > Shortcut Mapper
  • Switch to Plugin
  • Scroll down until you see your script name (i.e. npp_timedate_stamp)
  • Click on it, choose Modify, and choose a key.  I used: CTRL + ;

Furthermore

This was exciting to me in more ways than one, because in the past I’ve looked for ways of modifying Notepad++, and plugins can be a daunting undertaking.  Apparently the Python plugin is mapped to all the Notepad++ features, so I might utilize it more in the future.

20 comments:

  1. Hello Jason,
    thanks for your post.
    I ve been looking for something like this since a while. Your solution works perfect for me.

    Comments:
    - I had to restart NP++ to find my script name in the Shortcut Mapper
    - For anyone interested: use '%d.%m.%Y %H:%M:%S' to get following output: 10.04.2013 09:52:45

    Thanks again, Peter

    ReplyDelete
  2. Thanks for the feedback Peter!

    ReplyDelete
  3. Excellent work and explanation! This is just what I needed for my note-taking during calls.

    Thanks, Rick

    ReplyDelete
  4. What versions of Notepad++ and Python Script plugin are you guys using? Because this isn't working for me. First time I trigger the script, nothing happens at all. Second time, I get a popup telling me that a script is already running. "Stop Script" doesn't seem to have any effect either.

    I'm running Notepad++ 6.3.3 and Python Script plugin 0.9.2.0 with Python 2.7.1 (release27-maint-npp, Feb 6 2011, 16:58:20) [MSC v.1500 32 bit (Intel)]

    ReplyDelete
  5. Nevermind, sorry for the noise.

    The problem was that Python Script plugin was installed into user's %APPDATA, which doesn't seem to work. All is fine after moving it to the application install location...

    ReplyDelete
  6. Glad you were able to figure it out. I double-checked my setup and my script is in the %APPDATA folder. I'm not sure what the problem is, but I also don't want to cause you any extra work!

    ReplyDelete
  7. Nice post! I have just added this to my NotePad++, and it works great! I did have to restart NotePad++ in order to see my script in the Shortcut Mapper, but after that everything was fine!

    ReplyDelete
  8. Hi Jason,
    Thanks for this blog - helped me out - nice work!

    ReplyDelete
  9. Great post. I used the guide to generate ISO 8601 timestamps with offsets (e.g. 2013-10-21T16:50:00+01:00).

    ReplyDelete
  10. Great work, thanks.

    ReplyDelete
  11. Thanks so much for posting this, this is exactly what I was looking for and it works great!

    ReplyDelete
  12. Thanks for this, I've created a script that automatically adds a new entry to my work journal in MarkDown format that includes the datestamp and a few preformatted headings. I love that so many programs now have Python APIs.

    ReplyDelete
  13. Awesome! I too have been looking for exactly this since I started using Npp.

    It's also a starting point for learning a little remedial Python...

    ReplyDelete
  14. Thank you very much.. Was really helpful..

    ReplyDelete
  15. Is there a way to use this to save a file as today's date? Thank you in advance.

    ReplyDelete
    Replies
    1. Try a variation of this command:

      import time
      notepad.saveAs( time.strftime('%I%M %p %m-%d-%Y (%A).txt') )

      You must pay attention to which folder it will try to save to.

      Delete
  16. THANK YOU!!! This works like a charm!

    ReplyDelete
  17. Quick tip, doing editor.clear() first lets u overwrite any selected text

    ReplyDelete
  18. .LOG in notepad

    ReplyDelete