Skip to content

Instantly share code, notes, and snippets.

@marcosvpj
Last active March 22, 2024 13:41
Show Gist options
  • Star 36 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save marcosvpj/f04116e5443284ccb5f14f3c443a2d0d to your computer and use it in GitHub Desktop.
Save marcosvpj/f04116e5443284ccb5f14f3c443a2d0d to your computer and use it in GitHub Desktop.
How to remove duplicate lines in Visual Studio Code?

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: https://stackoverflow.com/q/1573361/3258851)

  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 https://stackoverflow.com/q/48722064/3258851)

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.

https://stackoverflow.com/questions/37992493/how-to-remove-duplicate-lines-in-visual-studio-code

@janschoepke
Copy link

For all those who want Visual Studio Code to sort the opened file alphabetically line by line (required for the first variant described above), here is a useful VS Code extension:

https://marketplace.visualstudio.com/items?itemName=Tyriar.sort-lines

instructions:

  1. install the above mentioned plugin
  2. Press F1
  3. enter "sort"
  4. select "sort lines (ascending, case insensitive)"
  5. enjoy!

@woctezuma
Copy link

woctezuma commented Jun 20, 2020

I am pretty confident that sorting lines is a feature available by default, without requiring an add-on.

sorting lines

See: microsoft/vscode#536 (comment)

@justdamilare
Copy link

This is super helpful. Thank you very much 😸

@woctezuma
Copy link

As mentioned on StackOverflow, this is now included in VSCode.

remove duplicate lines

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment