Last updated: Apr 6, 2024
Reading time·4 min
One way to duplicate a line or a selection is VS Code is to use a keyboard shortcut:
on Windows:
Shift
+ Alt
+ Up arrow keyShift
+ Alt
+ Down arrow keyon macOS:
Shift
+ Option
+ Up arrow keyShift
+ Option
+ Down arrow keyon Linux:
Ctrl
+ Shift
+ Alt
+ Up arrow keyCtrl
+ Shift
+ Alt
+ Down arrow keyMake sure to place your cursor on the line you want to duplicate before pressing the key combination.
The same approach can be used to duplicate a selection - select multiple lines and press the key combination.
Linux
(esp. Ubuntu). The article has instructions on how to change your keyboard shortcut.If you are on Linux, try using the Numpad Down and Up arrows.
An alternative approach to duplicate a line or selection is to use Ctrl
+ C
to copy the line and Ctrl
+ V
to paste it.
On macOS, use Cmd
+ C
to copy and Cmd
+ V
to paste.
The same approach can be used to duplicate multiple lines, you just have to select the lines with your mouse prior to copying them.
Ctrl
+ C
, you copy the entire line.You don't have to select it with the mouse if you only need to copy a single line.
The keyboard shortcuts for duplicating a line or selection up or down are not very convenient and often cause issues on Ubuntu because the operating system sometimes hides them.
To update your keyboard shortcuts for the Copy Line commands:
Ctrl
+ Shift
+ P
on Windows and Linux.Command
+ Shift
+ P
on macOS.F1
to open the Command Palette.Enter
.Enter
.I've used Ctrl
+ Shift
+ D
for the Copy Line Down command and Ctrl
+
Shift
+ U
for Copy Line Up, however, you can use any other key
combination.
Once you set the keyboard shortcut, you can use it to duplicate a line or a selection.
You can also use the Command Palette to duplicate a line:
Ctrl
+ Shift
+ P
on Windows and Linux.Command
+ Shift
+ P
on macOS.F1
to open the Command Palette.There is also a Duplicate Selection command.
Ctrl
+ Shift
+ P
on Windows and Linux.Command
+ Shift
+ P
on macOS.F1
to open the Command Palette.The Duplicate Selection command:
The Duplicate Selection command doesn't write to the system clipboard.
By default, the Duplicate Selection command doesn't have a keyboard shortcut set.
If you need to set a keyboard shortcut for the command:
Ctrl
+ Shift
+ P
on Windows and Linux.Command
+ Shift
+ P
on macOS.F1
to open the Command Palette.Enter
to confirm.You can also click on the plus to add a keyboard shortcut for the command.
For example, I've set the command to Ctrl
+ Shift
+ Y
.
I've also written an article on how to remove the duplicate lines in VS Code.
If you need to move a line or a selection up or down, check out the following article.
You can learn more about the related topics by checking out the following tutorials: