keepcalculator

06 Numbers and Powers

VS Code: How to Select Multiple Lines (3 Ways)

Card No. 06·69880 · Updated · Standard formula, shown below

To select multiple lines in VS Code, hold Alt (Windows/Linux) or Option (macOS) and click and drag vertically down the line numbers, or use Ctrl+Alt+Down/Up (Windows/Linux) or Cmd+Option+Down/Up (macOS) to add the next line above or below. This creates multiple cursors, letting you edit all selected lines at once. Below are the three main methods, when to use each, and how to avoid common pitfalls.

Method 1: Mouse – Column Selection

The quickest way to select several full lines is to click and drag with the mouse while holding a modifier key. This is often called column selection because it selects a rectangular block of text.

  • Windows/Linux: Hold Alt and drag vertically over the line numbers (the gutter) or over the text itself.
  • macOS: Hold Option and drag vertically.

Dragging over the line numbers selects entire lines. Dragging over the text selects a column of characters – useful when you want to edit the same column in multiple lines (e.g., changing the indentation or a prefix). If you start the drag on the line numbers, you get full-line selections; if you start on the text, you get a rectangular block.

Method 2: Keyboard – Add Cursor Above/Below

For precise control without the mouse, use the keyboard shortcuts that add a cursor on the next line up or down:

  • Windows/Linux: Ctrl+Alt+Down (add cursor below) and Ctrl+Alt+Up (add cursor above).
  • macOS: Cmd+Option+Down and Cmd+Option+Up.

Each press adds a cursor on the adjacent line at the same column position. This is ideal when you want to select a few specific lines that are not contiguous – you can skip lines by not pressing the shortcut on them. Once you have cursors on the lines you need, type or delete to edit all at once.

Method 3: Select All Occurrences

If you want to select multiple lines that share the same text (e.g., all lines that start with console.log), you can use Select All Occurrences:

  1. Select the text that appears in each line you want to edit.
  2. Press Ctrl+Shift+L (Windows/Linux) or Cmd+Shift+L (macOS).
  3. VS Code adds a cursor at every occurrence of that exact text in the current file.

This is not strictly “select multiple lines” but it achieves the same outcome – you can edit all matching lines simultaneously. It’s especially handy for renaming a variable or changing a repeated log message.

How to Choose the Right Method

Each method suits a different situation:

  • Column selection (mouse) – best when you want a rectangular block of text, like changing the first 5 characters of 10 lines.
  • Add cursor above/below (keyboard) – best when you want to select a handful of consecutive lines and edit them identically, without selecting extra text.
  • Select all occurrences – best when the lines you want share the same string, even if they are scattered across the file.

You can combine methods. For example, use column selection to select a block, then press Ctrl+Shift+L to add cursors at every occurrence of that block in the file.

Common Problems and Fixes

Users often run into these issues:

  • Alt+drag selects a column but not full lines. Make sure you start the drag on the line numbers (the gutter), not on the text. If you start on the text, you get a rectangular selection.
  • Ctrl+Alt+Down doesn’t work. On some Linux distributions, this shortcut is captured by the window manager or desktop environment. You can change the shortcut in VS Code: open File > Preferences > Keyboard Shortcuts, search for “Add Cursor Below”, and assign a new key combination.
  • Multiple cursors appear but typing only affects one line. This usually happens when the cursors are not actually on separate lines – check that each cursor is on its own line. If you accidentally have two cursors on the same line, press Esc and try again.
  • Select all occurrences selects too much. If your search text is too short (like a single letter), it will match many places. Use a longer, more specific string.

Using Multiple Cursors to Edit Lines

Once you have multiple lines selected (with any method), you can:

  • Type to insert the same text before each cursor.
  • Press Delete or Backspace to remove the selected text.
  • Press Home or End to move all cursors to the start or end of their respective lines.
  • Use Ctrl+Right/Left (Windows/Linux) or Cmd+Right/Left (macOS) to move all cursors by word.

This is the core power of multi-cursor editing: you can apply the same change to many lines in one keystroke, instead of repeating the edit line by line.

Alternatives to Multiple Selection

Sometimes you don’t need multiple cursors at all. If you want to change the same text on many lines, consider:

  • Find and Replace (Ctrl+H / Cmd+H) – replace all occurrences of a string.
  • Rename Symbol (F2) – rename a variable or function across the whole file or workspace.
  • Format Document (Shift+Alt+F / Shift+Option+F) – fix indentation on all lines at once.

These are often faster and less error-prone than manually selecting lines, especially when the change is uniform.

Frequently Asked Questions

How do I select all lines in VS Code?

Press Ctrl+A (Windows/Linux) or Cmd+A (macOS) to select the entire file. If you want to select all lines that match a pattern, use Ctrl+Shift+L after selecting the pattern.

How do I select multiple non-consecutive lines?

Use the keyboard shortcut Ctrl+Alt+Down / Cmd+Option+Down to add cursors line by line, skipping the lines you don’t want. Alternatively, use Ctrl+Shift+L to select all occurrences of a specific text.

Why doesn’t Alt+click work on Linux?

On some Linux desktops (like GNOME), Alt is used by the window manager to move windows. You can either change the VS Code shortcut to another key (e.g., Ctrl+Alt) or change the desktop setting to use Super (Windows key) for window management.

Can I select multiple lines without holding a modifier key?

Yes, you can use Ctrl+Shift+L to select all occurrences of a selection, which doesn’t require holding a modifier. For a rectangular selection without modifiers, you can enable “Column Selection Mode” from the command palette (Ctrl+Shift+P > “Column Selection Mode”), then drag with the mouse.

How do I undo multiple cursors?

Press Esc to exit multi-cursor mode and return to a single cursor. If you want to undo the last edit made with multiple cursors, press Ctrl+Z / Cmd+Z as usual.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →