Search code examples
sublimetext3sublimetextsublime-text-plugin

how to remove duplicate characters in sublime?


Before:

我是中国人来自中国,I am Chinese people from China

After:

我是中国人来自,IamChinespolfr

how to remove duplicate characters in sublime ?


Solution

  • A a one-liner for the ST console ctrl+`:

    import collections; content="".join(collections.OrderedDict.fromkeys(view.substr(sublime.Region(0, view.size())))); view.run_command("select_all"); view.run_command("insert", {"characters": content})
    

    If you want to write a plugin press Tools >>> New Plugin... and write:

    import sublime
    import sublime_plugin
    from collections import OrderedDict
    
    
    class RemoveDuplicateCharactersCommand(sublime_plugin.TextCommand):
        def remove_chars(self, edit, region):
            view = self.view
            content = "".join(OrderedDict.fromkeys(view.substr(region)))
            view.replace(edit, region, content)
    
        def run(self, edit):
            view = self.view
            all_sel_empty = True
            for sel in view.sel():
                if sel.empty():
                    continue
                all_sel_empty = False
                self.remove_chars(edit, sel)
            if all_sel_empty:
                self.remove_chars(edit, sublime.Region(0, view.size()))
    

    And create a keybinding in Keybindings - User:

    {
        "keys": ["ctrl+alt+shift+r"],
        "command": "remove_duplicate_characters",
    },
    

    Afterwards you can just select a text and press ctrl+alt+shift+r and the duplicated characters will be removed. If you don't have a selection, it will be applied for the whole view.