How to Change All Instances in VS Code

It might be rare, but sometimes you will need to change some attributes to ensure they don’t share a name. For example, you may have just written an independent function that mentions an entirely different element using the same name and need to differentiate between them.

How to Change All Instances in VS Code

For large projects, this can mean going through hundreds or thousands of lines at a time. Luckily, Visual Studio Code has a handy shortcut that allows you to select a particular element, line, or column throughout the project and edit all the instances of it at once. Read on to learn more.

How to Change All Instances on a Windows PC

If you want to change all the instances of a word at once on Windows, you need to do the following:

  1. Select a particular element or value that you want to change.
  2. To select all the instances of that element, use the keyboard shortcut “CTRL + SHIFT + L.”
  3. All the instances should be highlighted throughout the code, with a cursor at the end.
  4. With the multi-cursor mode on, make your changes to the word as needed (or input additional text afterward). Click anywhere inside the code to exit multi-cursor mode and return to a single cursor.

In case you want to select instances one at a time, the process is a little different and goes like this:

  1. Choose the element or value you want changed.
  2. Press “CTRL + D” to select the next instance, then again for the next, and so on.
  3. You can change all selected instances directly.
  4. Exit the multi-cursor mode by clicking anywhere inside the code.

How to Change All Instances on a Mac

VS Code works similarly on all platforms. One of the few differences lies in keyboard shortcuts due to each system using various keyboard settings and defaults.

Once you open up your code and figure the word you want to replace, the procedure for changing all instances of it goes as follows:

  1. Click anywhere inside the word you want to mass edit.
  2. Press “CMD + SHIFT + L” to highlight and select all the instances throughout the code and enter multi-cursor mode. The cursors go to the end of the word by default and have the word selected.
  3. Make changes as you see fit (you’re not constricted to the selected word and can add text beyond it).
  4. Close the multi-cursor mode by clicking anywhere inside the editor.

You can also enter multi-cursor mode by sequentially adding instances of a word or attribute to the selection. Here’s how:

  1. Select the word you want to edit.
  2. Press “CMD + D” to select the next instance of it in the code.
  3. Repeat Step 2 until you reach a point in code where you don’t need to make any changes.
  4. Edit the selected word as needed.
  5. Click anywhere to return to single-cursor mode.

How to Change All Instances on Linux

VS Code in Linux shares the keyboard shortcut settings with Windows.

If you want to change all instances of a word at once, here’s what you need to do:

  1. Select the word you need anywhere in the code.
  2. Press “CTRL + SHIFT + L” to select all the instances of that element throughout the code. All the instances should be highlighted, with a cursor at the end, and the platform edits in multi-cursor mode.
  3. Enter the changes you want. The editor will work on all the selected changes simultaneously.
  4. Click anywhere else to return to a single cursor when you’re done.

If you want to be more precise (such as keeping the new instances the same), you can select sequentially. Here’s how:

  1. Select a word that needs to be changed.
  2. Press “CTRL + D” to select the next instance. Note that this opens multi-cursor editing.
  3. Repeat Step 2 until you reach the part of code that can stay as-is.
  4. Make edits to the selected words.
  5. Exit multi-cursor selection by clicking anywhere.

Other Tips

VS Code has plenty of other nifty shortcuts, such as “Shift + Alt” creating a column box for multi-row changes, or selecting a line by clicking on its line number.

Did you know about these shortcuts for VS Code? Is there any other code editor you prefer using? Where do you get your materials and instructions for coding? Make sure to share it with us in the comments!

Disclaimer: Some pages on this site may include an affiliate link. This does not effect our editorial in any way.