Tag Archives: scripting

I had been looking off and on for a few years for a way to insert a timestamp into a file in Notepad++, preferably by a hotkey. The no-longer-in-development TextFX plugin frequently comes up in Google searches as one way of doing this, but the plugin doesn’t offer any ability to customize the format of the date or time stamp.

I found this article: http://sourceforge.net/p/notepad-plus/discussion/331753/thread/3458d1da that explains, briefly, how to do this using the Python Script plugin. I’ve been curious about Python for awhile, too, so I was willing to give it a shot. The SourceForge project page for Python Script is at http://sourceforge.net/projects/npppythonscript/.

So, here’s a quick tutorial on adding a customizable insert datestamp/timestamp macro to Notepad++.

Download the Python Script plugin from http://npppythonscript.sourceforge.net/download.shtml and install it. Do not download it from within Notepad++ by clicking Plugins | Plugin Manager | Show Plugin Manager, scrolling down the Available plugins list to Python Script, checking the box and clicking Install, because it just won’t work (and you may get an older version of the plugin).

Restart Notepad++ and you’ll find that Python Script has been added to the Plugins menu.

To create the script that will insert the timestamp, click Plugins | Python Script | New Script. Enter a filename for the script file you are about to create, like “Time.py”, and click Save.

A new, blank tab will appear in Notepad++. Paste in the following text:

import time 
editor.addText( time.strftime( '%Y-%m-%d %I:%M %p' ) )

Save the Time.py file to the default location in your user profile.

Add the Time.py script to Notepad++ by clicking on Plugins | Python Script | Configuration, highlighting Time.py, clicking the Add button above Menu items, and then clicking OK.

But, you probably want to be able to run this as a macro from a keyboard shortcut. Close the Time.py tab and then exit and relaunch Notepad++. Click on Settings | Shortcut Mapper… and choose the Plugin commands tab. The Time script should be listed here somewhere (in my case, it is usually somewhere around number 27). Highlight it, click Modify and assign it to a shortcut.

I choose to map my Time script to F5, because this mirrors the timestamp functionality built into Windows notepad.exe, but Notepad++ already uses that keystroke for the Run command. I never use the Run command, so I just remove the shortcut from that command by clicking on Settings | Shortcut Mapper… and choosing the Main menu tab, scrolling down to the Run item and either removing the mapping by changing the shortcut to None or changing it to something else, such as Ctrl+F5. For me, Run was located in the Shortcut mapper under Main menu around number 208. Close the Shortcut Mapper and you’re ready to use your new timestamp hotkey.

You can change the datetime formatting using variables! Just modify the contents of the Time.py file located at “%AppData%\Notepad++\plugins\Config\PythonScript\scripts”. See: https://docs.python.org/2/library/time.html#time.strftime