Search code examples
windowswindows-10sublimetext3

Remove duplicates lines but not blank lines in Sublime Text


I know that you can remove duplicate lines by doing Edit>Permute Lines>Unique. But that will remove blank(empty) lines. I would like not to remove them, so blank lines will stay(essentially empty space).


Solution

  • You can do this using Permute Selections. First, open the Find menu (CtrlF or Find → Find…) and make sure the Regex, Wrap, and Highlight matches buttons are selected. In the search field, enter ^.+\n. This matches the beginning of the line ^, 1 or more characters of any type .+, and the newline character(s). Therefore, it will select any line that is NOT just a newline. Note that this will select lines that contain only whitespace, for example a tab character followed by a newline.

    Regex search

    Next, hit the Find All button to select each line individually.

    Find all

    Finally, select Edit → Permute Selections → Unique and all duplicate lines will be erased, leaving all blank lines behind.

    Unique lines plus blank lines