如何删除sublime中的重复字符?

时间:2016-04-27 08:15:18

标签: sublimetext3 sublimetext sublime-text-plugin

之前:

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

之后:

我是中国人来自,IamChinespolfr

如何删除sublime中的重复字符?

2 个答案:

答案 0 :(得分:4)

ST控制台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})

如果您想编写插件,请按Tools >>> New Plugin...并写:

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()))

Keybindings - User中创建一个键绑定:

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

之后您只需选择一个文字并按ctrl+alt+shift+r即可删除重复的字符。如果您没有选择,它将应用于整个视图。

答案 1 :(得分:1)

  1. 打开Find菜单
  2. 选择Replace...
  3. 确保已启用Regular expression模式
  4. Find What:中,输入(.)(.*?)\1
  5. Replace With:中,输入$1$2
  6. 点击Replace All
  7. 重复,直到没有更多匹配/重复字符