8

I work in U.S. and Europe, and switching between month/day (US) and day/month (EU) formats leads to ambiguities when looking through logs or notes (e.g. is 7/8/12 July 8th or 7th August?).

I'd like to change Notepad++ timestamp feature to use international format

YYYY-MM-DD--DAY--HH:MM

2012-08-19--Sun--20:30

Note, having the DAY of the week spelt out would be a nice extra, and avoids having to look up what day it was in a calendar.

Currently NPP does not have this flexibility. Is there a way to do this using a macro or script of some kind, perhaps using Ruby?

3
  • What are the dashes between DD & DAY and DAY & HH? Aug 20, 2012 at 10:10
  • The double dashes -- are to visually set-off the date from the day from the time. Seems more pleasing aesthetically than 2012-08-19-Sun-20:30. (Would like the timestamp to be a single unit of characters, hence don't want spaces...) Have edited the question accordingly. Aug 20, 2012 at 15:57
  • 1
    Having done heavy coding work over the years using Notepad++ due to its out-of-the-box powerful featureset, you have no idea how many times I've wanted to be able to plug into it with a scripting language from within the GUI. Thanks for posting this! Sep 6, 2012 at 20:09

1 Answer 1

6

Sharing how to do this using the NppExec plugin and Ruby.

Solution:

Approach: Get an external scripting language (in this case Ruby) to return the formatted timestamp and for NppExec to receive it and insert it into the cursor location in the current file shown in Notepad++.

First configure the console :

    npe_console v+   // set console to receive output in $(OUTPUT) variable
    npe_console d+   // set current working directory to same as current file

Then in NppExec > Execute menu command, enter the following snippet:

    // Diary snippet (Generates timestamp YYYY-MM-DD--DAY--HH:MM for NPP++)
    npp_console disable     // turn off output displaying to console
    // this ruby one-liner does the work
    ruby -r Date -e "dt=DateTime.now; dname = Date::ABBR_DAYNAMES[dt.wday]; puts dt.strftime(\"%Y-%m-%d--#{dname}--%H:%M\");"  
    sel_settext $(OUTPUT)   // put result at cursor in current file
    npp_console enable      // restore output displaying to console
    npp_console 0           // hide the console window

This uses the Ruby engine to generate the timestamp and inserts it where the cursor is in the current file in Notepad++. The result (with about a 500ms delay) is the timestamp:

    2012-08-20--Mon--20:16

Note: While this certainly does the job, if anyone has a solution that doesn't rely on anything external (in this case Ruby), that would be better.


Edit: "Productionising" the solution... (robustness and convenience)

By following the steps below, the above solution can be productionised...

A hurdle is that closing Notepad++ loses the console configuration, so this has to be setup on every re-start.

So we set up the console configuration as a script that is run automatically every time Notepad++ starts.

Then associate a keyboard shortcut (hotkey) to the diary script for convenience.

Step 1: Enter the diary code into an NppExec Excute... dialog: Plugins > NppExec > Execute..., giving it a name (you'll use that name later)

Step 1 - Enter diary code

Step 2: Enter the setup_console configuration code similarly:

Step 2 - Enter console configuration code

Steps 3, 4, 5: In Plugins > NppExec > Advanced Options, set setup_console to run at startup (3), create a Menu item for the diary code (4), and put the Menu item into the Macro menu (5):

Steps 3,4,5 - Getting it all setup

Step 6: Set a keyboard hotkey (shortcut) to the diary script: Settings > Shortcut Mapper... > Plugins, and scroll down till you find the diary script name you used...

Step 6 - Keyboard shortcut

Result: Looking under the Macros menu command, you'll see your new Timestamp command with keyboard shortcut.

Result - done

Done!

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .