Sublime Forum

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

#1

Hi.
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

Text.text.text
Text…text…text

0 Likes

#2

I wrote this and put it as deleteSpacesFw.py 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)
0 Likes

#3

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

0 Likes

#4

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
PowerCursors

0 Likes

#5

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

0 Likes

#6

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
SMART!!!

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…

0 Likes