Copy entire line and paste it BELOW the line with the cursor

Learn copy entire line and paste it below the line with the cursor with practical examples, diagrams, and best practices. Covers visual-studio-code development techniques with visual explanations.

Copy and Paste Entire Lines in Visual Studio Code

Copy and Paste Entire Lines in Visual Studio Code

Learn how to efficiently duplicate and move entire lines of code in Visual Studio Code without manually selecting them. This guide covers built-in shortcuts and advanced techniques.

Visual Studio Code (VS Code) is a powerful editor, and mastering its keyboard shortcuts can significantly boost your productivity. One common task is duplicating an entire line of code. While you could manually select the line, copy it, and then paste it, VS Code offers a much faster, built-in way to copy and paste a line directly below the current cursor position. This article will walk you through the core functionality and provide tips for seamless code manipulation.

The Basic Shortcut: Duplicating a Line Downwards

VS Code provides a straightforward keyboard shortcut to duplicate the current line (or selected lines) and insert the copy immediately below. This is incredibly useful for repetitive code structures, creating variations of a line, or quickly scaffolding new code.

Windows/Linux: Shift + Alt + Down Arrow
macOS: Shift + Option + Down Arrow

The primary shortcut for duplicating a line downwards.

How It Works: An Example

Let's illustrate this with a simple code snippet. Imagine you have a variable declaration and you want to quickly create another similar one.

const myVariable = 'Hello';
// Cursor is on this line

Starting with a single line of code.

With your cursor anywhere on the const myVariable = 'Hello'; line, press Shift + Alt + Down Arrow (or Shift + Option + Down Arrow on macOS). The result will be:

const myVariable = 'Hello';
const myVariable = 'Hello';

The line has been duplicated directly below the original.

Moving Lines Up and Down

Beyond simple duplication, VS Code also offers shortcuts to move entire lines or selections of lines up or down. This is useful for reordering code blocks without cutting and pasting.

Windows/Linux: Alt + Up/Down Arrow
macOS: Option + Up/Down Arrow

Shortcuts for moving lines up or down.

This pair of shortcuts allows you to move the current line (or selected lines) without affecting other lines, making refactoring and reordering much more fluid.

A workflow diagram showing three steps: 1. Place cursor on a line. 2. Use Shift+Alt+Down (or Option+Shift+Down) to duplicate the line below. 3. Use Alt+Down (or Option+Down) to move the duplicated line further down. Each step is represented by a blue box with a descriptive label and an arrow pointing to the next step. The diagram has a clean, minimalist style.

Workflow for duplicating and moving lines in VS Code.

Customizing Keybindings

If these default shortcuts conflict with other applications or your personal preferences, VS Code allows you to customize them. You can access the Keyboard Shortcuts editor by going to File > Preferences > Keyboard Shortcuts (or Code > Preferences > Keyboard Shortcuts on macOS).

1. Step 1

Open Keyboard Shortcuts: Press Ctrl+K Ctrl+S (Windows/Linux) or Cmd+K Cmd+S (macOS).

2. Step 2

Search for Commands: In the search bar, type copy line down to find the editor.action.copyLinesDownAction command or move line down for editor.action.moveLinesDownAction.

3. Step 3

Assign New Keybinding: Double-click on the command or click the '+' icon to assign a new key combination.