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

 

In Visual Studio Code (VS Code), you can easily duplicate a line or selection of lines using built-in keyboard shortcuts. Here's how to do it:

Duplicating a Line:

  1. Place your cursor on the line you want to duplicate.

  2. Use one of the following keyboard shortcuts:

    • For Windows/Linux: Shift + Alt + Down Arrow (To duplicate the line below the current line).
    • For macOS: Shift + Option + Down Arrow (To duplicate the line below the current line).

    This will create a copy of the line below the original line, and your cursor will move to the duplicated line.

Duplicating a Selection of Lines:

  1. Select the lines you want to duplicate. You can do this by clicking and dragging your mouse cursor to select multiple lines or by holding down the Shift key while using the arrow keys to select lines.

  2. Once you have the lines selected, use the same keyboard shortcuts as mentioned above:

    • For Windows/Linux: Shift + Alt + Down Arrow (To duplicate the lines below the selection).
    • For macOS: Shift + Option + Down Arrow (To duplicate the lines below the selection).

    This will create copies of the selected lines below the original lines, and your cursor will be at the end of the duplicated lines.

Here's an example:

Suppose you have the following code:

javascript
function hello() { console.log("Hello, World!"); }

If you place your cursor on the console.log line and press the appropriate keyboard shortcut (Shift + Alt + Down Arrow on Windows/Linux or Shift + Option + Down Arrow on macOS), the result will be:

javascript
function hello() { console.log("Hello, World!"); console.log("Hello, World!"); // Duplicated line }

You can use these shortcuts to quickly duplicate lines or blocks of code in your Visual Studio Code editor.

Comments