Duplicating a line is one of those micro-actions developers perform dozens of times every hour — yet most developers rely on Ctrl+C / Ctrl+V when a dedicated vscode duplicate line shortcut would do the same job in a single keystroke, without disturbing the clipboard. If you have ever wondered how to master the vs code duplicate line command across Windows, macOS, and Linux — and how to verify that a duplicated block of code diverged only where you intended — this complete guide covers every method. While you are levelling up your VS Code productivity, also consider reading our guide on VS Code collapse all and format shortcuts and how to compare two files in VS Code — both are equally essential daily habits.
Why Duplicate Lines (and Why a Shortcut Matters)
Code duplication in the sense of architectural code smell is something to avoid. But intentional, tactical line duplication is the opposite: it is a precision editing tool. You reach for it when:
- Scaffolding repetitive structure. CSS rule blocks, JSON keys, SQL column definitions, and HTML list items often follow identical patterns. Duplicating a line and editing the variable part is faster than typing from scratch.
- Creating variation pairs. Writing a function and an overloaded version, or a test and a negative-case test, starts with a duplicate.
- Experimenting without losing the original. Duplicating a line before mutating it preserves a visual before/after reference right inside the file.
- Speeding up boilerplate. Configuration files —
webpack.config.js,.enventries, CI YAML — frequently reuse 80% of a previous block.
When you use Ctrl+C / Ctrl+V, you overwrite the clipboard, disrupt the cursor position, and add two extra keystrokes. The built-in vscode duplicate line shortcut leaves the clipboard untouched, duplicates exactly the current line (or selection), and moves the cursor to the copy automatically. Over an eight-hour day that difference is meaningful.
Line duplication also intersects closely with VS Code multi-cursor editing — once you have duplicated a block, multi-cursor lets you edit all instances simultaneously, turning what would be tedious manual repetition into a single burst of keystrokes.
The Universal vscode duplicate line Shortcut: Quick Reference
VS Code ships with two built-in duplicate-line commands. Neither requires a text selection: just position the cursor on any line and fire the shortcut.
| Action | Windows / Linux | macOS | Command ID |
|---|---|---|---|
| Copy Line Down | Shift+Alt+↓ | Shift+Option+↓ | editor.action.copyLinesDownAction |
| Copy Line Up | Shift+Alt+↑ | Shift+Option+↑ | editor.action.copyLinesUpAction |
| Duplicate Selection | No default | No default | editor.action.duplicateSelection |
The official VS Code documentation details these commands across all platforms. You can find the complete default keybindings in the VS Code Keyboard Shortcuts reference. Note that Duplicate Selection has no default keybinding — you must assign one manually (see the customization section below).
Copy Line Down vs Copy Line Up: When to Use Each
The distinction is simple but worth internalizing so muscle memory develops correctly:
- Copy Line Down (Shift+Alt+↓) inserts the duplicate immediately below the current line and moves the cursor to the copy. This is the 95% case — you duplicate a line and then edit the new version below the original.
- Copy Line Up (Shift+Alt+↑) inserts the duplicate above the current line, leaving the cursor on the new upper copy. Use this when you want to prepend a similar line before an existing one — for example, adding a new CSS rule above a rule you are using as a template.
Both commands work identically when you have a multi-line selection: VS Code duplicates the entire selected block, not just a single line. Select three lines, press Shift+Alt+↓, and all three lines are duplicated below the selection as a unit.
An important nuance: both commands duplicate the full line content including any leading indentation and trailing characters. The clipboard is untouched throughout. This means you can duplicate a line, edit the copy, then paste previously copied text at any point without losing it.
Method 2: Duplicate via Command Palette
If you are at a keyboard where the default shortcut conflicts with another application, or if you simply cannot remember the key combination, the Command Palette is always available:
- Place the cursor on the line you want to duplicate (no selection needed).
- Open the Command Palette: Ctrl+Shift+P on Windows/Linux, Cmd+Shift+P on macOS.
- Type Copy Line Down or Copy Line Up and press Enter.
You can also search for Duplicate Selection in the Command Palette — but remember that command only acts on selected text. If nothing is selected when you run it, nothing happens.
The Command Palette approach is also the easiest way to discover the exact command ID (shown in the right column of the Keyboard Shortcuts editor) before you bind a custom key to it.
Method 3: Duplicate Selected Text (Not Just Whole Lines)
Copy Line Down always duplicates the entire line. Sometimes you need to duplicate only a portion of a line — for example, duplicating a single CSS value, a function argument, or a JSON string inside a longer line.
That is where Duplicate Selection
(editor.action.duplicateSelection) comes in:
- Select the exact text you want to duplicate using a click-drag or Shift+arrow keys.
- Open the Command Palette and run Duplicate Selection, or use a custom keybinding you have assigned (see the customization section).
- VS Code inserts a copy of the selected text immediately after the selection and moves the cursor to the end of the copy.
Because Duplicate Selection has no default keybinding, many developers assign it something ergonomic like Ctrl+D (note: that key is taken by Add Selection to Next Find Match by default — pick something that does not conflict). A common choice is Ctrl+Shift+D or Alt+D.
Method 4: Multi-Cursor + Duplicate for Bulk Operations
The real productivity multiplier comes from combining line duplication with multi-cursor editing. Here is a concrete workflow:
Scenario: You have five CSS class definitions that are nearly identical. You want to create five new variants, each with a different color value.
- Select all five lines using Shift+Click at the start and end of the block.
- Press Shift+Alt+↓ to duplicate all five lines as a group below the originals. You now have ten lines.
- In the new five-line block, use Ctrl+Shift+L to select all occurrences of the value you want to change, then type the replacement. All five copies update simultaneously.
Alternatively, add multiple cursors with Alt+Click at the start of each of the five new lines, then navigate to the field you want to change and edit all at once.
Another powerful pattern: duplicate a single line ten times by pressing Shift+Alt+↓ repeatedly (or hold it down), then use Ctrl+Shift+L on a unique token to place cursors on each copy. This avoids tedious manual repetition for config blocks, test data, or migration seeds.
Customizing the vs code duplicate line Shortcut
Two reasons to customize: the default shortcut conflicts with another application or OS hotkey, or you want to add a binding for Duplicate Selection (which ships with no default).
Using the Keyboard Shortcuts UI
- Open the Keyboard Shortcuts editor: Ctrl+K then Ctrl+S.
- In the search box, type Copy Line Down.
- Click the pencil icon to the left of the entry and press your desired key combination.
- Press Enter to confirm. VS Code writes the binding to
keybindings.jsonautomatically.
Editing keybindings.json Directly
Open the Command Palette and run Open Keyboard Shortcuts (JSON). Add entries like the following (escape braces and angle brackets are for display; use real braces in the actual file):
{
"key": "alt+d",
"command": "editor.action.duplicateSelection",
"when": "editorTextFocus"
},
{
"key": "shift+alt+down",
"command": "editor.action.copyLinesDownAction",
"when": "editorTextFocus && !editorReadonly"
}
The when clause editorTextFocus && !editorReadonly ensures the
shortcut only fires when you are actually editing, preventing accidental duplications in
read-only diff views or the terminal panel.
After saving keybindings.json, changes take effect immediately — no restart
required. The VS Code documentation has a
full reference for keybinding syntax and when clause operators.
Verify Duplicated Code with a Diff Tool
Here is a scenario that no other guide covers: you duplicate a 20-line function, spend fifteen minutes editing the copy to handle a new edge case, and then review the PR. How confident are you that you only changed the lines you intended? Comments, variable names, and import paths are easy to accidentally leave identical — or accidentally change — in a duplicate.
A diff comparison between the original and the edited copy answers that question in seconds. The workflow:
- Before editing the duplicate, copy the original function body to a scratch file or a second browser tab.
- Make all your intended changes to the duplicated copy in VS Code.
- Open the Diff Checker Chrome extension, paste the original in the left panel and the modified copy in the right panel.
- The side-by-side diff highlights every line that changed. Lines you expected to change are green; any unexpected red or yellow highlights signal unintended drift.
The Diff Checker extension supports side-by-side and inline view modes, so you can switch views depending on how dense the changes are. It also handles DOCX and XLSX files if you are duplicating template documents rather than source code.
This pattern is particularly valuable when:
- Duplicating a database migration file — where an accidental column name carryover can corrupt production data.
- Duplicating a CI/CD pipeline step — where an un-updated environment variable causes a silent build failure.
- Duplicating a test fixture — where leftover hardcoded IDs cause tests to pass for the wrong reason.
For a deeper look at diff workflows inside VS Code itself, see our guide on comparing two files in VS Code. If you are comparing code snapshots outside the IDE, the diff definition guide explains the underlying algorithm and terminology.
When to Duplicate vs Refactor: Avoiding Code Smell
Knowing how to duplicate a line quickly is only half the picture. Knowing when not to is equally important.
Duplication is the right choice when:
- The two blocks will diverge significantly over time. Premature abstraction is often worse than duplication — wait until you see the pattern clearly.
- You are scaffolding test cases or fixtures where explicit repetition improves readability.
- One instance is stable (production) and one is experimental. Keeping them separate prevents coupling.
- The duplicated code is genuinely trivial (a single assignment, a one-line import). Extracting a function for one line adds ceremony without value.
Refactor instead when:
- You find yourself duplicating a block for the third time. The Rule of Three (coined by Martin Fowler in Refactoring) suggests three duplications signal an abstraction waiting to emerge.
- The duplicated blocks already share 90%+ of their logic and differ only in one parameter. Extract a function and pass the variable part as an argument.
- A bug in one copy must always be fixed in the others. If you cannot rely on keeping copies in sync, they should not be copies.
Static analysis tools can flag unintentional duplication before it becomes technical debt. Our guide to static code analysis tools covers linters and duplication detectors that integrate directly into VS Code via the Extensions marketplace.
If you are ever unsure whether a code duplication introduces a real difference or is accidentally identical, run a quick diff. It takes ten seconds and surfaces the answer definitively — which is exactly the use case the find the difference mindset is built on.
Troubleshooting: Shortcut Not Working
If Shift+Alt+↓ (or the macOS equivalent) does nothing, work through these checks in order:
1. Check for Keybinding Conflicts
Open Keyboard Shortcuts (Ctrl+K Ctrl+S), search
for Copy Line Down, and look at the "Conflicts" column. If another command
claims the same key in the same when context, that command wins. Either
reassign the conflicting command or rebind Copy Line Down to a free key.
2. Vim / Modal Editing Extension
If you use the VSCodeVim or nvim extension, the extension
intercepts many key combinations before VS Code sees them. In Normal mode,
Shift+Alt+↓ may be remapped inside the Vim layer. Either
use Insert mode for the shortcut or add an explicit passthrough in the Vim extension
settings using vim.handleKeys.
3. OS-Level Shortcuts
On macOS, Shift+Option+↓ is occasionally claimed by system tools (Mission Control, Accessibility shortcuts, or third-party window managers like Magnet). Check System Settings → Keyboard → Keyboard Shortcuts and disable any conflicting system binding.
4. Remote / Dev Container Context
When using VS Code Remote SSH or Dev Containers, keybindings run on the local machine but the editor runs remotely. Most keybindings work unchanged, but some extensions installed only on the remote host might register conflicting commands. Check the Extensions panel and filter by "Remote" to audit remote-side extensions.
5. Read-Only File or Diff Editor
If the file is open in a diff view or is marked read-only (padlock icon in the tab bar), editing commands including line duplication are disabled. Close the diff view or save a writable copy of the file first.
Frequently Asked Questions
What is the shortcut for duplicate line in VS Code?
On Windows and Linux, press Shift+Alt+Down to copy the current line downward, or Shift+Alt+Up to copy it above the current line. On macOS the same actions use Shift+Option+Down and Shift+Option+Up. No text selection is required — place the cursor anywhere on the line and press the shortcut.
Is there a difference between Copy Line Down and Duplicate Selection?
Yes, and the difference matters. Copy Line Down always acts on the
entire current line, with or without a selection. Duplicate Selection
only acts on text you have explicitly highlighted — if nothing is selected, the
command does nothing. Additionally, Duplicate Selection ships with no default
keybinding; you must assign one yourself in keybindings.json.
How do I duplicate a line in VS Code on Mac?
Use Shift+Option+Down Arrow to copy the current line downward. Use Shift+Option+Up Arrow to copy it above. The Option key on Mac corresponds to the Alt key on Windows and Linux — it is the same underlying VS Code command on all platforms.
Why is the duplicate line shortcut not working in VS Code?
The most common causes are a keybinding conflict with another extension or OS shortcut, a Vim/modal editing extension intercepting the keystrokes, or the file being open in read-only mode. Open Keyboard Shortcuts (Ctrl+K Ctrl+S), search for Copy Line Down, and inspect the Conflicts column. See the Troubleshooting section above for a full diagnostic checklist.
Can I duplicate a line without using the keyboard?
Yes. Open the Command Palette (Ctrl+Shift+P / Cmd+Shift+P), type Copy Line Down, and press Enter. You can also select the entire line with Ctrl+L, copy with Ctrl+C, and paste with Ctrl+V — but note that the copy/paste method does overwrite your clipboard, while the dedicated duplicate command does not.
Just duplicated a function or config block? Verify your changes with Diff Checker's side-by-side diff. Paste the original and the edited copy, and see exactly which lines changed — in seconds, without leaving the browser.
Install Diff Checker — Free Chrome Extension