Add new features to Notepad++ using Python scripts: keyboard shortcut to insert current time

I have to say, I spend a lot of time daily in Notepad++ text editor for Windows. I keep my “logbook” there. I record what I am doing now and what needs to be done. This allows me not to keep everything in my head and switch the context more efficiently. I can recommend this to everyone. And it is especially useful to note when you started working on a task and when you finished. This gives an understanding of what actually takes your time. I’m not a fan of very strict and formal techniques such as pomodoro, but using some form of time management is good.

Add new features to Notepad++ using Python scripts: keyboard shortcut to insert current time

Recording timestamps manually is inconvenient. It would be much easier to press a key combination and automatically insert the current timestamp into the document. It turned out that this is possible, and even more – you can get the results of any Python script this way!

I installed the PythonScript 1.5.2. plugin in Plugins->Plugins Admin.

I installed the PythonScript 1.5.2. in Plugins->Plugin Admin

I got the new menu Plugins -> Python Script. Next, I needed to add my Python script, that I will execute.

System scripts are in the folder “C:\Program Files\Notepad++\plugins\PythonScript\scripts”. User scripts are in “C:\Users\Alexander\AppData\Roaming\Notepad++\plugins\config\PythonScript\scripts”. You will see this path if you select Plugins -> Python Script -> New Script. So, I add my put_time.py script to the user scripts folder:

import time
time_value = time.strftime('%H:%M')
editor.addText(time_value)

Read more about Editor Object here.

Then I added a menu item to run this script in Plugins -> Python Script -> Configuration:

Then I add a menu item to run this script in Plugins -> Python Script -> Configuration

And now I can run it using the menu Plugins -> Python Script -> Scripts -> put_time

And now I can run it using the menu Plugins -> Python Script -> Scripts -> put_time

But, of course, this is very inconvenient, so I go to Settings -> Shortcut Mapper and add a keyboard shortcut to run Plugin command.

keyboard shortcut to run Plugin command

Now I can insert the timestamp using “Alt + T” Shortcut.

What python binary is actually used? Not the system one! The dlls and libs are in C:\Program Files\Notepad++\plugins\PythonScript

Here is the output of Plugins -> Python Script -> Show Console:

Python 2.7.16 (v2.7.16:413a49145e, Mar 4 2019, 01:37:19) [MSC v.1500 64 bit (AMD64)]
Initialisation took 1000ms
Ready.

Python 2.7 is not good and you will have to install the modules manually. But on the other hand, nothing prevents you to make external calls, for example to run Python3:

import time
import subprocess
time_value = time.strftime('%H:%M')
editor.addText(time_value)
command = '"C:\Program Files\Python38\python.exe" -V' 
editor.addText(subprocess.check_output(command, shell=True).decode('utf-8'))

Output:

09:01Python 3.8.3

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.