Learn to Code via Tutorials on Repl.it!

← Back to all posts
Notepad++ Python Script
gwhite5 (1)

To automate text editing tasks, I daily use Python scripts I've written for the Python Script plugin of the Notepad++ editor. I had nothing to do with the creation of the plugin, but here I'm providing information to help you in writing your own scripts.

Scintilla is an editor engine at the core of Notepad++. The Python Script plugin gives us access to the Scintilla API. There is no Linux version of Notepad++ but there is an editor in Linux based on Scintilla: Geany.

Python Script Plugin:
http://npppythonscript.sourceforge.net/

Python Script documentation:
http://npppythonscript.sourceforge.net/docs/latest/index.html

Python Script forum:
https://sourceforge.net/p/npppythonscript/discussion/

Scintilla documentation:
https://www.scintilla.org/ScintillaDoc.html

In Notepad++ menu, “Settings / Shortcut Mapper / Scintilla commands” shows some key mappings of Scintilla commands.

To evaluate selected text as a Python expression, put it in the paste buffer, and display the result:

   OutputStr = str(eval(editor.getSelText()))
   editor.copyText(OutputStr)
   notepad.messageBox(OutputStr, "EvalSelect", 0)

To add up all the numbers that can be parsed from selected text, put it in the paste buffer, and display the result:

   import re

   nums = re.findall("[-+]?\d+[\.]?\d*[eE]?[-+]?\d*", 
   editor.getSelText())
   output = 1
   for n in nums:
       try:
           x = int(n)
       except ValueError:
           x = float(n)
       output += x
   OutputStr = str(output)
   editor.copyText(OutputStr)
   notepad.messageBox("Sum: "+OutputStr, "BlockMath", 0)