Sublime Forum

How to remove duplicate lines in selection, ignoring tab or spaces in text on that lines


how to remove duplicate lines in selected text in which there may be extra spaces or tabs in the middle of the text, the text itself is identical.

. — space or tab




I wrote this and put it as and bound it in Packages/UserDefault (OSX).sublime-keymap with:

{ "keys": ["ctrl+alt+right"], "command": "delete_spaces_forward"}


import sublime, sublime_plugin
import re
class deleteSpacesForwardCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        view = self.view
        has_selection = len(view.sel()[0]) > 0
        if not has_selection:
            selreg = view.find(r'\s+', view.sel()[0].a, sublime.IGNORECASE)
            sublime.status_message(f'spaces deleted: {len(selreg)}')
            self.view.erase(edit, selreg)


Sorry, I read “empty lines”. For duplicated lines ignoring spaces, you should delete spaces and compare. Is a bit longer code



This is impossible because I can break the structure of the text. Sometimes these spaces are needed and if, for example, there are 100,000 lines, it will be impossible to notice it.

It is necessary to somehow do it in the background.

Or something like this to find:

  1. create copy of the file
  2. remove all spaces,
  3. select duplicates
  4. extract the line numbers with the Copy with line number plugin,
  5. Use something to return the selection to the original.

Go to line has this option but its only go to 1 line at time.

Or: Maby there was plugin to

  1. save the selection
  2. use a plugin to restore it in another sheet. True, I did not find such a plugin.

plugin to save selection,but they do this only on original file and restore selection on other file imposible.
Extras: Plugin



But you don’t need to change the structure of the file. What I suggest is to compare the lines without spaces, no to change them. And if the lines without spaces are equal, delete in the original. I think it should be done with uniq (ignoring spaces or the things you want to ignore) and that’s something quite specific



I will try to do so i think i understand your hack
I did not think of such a way out of the situation, thank you

this is my problem))
in this line space betwen 519 and buttn neded.

tcimg=$cm519 buttn=TDlgCustomColors||TButton2|$

In that line there is additional two space near || and they are not important.

tcimg=$cm519 buttn=TDlgCustomColors || TButton2|$

in general they are all the same…