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
| Action | Windows / Linux | macOS |
| Duplicate line down | Shift+Alt+Down | Shift+Option+Down |
| Duplicate line up | Shift+Alt+Up | Shift+Option+Up |
| Duplicate selection down | Select text, then Shift+Alt+Down | Select 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 down | Alt+Down | Option+Down |
| Move line up | Alt+Up | Option+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:
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:
Practical Examples
Building HTML lists quickly
Type one list item, then duplicate it multiple times:
Creating test cases
SQL column definitions
CSS rule duplication
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:
- Duplicate a line 3 times.
- Select the word you want to change.
- Press
Ctrl+D(Windows/Linux) orCmd+D(macOS) to select the next occurrence. - Keep pressing
Ctrl+Duntil all occurrences are selected. - Type the replacement text. All cursors update simultaneously.
Alternatively, hold Alt and click at each position to place cursors manually.
Customizing the Shortcut
If you prefer a different key binding, you can remap the command:
- Open the Command Palette:
Ctrl+Shift+P/Cmd+Shift+P. - Type Open Keyboard Shortcuts (JSON) and select it.
- Add an entry:
The underlying VS Code commands are:
| Command ID | Action |
editor.action.copyLinesDownAction | Duplicate line/selection downward |
editor.action.copyLinesUpAction | Duplicate line/selection upward |
editor.action.moveLinesDownAction | Move line/selection down (no copy) |
editor.action.moveLinesUpAction | Move line/selection up (no copy) |
Related Line Manipulation Shortcuts
These shortcuts complement line duplication and speed up code editing:
| Action | Windows / Linux | macOS |
| Move line up | Alt+Up | Option+Up |
| Move line down | Alt+Down | Option+Down |
| Delete line | Ctrl+Shift+K | Cmd+Shift+K |
| Insert line below | Ctrl+Enter | Cmd+Enter |
| Insert line above | Ctrl+Shift+Enter | Cmd+Shift+Enter |
| Indent line | Ctrl+] | Cmd+] |
| Outdent line | Ctrl+[ | Cmd+[ |
| Toggle line comment | Ctrl+/ | Cmd+/ |
| Toggle block comment | Shift+Alt+A | Shift+Option+A |
| Select current line | Ctrl+L | Cmd+L |
| Join lines | N/A (install extension) | Ctrl+J |
Comparison with Other Editors
| Editor | Duplicate line shortcut |
| VS Code | Shift+Alt+Down / Shift+Option+Down |
| IntelliJ / WebStorm | Ctrl+D |
| Sublime Text | Ctrl+Shift+D / Cmd+Shift+D |
| Atom | Ctrl+Shift+D / Cmd+Shift+D |
| Vim | yyp (yank line + paste) |
| Emacs | C-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+Dopens 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+Ccontent 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+]orCtrl+[. - 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) orShift+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+Dmulti-cursor to edit duplicated lines in parallel. - Move lines (without copying) with
Alt+UpandAlt+Down. - Delete a line entirely with
Ctrl+Shift+K. - Remap shortcuts in
keybindings.jsonusingeditor.action.copyLinesDownAction.

