Say you have the following text:
abc
123
abc
456
789
abc
abc
I want to remove all \"abc\" lines and just keep one. I don\'t mind sorting. T
To add to @Marc.2377 's reply.
If the order is important and you don't care that you just keep the last of the duplicate lines, simply search for the following regexp if you want to only remove duplicte non-empty lines
^(.+\n)(?=(?:.*\n)*?\1)
If you also want to remove duplicate empty lines, use *
instead of +
^(.*\n)(?=(?:.*\n)*?\1)
and replace with nothing.
This will take a line and try to find ahead some more (maybe 0) lines followed by the exact same line taken. It will remove the taken line.
This is just a one-shot regex. No need to spam the replace button.
Not actually in Visual Studio Code, but if it works, it works.
It is not the best answer, as you specified Visual Studio Code, but as I said: If it works, it works :)
It has blisteringly fast native permutation functions.
Edit > Permute Lines > Unique
or ⇧⌘U, andEdit > Permute Selections > Unique
Visual Studio Code is my daily driver. But, I keep Sublime Text on standby for these situations.
Try find and replace with a regular expression.
Find:
^(.+)((?:\r?\n.*)*)(?:\r?\n\1)$
Replace:
$1$2
It is possible to introduce some variance in the first group.
Here is a very interesting extension: Transformer
Features:
For removing duplicate lines:
Removes duplicate lines from the document
Operates on selection or current block if no selection
I haven't played with it much besides the "Unique Lines" command but it seems quite nicely done (including attempting a macro recorder!).
I just had the same issue and found the Visual Studio Code package "Sort lines". See the Visual Studio Code market place for details (e.g. Sort lines).
This package has the option "Sorting lines (unique)", which did it for me. Take care of any white spaces at the beginning/end of lines. They influence whether lines are considered unique or not.