Though it worked fine for me, using TED Notepad, I wanted to resolve this one using Notepad++ too.
It won't be possible to use the, quite limited, macro feature of Npp, so I used a scripting plugin: Notepad++ Python Script
[One time only]- Install the Notepad++ Python Script plugin, using the MSI installer,
not using the plugin manager. It can be downloaded from
here. Close Notepad++ before the installer is run.
- Open Notepad++
- Check if Python Script is working correctly by opening the console: Plugins/Python Script/Show Console, en verifying that it shows the Python version, something like this:
Python 2.7.6-notepad++ r2 (default, Apr 21 2014, 19:26:54) [MSC v.1600 32 bit (Intel)]
Initialisation took 125ms
Ready.
- Choose Plugins/Python Script/New Script, type a filename: sort-by-length.py, and press Save. The file is created and opened in Notepad++
- Paste this script into sort-by-length.py:
# Ath: Found this in the notepad-plus forum on sourceforge.net, modified as suggested in the comments there
editor.beginUndoAction()
if editor.getSelText(): # grab the lines
linee = editor.getSelText().splitlines()
else:
linee = editor.getText().splitlines()
sorted_lines=sorted(linee,key=lambda x:len(x)) # add parameter reverse=True, to sort descending by length
editor.clearAll() # wipe lines in editor
for line in sorted_lines:
print(line) # put all lines back in the editor
editor.endUndoAction()
- Make sure the code is not indented (so each line starting on column 1) except for lines 4, 6 and 10 as they
must be indented.
- Save the file
- The script should now be available in the menu Plugins/Python Script/Scripts/ (don't select it, it will execute immediately!)
[Process a file]- Open the file to sort, and make sure it is the selected file
- Select Plugins/Python Script/Scripts/sort-by-length
- The file will be emptied and filled with all lines again, shortest first (or longest first if reversed=True was set). It may take some time, as Python (Script) doesn't seem very fast in this configuration
- If an error occurs during processing, it will be shown in the Python Script console (see above how to open that)
- Save the file
Done
Edit: Added sentence about the code needing to be at column 1