Created on 28 Nov 2018 ;    Modified on 28 Nov 2018 ;    Translationitalian

Notepad++: add a macro to insert current date and time in text

Insert the current date and time in a text file could be a frequent operation, especially in case you want to document maintenance of a program. The editor file Notepad ++ <https://notepad-plus-plus.org/> _ does not have this as a core functionality. Let's see how to add it by writing a simple macro ... of course in Python :-).

What do we need?

Besides the Notepad++ editor, we need to have the Python Script plugin.

If it isn't already present in our Notepad++ installation, we can load it easily using the Plugin Manager. Select from the menu Plugin> Plugin manager> Show Plugin manager> Available, then check the entry Python script and press the install button.

plugin manager

Answer yes to subsequent authorization requests for allow the installation of the software.

What do we have to do?

Now let's create the macro with Python, selecting from the menu Plugin> Python Script> New Script

new script

This command opens a dialog box in which we put the file name, in my case I called it addDatetime.py.

In the next (empty) editing window we write the following code:

import time

editor.addText (time.strftime ('% Y-% m-% d% H:% M:% S'))

As you might guess, the line ``editor.addText ... `` adds the text of the ``time.strftime ... `` at the current location of the file being edited.

System Message: WARNING/2 (<string>, line 57); backlink

Inline literal start-string without end-string.

System Message: WARNING/2 (<string>, line 57); backlink

Inline literal start-string without end-string.

Save the test and try it by executing the menu item Plugin> Python Script> Scripts> addDatetime. If everything is ok the current date and time will appear in the file being edited. If we did something wrong, let's open the Python console, where we would have any error, such as in the following picture:

error_example

In the lower part of the image we observe the Python Console, opened by selecting the menu item Plugins> Python Script> Show Console. In console the red lines indicate errors occurred while running the script [1].

Configure a function key

To easily run our new macro, we need to configure Notepad++ to launch it with a function key.

To do this, we register the macro with the menu item Plugin> Python Script> Configuration in User Scripts select our script, and with the add button add it to Menu, then OK.

register macro

Restart Notepad ++, select the menu item Configuration> Shortcut Keys> Commands Plugin, look for our macro, possibly with the aid of the filter, and assign it to the desired key. E.g. Ctrl + F5

configure function key

Conclusions

With this work of a few minutes, we will be able to insert current date and time in any text file.

For those with the curiosity to know where is the file that contains the macro, well, in my system it is in C:\Users\user\AppData\Roaming\Notepad++\plugins\config\PythonScript\scripts\addDatetime.py where user is the user's name.


[1]In the case in figure, the error is the lowercase s letter, ending the string '%Y-%m-%d %H:%M:%s': it must be a capital letter!