Visual Studio Code
Programming Tips
Coding
Software Development
Text Editing

How do I duplicate a line or selection within Visual Studio Code?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

To duplicate a line in VS Code, place your cursor on the line and press Shift+Alt+Down (Windows/Linux) or Shift+Option+Down (macOS). The line is copied immediately below the original. To duplicate upward, use Shift+Alt+Up or Shift+Option+Up. If you select multiple lines first, the entire selection is duplicated.

Quick Reference

ActionWindows / LinuxmacOS
Duplicate line downShift+Alt+DownShift+Option+Down
Duplicate line upShift+Alt+UpShift+Option+Up
Duplicate selection downSelect text, then Shift+Alt+DownSelect text, then Shift+Option+Down
Copy line (to clipboard)Ctrl+C (no selection)Cmd+C (no selection)
Cut line (to clipboard)Ctrl+X (no selection)Cmd+X (no selection)
Move line downAlt+DownOption+Down
Move line upAlt+UpOption+Up

Note: You do not need to select the entire line. Just place the cursor anywhere on the line and use the shortcut. VS Code duplicates the full line automatically.

Duplicating a Single Line

Place your cursor on any line and press Shift+Alt+Down. The line is duplicated below:

python
1# Before: cursor on line 1
2prices = [10, 20, 30, 40, 50]
3
4# After Shift+Alt+Down:
5prices = [10, 20, 30, 40, 50]
6prices = [10, 20, 30, 40, 50]

The cursor moves to the new line, so you can immediately start editing the duplicate.

Duplicating Multiple Lines

Select two or more lines (you do not need to select complete lines; a partial selection works), then press Shift+Alt+Down:

javascript
1// Before: select both lines
2const firstName = "Alice";
3const lastName = "Smith";
4
5// After Shift+Alt+Down:
6const firstName = "Alice";
7const lastName = "Smith";
8const firstName = "Alice";
9const lastName = "Smith";

Practical Examples

Building HTML lists quickly

Type one list item, then duplicate it multiple times:

html
1<!-- Start with one item -->
2<li class="nav-item"><a href="/home">Home</a></li>
3
4<!-- Duplicate 3 times with Shift+Alt+Down, then edit each -->
5<li class="nav-item"><a href="/home">Home</a></li>
6<li class="nav-item"><a href="/about">About</a></li>
7<li class="nav-item"><a href="/blog">Blog</a></li>
8<li class="nav-item"><a href="/contact">Contact</a></li>

Creating test cases

python
1# Duplicate the test template and modify each copy
2def test_add_positive_numbers(self):
3    self.assertEqual(add(2, 3), 5)
4
5def test_add_negative_numbers(self):
6    self.assertEqual(add(-2, -3), -5)
7
8def test_add_mixed_numbers(self):
9    self.assertEqual(add(-2, 3), 1)
10
11def test_add_zeros(self):
12    self.assertEqual(add(0, 0), 0)

SQL column definitions

sql
1-- Duplicate and edit each column definition
2CREATE TABLE employees (
3    id          INT PRIMARY KEY AUTO_INCREMENT,
4    first_name  VARCHAR(100) NOT NULL,
5    last_name   VARCHAR(100) NOT NULL,
6    email       VARCHAR(255) NOT NULL UNIQUE,
7    department  VARCHAR(100) NOT NULL,
8    hire_date   DATE NOT NULL,
9    salary      DECIMAL(10,2) NOT NULL
10);

CSS rule duplication

css
1/* Duplicate the base rule and modify for each variant */
2.btn-primary {
3    background-color: #007bff;
4    border-color: #007bff;
5    color: #ffffff;
6}
7
8.btn-secondary {
9    background-color: #6c757d;
10    border-color: #6c757d;
11    color: #ffffff;
12}
13
14.btn-danger {
15    background-color: #dc3545;
16    border-color: #dc3545;
17    color: #ffffff;
18}

Combining Duplicate with Multi-Cursor Editing

After duplicating lines, you often need to change the same position across all copies. Multi-cursor editing pairs perfectly with duplication:

  1. Duplicate a line 3 times.
  2. Select the word you want to change.
  3. Press Ctrl+D (Windows/Linux) or Cmd+D (macOS) to select the next occurrence.
  4. Keep pressing Ctrl+D until all occurrences are selected.
  5. Type the replacement text. All cursors update simultaneously.

Alternatively, hold Alt and click at each position to place cursors manually.

javascript
1// After duplicating "const item1 = getItem(1);" four times
2// Use Ctrl+D to select and rename each "item1" and "1"
3const item1 = getItem(1);
4const item2 = getItem(2);
5const item3 = getItem(3);
6const item4 = getItem(4);

Customizing the Shortcut

If you prefer a different key binding, you can remap the command:

  1. Open the Command Palette: Ctrl+Shift+P / Cmd+Shift+P.
  2. Type Open Keyboard Shortcuts (JSON) and select it.
  3. Add an entry:
json
1[
2  {
3    "key": "ctrl+shift+d",
4    "command": "editor.action.copyLinesDownAction",
5    "when": "editorTextFocus && !editorReadonly"
6  },
7  {
8    "key": "ctrl+shift+u",
9    "command": "editor.action.copyLinesUpAction",
10    "when": "editorTextFocus && !editorReadonly"
11  }
12]

The underlying VS Code commands are:

Command IDAction
editor.action.copyLinesDownActionDuplicate line/selection downward
editor.action.copyLinesUpActionDuplicate line/selection upward
editor.action.moveLinesDownActionMove line/selection down (no copy)
editor.action.moveLinesUpActionMove line/selection up (no copy)

These shortcuts complement line duplication and speed up code editing:

ActionWindows / LinuxmacOS
Move line upAlt+UpOption+Up
Move line downAlt+DownOption+Down
Delete lineCtrl+Shift+KCmd+Shift+K
Insert line belowCtrl+EnterCmd+Enter
Insert line aboveCtrl+Shift+EnterCmd+Shift+Enter
Indent lineCtrl+]Cmd+]
Outdent lineCtrl+[Cmd+[
Toggle line commentCtrl+/Cmd+/
Toggle block commentShift+Alt+AShift+Option+A
Select current lineCtrl+LCmd+L
Join linesN/A (install extension)Ctrl+J

Comparison with Other Editors

EditorDuplicate line shortcut
VS CodeShift+Alt+Down / Shift+Option+Down
IntelliJ / WebStormCtrl+D
Sublime TextCtrl+Shift+D / Cmd+Shift+D
AtomCtrl+Shift+D / Cmd+Shift+D
Vimyyp (yank line + paste)
EmacsC-a C-k C-k C-y C-y
Notepad++Ctrl+D

If you are coming from IntelliJ or Sublime Text and want to keep Ctrl+Shift+D, remap it using the keybindings JSON shown above.

Common Pitfalls

  • Using Ctrl+Shift+D instead of Shift+Alt+Down. In VS Code, Ctrl+Shift+D opens the Debug panel, not duplicate line. This catches many developers coming from other editors. Remap if needed.
  • Duplicating with an active selection vs. no selection. With no selection, the entire line is duplicated. With a partial selection within a single line, VS Code still duplicates the entire line (not just the selected text). This is by design.
  • Clipboard is not affected. Line duplication does not touch the clipboard. Your previous Ctrl+C content is preserved.
  • Formatting drift. After duplicating, indentation is preserved from the original. If you duplicate into a different nesting level, you may need to re-indent with Ctrl+] or Ctrl+[.
  • Remote SSH and WSL. Keybindings work the same in Remote-SSH and WSL sessions. If they do not respond, check that the keyboard shortcut is not intercepted by the terminal.

Summary

  • Duplicate a line down with Shift+Alt+Down (Windows/Linux) or Shift+Option+Down (macOS).
  • Duplicate upward with Shift+Alt+Up / Shift+Option+Up.
  • No selection needed for a single line. Select multiple lines to duplicate a block.
  • Pair with Ctrl+D multi-cursor to edit duplicated lines in parallel.
  • Move lines (without copying) with Alt+Up and Alt+Down.
  • Delete a line entirely with Ctrl+Shift+K.
  • Remap shortcuts in keybindings.json using editor.action.copyLinesDownAction.

Course illustration
Course illustration

All Rights Reserved.