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
orCopy Line Down
Arrow keyUsing shortcut commands
- Windows:
Shift + Alt+ Down
key andShift + Alt+ Up
key - Linux:
Ctrl + Shift + Alt + Down
key andCtrl + Shift + Alt + Up
key - MacOS:
Shift + Option + Down
key andShift + Option + Up
key
- Windows:
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
- Windows: hotkeys:
It opens the
command palette
typesdelete
in the search box and selects theDelete Duplicate Lines
option
- 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 ShortcutCtrl + H
in Windows,⌘ + Alt + F
on Mac.It opens the
Replace
popup upper right cornerSelect and type below
- first find box with
^(.*)(\n\1)+$
- for duplicate lines - Second input box with
$1
- Select Search Mode as
Regular Expression(.*)
or ShortCutAlt + R
in Windows,⌘ + Alt + R
in Mac - Click on the
Replace All
button or useCtrl + Alt + Enter
- first find box with
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 combining lines’ end and beginning spaces and tabs and deleting comments.
Install the Transformer Package.
Go to the Extensions icon and Left Side vertical bar
type “transformer” and Select
Transformer
extensionOnce the
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 the following shortcut commands - Windows: hotkeys:Ctrl + Shift + P
- Mac: shortcuts:⌘ + Shift +P
It opens the
command palette
and typetransform
in the search box and selects thetransform: Unique Lines
optionIt removes duplicate lines or rows