Skip to content

Instantly share code, notes, and snippets.

@Alex-Just
Last active May 23, 2024 21:03
Show Gist options
  • Save Alex-Just/e9e100f2dc41c5b03827 to your computer and use it in GitHub Desktop.
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.
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)
[
{
"id": "edit",
"children":
[
{ "command": "count_duplicates" }
]
}
]
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
@void285
Copy link

void285 commented Apr 25, 2018

Thank you the this wonderfull tool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment