Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 40 ยท Editing and Navigation

Multi-Cursor and Selection Editing

Edit many places at once with VS Code multiple cursors: Alt+Click, Ctrl+D, Ctrl+Shift+L, column selection, expand selection and line moves, with a runnable cursor model.

Typing the same change in ten places is slow and error-prone. VS Code lets you place several cursors and type once: every cursor receives the same keystrokes at the same time. Combined with smart selection commands and line operations, this removes most repetitive editing. This lesson covers the shortcuts and the thinking behind them.

Adding cursors

Action Windows / Linux macOS
Add a cursor where you click Alt+Click Option+Click
Add cursor above / below Ctrl+Alt+Up / Down Cmd+Option+Up / Down
Select next occurrence of the word Ctrl+D Cmd+D
Skip this occurrence, select the next Ctrl+K Ctrl+D Cmd+K Cmd+D
Select all occurrences Ctrl+Shift+L Cmd+Shift+L
Cursor at the end of each selected line Shift+Alt+I Shift+Option+I
Undo last cursor operation Ctrl+U Cmd+U
Back to a single cursor Esc Esc

On some Linux desktops Ctrl+Alt+Up/Down is used by the window manager; use the Keyboard Shortcuts editor to rebind it if it does not work.

Select next occurrence (Ctrl+D)

Place the cursor in a word (or select it) and press Ctrl+D. The first press selects the word; each further press selects the next match in the file and adds a cursor. Type to replace all of them. This is the fastest way to rename a variable inside one function without using Rename Symbol.

If one match is unwanted, Ctrl+K Ctrl+D skips it. If you went too far, Ctrl+U removes the last cursor.

Select all occurrences (Ctrl+Shift+L)

Selects every occurrence of the current selection in the file immediately. Use it carefully: it matches text, not meaning, so count inside counter can also be selected unless you enable Match Whole Word in the find widget first. For safe renames across a project use F2 (see Refactoring and Symbols).

Alt+Click and cursors above or below

Alt+Click adds a cursor anywhere, so you can fix lines that are not aligned. Ctrl+Alt+Down adds a cursor on the line below at the same column, which lets you edit several consecutive lines at once. Mouse alternative: hold Shift+Alt and drag to select a rectangular block, called a column (box) selection; every row gets its own cursor.

Cursors at line ends

Select several lines, then press Shift+Alt+I. A cursor appears at the end of each selected line. Now you can append a comma, a semicolon or a closing quote to all of them in one go. Press Home or End to move all cursors to line starts or ends.

Worked example: turn a list into an array

You have these lines:

TEXT
apple
banana
cherry
  1. Select the three lines.
  2. Press Shift+Alt+I (a cursor at the end of every line).
  3. Type ", โ€” each line now ends with ",.
  4. Press Home for all cursors to jump to the start of their lines, and type ".

Result: "apple", and so on, ready to paste into an array. Four steps instead of six manual edits.

Smart selection commands

You often want to select a meaningful piece of text rather than drag with the mouse.

  • Shift+Alt+Right (Expand Selection) grows the selection step by step: word, string, expression, statement, block. Shift+Alt+Left shrinks it back.
  • Ctrl+L selects the current line (press again to add the next line).
  • Ctrl+Shift+\ jumps to the matching bracket.
  • Double-click selects a word; triple-click selects a line.
  • Ctrl+Shift+Left / Right extends the selection by words, Shift+Home / End to line ends.

In many languages, expand selection understands the structure of the code, so one press inside a function call selects the argument list contents, the next the whole call, and so on.

Line operations

These work on the current line or on all selected lines:

Action Shortcut
Move line up / down Alt+Up / Alt+Down
Copy line up / down Shift+Alt+Up / Shift+Alt+Down
Delete line Ctrl+Shift+K
Insert line below / above Ctrl+Enter / Ctrl+Shift+Enter
Join lines Ctrl+J
Indent / outdent Ctrl+] / Ctrl+[
Toggle line comment Ctrl+/
Toggle block comment Shift+Alt+A
Cut/copy a line (no selection needed) Ctrl+X / Ctrl+C

Cutting or copying without a selection takes the whole current line, which makes moving a line quick: Ctrl+X, move, Ctrl+V.

The Command Palette has text transformations too: Transform to Uppercase, Transform to Lowercase, Transform to Title Case, Sort Lines Ascending, Delete Duplicate Lines, Trim Trailing Whitespace. They act on the selection, so they work on every cursor at once.

A stand-in: how multiple cursors edit

This JavaScript model stores a text and a list of cursors (line, column), then types the same string at each one. It shows why edits must be applied from the last cursor to the first, so earlier insertions do not shift the positions of later ones.

JS Stand-in: typing at many cursors

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The second example prints a- b-. If the engine edited the left cursor first, the right cursor's column would be wrong by one character, so real editors apply edits carefully in the right order.

JS Stand-in: select next occurrence

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Tips and habits

  1. Reach for Ctrl+D before Find and Replace when the change is local to one screen.
  2. Press Esc to collapse cursors before typing anything unrelated; otherwise your next keystrokes go to all cursors.
  3. Use column selection on aligned data such as columns of a table or CSV.
  4. Turn on editor.multiCursorModifier to ctrlCmd if Alt+Click clashes with your system (then Ctrl+Click adds cursors).
  5. Paste with many cursors: if you copied as many lines as there are cursors, each cursor gets one line. That is a handy trick for adding line numbers or names to a list.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which shortcut selects the next occurrence of the current word?

    Ctrl+D selects the next match and adds a cursor; Ctrl+Shift+L selects all matches.
  2. 2. You selected five lines and want to append a semicolon to each. What helps?

    Shift+Alt+I creates a cursor at the end of each selected line, so one typed character goes to all of them.
  3. 3. How do you return to a single cursor?

    Escape collapses multiple cursors into the primary one.
  4. 4. What does Alt+Up do?

    Alt+Up and Alt+Down move the current line or selected lines.

Summary

  • Alt+Click, Ctrl+Alt+Up/Down and Shift+Alt+I place multiple cursors; typing affects them all.
  • Ctrl+D selects the next occurrence, Ctrl+K Ctrl+D skips one, Ctrl+Shift+L selects all.
  • Expand Selection (Shift+Alt+Right) grows the selection by code structure.
  • Move, copy, delete and comment lines with single shortcuts; no selection needed.
  • Press Esc to return to one cursor.

Next: Find and Replace with Regular Expressions