Using NppExec and Ruby to script a new macro feature for Notepad++
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?
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 2: Enter the setup_console configuration code similarly:
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):
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...
Result: Looking under the Macros
menu command, you'll see your new Timestamp command with keyboard shortcut.
Done!