How to Duplicate a Line or a Selection in VS Code

avatar
Borislav Hadzhiev

Last updated: Apr 6, 2024
4 min

banner

# How to Duplicate a Line or a Selection in VS Code

One way to duplicate a line or a selection is VS Code is to use a keyboard shortcut:

on Windows:

  • Shift + Alt + Up arrow key
  • Shift + Alt + Down arrow key

on macOS:

  • Shift + Option + Up arrow key
  • Shift + Option + Down arrow key

on Linux:

  • Ctrl + Shift + Alt + Up arrow key
  • Ctrl + Shift + Alt + Down arrow key
If you use the Up arrow key, you duplicate the line up. If you use the Down arrow key, the line is copied down.

Make sure to place your cursor on the line you want to duplicate before pressing the key combination.

vscode duplicate line or selection

The same approach can be used to duplicate a selection - select multiple lines and press the key combination.

duplicate-selection

Note that the keyboard shortcut often causes issues on 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.

# Manually duplicating a line

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.

duplicate line using ctrl c ctrl v

The same approach can be used to duplicate multiple lines, you just have to select the lines with your mouse prior to copying them.

duplicate selection ctrl c ctrl v

If you place your cursor on a line and press Ctrl + C, you copy the entire line.

duplicate line without selection

You don't have to select it with the mouse if you only need to copy a single line.

# Changing the keyboard shortcut for duplicating lines in VS Code

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:

  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. Type copy line in the search field.

change keyboard shortcut for copy line commands

  1. Double-click on the Copy Line Down command, press a key combination for duplicating a line down and press Enter.
  2. Double-click on the Copy Line Up command, press a key combination for duplicating a line down and press 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.

custom key combination duplicate line

# Using the Command Palette to duplicate a line

You can also use the Command Palette to duplicate a line:

  1. Place your cursor on the line you'd like to duplicate.
  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 copy line and select Copy Line Up or Copy Line Down, depending on if you want to duplicate the line up or down.

duplicate line command palette

# Using the Duplicate Selection command

There is also a Duplicate Selection command.

  1. Select some code or 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 Duplicate Selection and select the option.

duplicate selection command

The Duplicate Selection command:

  • duplicates the current selection and selects the result.
  • when there is no selection, the current line is duplicated.

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:

  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. Type duplicate selection in the search field.

add keyboard shortcut for duplicate selection command

  1. Double-click on the Duplicate selection command, press a key combination and hit 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.

keyboard shortcut for duplicate selection

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.

# 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.