How can I remove duplicate lines in Visual Studio Code?

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

Solution 1:

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?)

  1. Control+F

  2. Toggle "Replace mode"

  3. Toggle "Use Regular Expression" (the icon with the .* symbol)

  4. In the search field, type ^(.*)(\n\1)+$

  5. In the "replace with" field, type $1

  6. Click the Replace All button ("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)

Caution: Blocks for files with too many lines (1000+); may cause VS Code to crash; may introduce blank lines in some cases.

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

Solution 2:

Coming in vscode v1.62 is a command to eliminate duplicate lines from a selection:

Delete Duplicate Lines in the Command Palette

or

editor.action.removeDuplicateLines as a command in a keybinding

(there is no default keybinding for this command)


Here is a very interesting extension: Transformer

Features:

  • Unique Lines As New Document
  • Unique Lines

  • Align CSV
  • Align To Cursor
  • Compact CSV
  • Copy To New Document
  • Count Duplicate Lines As New Document
  • Encode / Decode
  • Filter Lines As New Document
  • Filter Lines
  • Join Lines
  • JSON String As Text
  • Lines As JSON String Array
  • Normalize Diacritical Marks
  • Randomize Lines
  • Randomize Selections
  • Reverse Lines
  • Reverse Selections
  • Rotate Backward Selections
  • Rotate Forward Selections
  • Select Highlights
  • Select Lines
  • Selection As JSON String
  • Sort Lines By Length
  • Sort Lines
  • Sort Selections
  • Split Lines After
  • Split Lines Before
  • Split Lines
  • Trim Lines
  • Trim Selections

Unique Lines

Removes duplicate lines from the document Operates on selection or current block if no selection

Unique Lines As New Document

Unique lines are opened in a new 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!).