This post covers different examples of how to make and remove duplicate lines in Visual Studio Code

It contains Multiple ways to Create and delete a duplicate line or rows in a text file opened in VSCode.

How to create a duplicate line in VSCode?

You can create a duplicate line in multiple ways.

  • using Copy and paste You can use the normal copy and paste option to duplicate the lines. First, select the line you want to duplicate using the below command. Ctrl +C for copy, Ctrl + V for paste.

  • Inbuilt Copy Line Up and Down feature

    Go to Selection Menu > Copy Line Up or Copy Line Down Arrow key

  • Using shortcut commands

    • Windows: Shift + Alt+ Down key and Shift + Alt+ Up key
    • Linux: Ctrl + Shift + Alt + Down key and Ctrl + Shift + Alt + Up key
    • MacOS: Shift + Option + Down key and Shift + Option + Up key

by default, these shortcut commands are bound to the below properties in Key bindings with File > Preferences > Keyboard shortcuts

  • editor.action.copyLinesDownAction
  • editor.action.copyLinesUpAction So you can customize these bindings in shortcuts or modify them in the keybindings.json file with these properties.

How to delete duplicate rows or lines in a visual Studio Code?

Let’s have a first.txt file opened in text content.

abc
def
abc
abc
def
abc

And output:

abc
def

One approach is to hover over a line and select a text, then remove the duplicate lines manually.

It’s simple for files that aren’t very large. For a larger file with many lines of text, however, it is a time-consuming job.

There are multiple ways you can remove duplicate or blank lines.

In the builtin Delete Duplicate Lines feature

This feature is available as inbuilt vs code.

  • Select Views Menu + Command palette or use the following shortcut commands

    • Windows: hotkeys: Ctrl + Shift + P
    • Mac: shortcuts: ⌘ + Shift +P
  • It opens the command palette and types delete in the search box and selects the Delete Duplicate Lines option

Vscode delete duplicate lines or rows

  • It removes Duplicate lines from the current file.

Find and Search with regular expression

Find and search is an inbuilt feature in the editor.

  • Open Edit > Replace or use Shortcut Ctrl + H in Windows, ⌘ + Alt + Fon Mac.

  • It opens Replace popup upper right corner

  • Select and type below

    • first find box with ^(.*)(\n\1)+$ - for duplicate lines
    • Second input box with $1
    • Select Search Mode as Regular Expression(.*) or ShortCut Alt + R in Windows, ⌘ + Alt + R in Mac
    • Click on Replace All button or use Ctrl + Alt + Enter
  • It removes duplicate lines from the file.

Use the Transformer extension

Transformer is a custom extension that deletes duplicate lines for an entire file or selection of a file apart from other things such as filtering lines and combining lines' end and beginning spaces and tabs and deleting comments.

Install the Transformer Package.

  • Go to Extensions icon and Left Side vertical bar

  • type “transformer” and Select Transformer extension

  • Once Transformer extension is installed, Go to the next steps.

  • Go to Command Palette with one of the options - Select Views Menu + Command palette or - use following shortcut commands - Windows: hotkeys: Ctrl + Shift + P - Mac: shortcuts: ⌘ + Shift +P

  • It opens the command palette and type transform in the search box and selects the transform: Unique Lines option

  • It removes duplicate lines or rows