Last active
May 23, 2024 21:03
-
-
Save Alex-Just/e9e100f2dc41c5b03827 to your computer and use it in GitHub Desktop.
Sublime Text plugin "Count Duplicates". Adds command in the Edit main menu that counts number of duplicate rows in a file and prints it in the first left column.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
import sublime | |
import sublime_plugin | |
class CountDuplicatesCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
v = self.view | |
result = [] | |
allcontent = sublime.Region(0, v.size()) | |
lines = sorted(v.substr(allcontent).split('\n')) | |
for key, rows in itertools.groupby(lines): | |
result.append((sum(1 for r in rows), key)) | |
result.sort(key=lambda tup: tup[0], reverse=True) | |
output = '\n'.join([str(r[0]) + ' ' + r[1] for r in result]) | |
self.view.replace(edit, allcontent, output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 | |
1 | |
1 result.append((sum(1 for r in rows), key)) | |
1 allcontent = sublime.Region(0, v.size()) | |
1 for key, rows in itertools.groupby(lines): | |
1 lines = sorted(v.substr(allcontent).split('\n')) | |
1 output = '\n'.join([str(r[0]) + ' ' + r[1] for r in result]) | |
1 result = [] | |
1 result.sort(key=lambda tup: tup[0], reverse=True) | |
1 self.view.replace(edit, allcontent, output) | |
1 v = self.view | |
1 def run(self, edit): | |
1 class CountDuplicatesCommand(sublime_plugin.TextCommand): | |
1 import itertools | |
1 import sublime | |
1 import sublime_plugin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you the this wonderfull tool!