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 :-).
Besides the Notepad++ editor, we need to have the Python Script plugin.
EDIT on 2019-05-20 13:36:14. WARNING: the following instructions are valid for notepad ++ ver.7.5. If you use ver.7.6 and later, follow these instructions, then go to the next chapter: What do we have to do?.
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.
Answer yes to subsequent authorization requests for allow the installation of the software.
Now let's create the macro with Python, selecting from the menu Plugin> Python 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'))
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:
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 .
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.
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
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.
|||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!|