sort file removing duplicates possible?
one of the functions I was using in my previous editor was
“sort file removing duplicates”,
is this possible using np++?
A direct command for this does not exist (natively), but you can record and save a macro to do it!
Sort Lines Lexicographically Ascending
- Press Ctrl+h (to invoke the replace dialog box).
- In the
Find whatfield, type
- Clear out anything that appears in the
Replace withfield; make it empty!
- Set the
- Uncheck all other checkboxes in the Replace dialog.
- Press the
Save Currently Recorded Macro; give it a good name, maybe assign a keyboard shortcut.
To run it, either use the keyboard shortcut (if you assigned one), or use the
Macromenu and select the menu entry for the name you gave the macro.
For the technical, detail-oriented reader, what happens internally is that a block like this is inserted into the
<Macro name="test sort and del dupe lines" Ctrl="no" Alt="no" Shift="no" Key="0"> <Action type="2" message="0" wParam="42059" lParam="0" sParam="" /> <Action type="3" message="1700" wParam="0" lParam="0" sParam="" /> <Action type="3" message="1601" wParam="0" lParam="0" sParam="(?-s)^(.*)(?:\R)(?s)(?=.*^\1\R)" /> <Action type="3" message="1625" wParam="0" lParam="2" sParam="" /> <Action type="3" message="1602" wParam="0" lParam="0" sParam="" /> <Action type="3" message="1702" wParam="0" lParam="512" sParam="" /> <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" /> </Macro>
If this (or ANY) posting was useful, don’t post a “thanks”, just upvote ( click the
^ 0 varea on the right ).
The above macro operates on the entire file in the active editor window when it is run. Perhaps even more useful is to have it operate only on the lines making up a selection. This is an easy modification. Simply select/highlight a block of text before starting to record the macro. This will cause the
In selectioncheckbox on the Replace tab of the
Finddialog to be enabled. Check this checkbox before running the
Replace All(while recording the macro).
shortcuts.xmland change the message=“1702” line to contain lParam=“640” instead of 512. This adds in the check in the
In selectionbox to the already-present
Direction: Downsetting for the replacement. The various meanings for the 1702 value, as well as the encodings for all of the lines in the macro section of shortcuts.xml may be found here: http://docs.notepad-plus-plus.org/index.php/Editing_Configuration_Files#Search_.2F_Replace_encoding Note that while researching this I discovered that this documentation appears to be incorrect for the search direction–it seems that downward appears to be 512, not upwards. Side note: 640 = 512 + 128, meaning that both a downward search and an in-selection search are in effect when the replacement runs within the macro.
Note that if this “selection” change to the macro is implemented, it is necessary to select beforehand the entire file (easy way:
ctrl+a), if the action is desired upon the entire file contents. This is a change from the first version of the macro, where it acted upon the whole file by default and nothing needed to be selected.
Also, be careful when specifying the range of lines in your selection. It is very easy to get one more line than intended at the tail end of your range, if you do the natural thing (with the mouse, at least) and select from a point earlier in the file to a later point. I find there is less confusion about this if I remember to select from a later point (greater line number) to an earlier point (lesser line number).
As an example, if your intended sort selection in the following is lines
bbb, and you select like this:
you will pull (undesired) line
aaainto the sort as well (the fact that you can see the thin caret is on the
aaaline is the key) and the sort will result in the undesired:
However, selecting from the bottom up (start on
bbband making the selection cover
ddd) makes it clearer to see what will be sorted:
The reality of the way it works is that any line with at least a partial selection will be included, so even this works just as well:
The most recent two selections shown yield the following sort result, as desired: