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. The result should be like this:
abc
123
456
789
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. The result should be like this:
abc
123
456
789
Not actually in vscode, but if it works, it works.
Data
tabRemove Duplicates
(somewhat in the middle of the bar)OK
to remove duplicates.Not the best answer, as you specified vscode, but as I said: If it works, it works :)
Try find & replace with regexp.
Find :
^(.+)((?:\r?\n.*)*)(?:\r?\n\1)$
Replace :
$1$2
It is possible to introduce some variance in the first group.
If the order of lines is not important
Sort lines alphabetically, if they aren't already, and perform these steps:
(based on this related question: How do I find and remove duplicate lines from a file using Regular Expressions?)
Control+F
Toggle "Replace mode"
Toggle "Use Regular Expression" (the icon with the
.*
symbol)In the search field, type
^(.*)(\n\1)+$
In the "replace with" field, type
$1
Click ("Replace All").
If the order of lines is important so you can't sort
In this case, either resort to a solution outside VS Code (see here), or - if your document is not very large and you don't mind spamming the Replace All button - follow the previous steps, but in steps 4 and 5, enter these:
(based on Remove specific duplicate lines without sorting)
search:
((^[^\S$]*?(?=\S)(?:.*)+$)[\S\s]*?)^\2$(?:\n)?
replace with:
$1
and then click the "Replace All" button as many times as there are duplicate occurrences.
You'll know it's enough when the line count stops decreasing when you click the button. Navigate to the last line of the document to keep an eye on that.
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 regexp
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.
Install DupChecker extension, hit F1, type "Check Duplicates".
It will check dupes and ask if you want to remove them.
Just had the same issue and found the VSCode package "Sort lines", see the VSCode market place for details (e.g. https://marketplace.visualstudio.com/items?itemName=Tyriar.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.