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.
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 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:
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.
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
Hi! My name is Alexander and I am a Vulnerability Management specialist. You can read more about me here. Currently, the best way to follow me is my Telegram channel @avleonovcom. I update it more often than this site. If you haven’t used Telegram yet, give it a try. It’s great. You can discuss my posts or ask questions at @avleonovchat.
А всех русскоязычных я приглашаю в ещё один телеграмм канал @avleonovrus, первым делом теперь пишу туда.