How to remove Duplicate Lines in Visual Studio Code

avatar
Borislav Hadzhiev

Last updated: Apr 6, 2024
3 min

banner

# How to remove Duplicate Lines in Visual Studio Code

To remove the duplicate lines in Visual Studio Code:

  1. Select the text.
  2. Press:
  • Ctrl + Shift + P on Windows and Linux
  • Command + Shift + P on macOS
Note: you can also press F1 to open the Command Palette.
  1. Type Delete Duplicate Lines and select the option.

select delete duplicate lines

Here is a short clip that demonstrates the process.

delete duplicate lines

Make sure to select the text prior to running the Delete Duplicate Lines command, otherwise it does nothing.

# Setting a keyboard shortcut for the Delete Duplicate lines command

If you need to set a keyboard shortcut for the Delete Duplicate lines command:

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type Keyboard Shortcuts and select Preferences: Open Keyboard Shortcuts.

preferences open keyboard shortcuts

  1. Search for remove duplicate lines and double-click on the option.

keyboard shortcut remove duplicate lines

You can also click on the plus icon to add a keyboard shortcut.

  1. Specify a key combination and press Enter to confirm.

For example, I've bound the command to Ctrl + F4.

remove duplicate lines custom keyboard shortcut

Here is a short clip in which I use the custom keyboard shortcut.

delete duplicate lines using custom keyboard shortcut

# Remove duplicate lines in VS Code if the order is not important

You can also use a regular expression to remove the duplicate lines.

The following example assumes that the order of the lines in the file is not important.

Use the next subheading if you need to remove the duplicate lines with a regex where the order is important.
  1. If the lines in the file are not already sorted alphabetically, sort them:

First, select the lines.

Then, press:

  • Ctrl + Shift + P on Windows and Linux
  • Command + Shift + P on macOS

Type sort lines and select Sort Lines Ascending.

sort lines ascending

Here is a short clip that demonstrates the process.

sort lines ascending

  1. Press Ctrl + F on Windows and Linux or Cmd + F on macOS to show the search field.

  2. Type the following regular expression in the Find field - ^(.*)(\n\1)+$.

find-field
^(.*)(\n\1)+$

remove duplicate lines using regex

  1. Click on the arrow to expand the Replace field and enable regular expression search by clicking on the . * button.

  2. Type $1 in the Replace field.

replace-field
$1
  1. Click on the Replace All button.

Here is a short clip that demonstrates the process.

remove duplicate lines using regex

# Remove duplicate lines in VS Code if the order is important

If you need to remove the duplicate lines and the order of lines is important:

  1. Press Ctrl + F on Windows and Linux or Cmd + F on macOS to show the search field.

  2. Click on the arrow to expand the Replace field and enable regular expression search by clicking on the . * button.

remove duplicate lines with regex where order is important

  1. Paste the following regular expression in the Find field - ((^[^\S$]*?(?=\S)(?:.*)+$)[\S\s]*?)^\2$(?:\n)?.
find-field
((^[^\S$]*?(?=\S)(?:.*)+$)[\S\s]*?)^\2$(?:\n)?
  1. Type $1 in the Replace field.
replace-field
$1
  1. Click on the Replace All button.
NOTE: If there are many duplicate lines in the file, you will have to click on the Replace all button multiple times.

You'll know that all duplicate lines have been replaced when the regex has no matches and the text No results is shown.

no results shown

Here is a short clip that demonstrates the process.

remove duplicate lines and preserve order

This approach removes the duplicate lines and preserves the order.

I've also written an article on how to remove the trailing spaces automatically in VS Code

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.