山茶树和葡萄树
山茶树和葡萄树

Reputation: 2368

how to remove duplicate characters in sublime?

Before:

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

After:

我是中国人来自,IamChinespolfr

how to remove duplicate characters in sublime ?

Upvotes: 2

Views: 954

Answers (2)

Keith Hall
Keith Hall

Reputation: 16065

  1. Open the Find menu
  2. Select Replace...
  3. Ensure the Regular expression mode is enabled
  4. In Find What:, type (.)(.*?)\1
  5. In Replace With:, type $1$2
  6. Click Replace All
  7. Repeat until there are no more matches/duplicate characters

Upvotes: 1

r-stein
r-stein

Reputation: 4847

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.

Upvotes: 5

Related Questions