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.
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:
- Select a particular element or value that you want to change.
- To select all the instances of that element, use the keyboard shortcut “CTRL + SHIFT + L.”
- All the instances should be highlighted throughout the code, with a cursor at the end.
- 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:
- Choose the element or value you want changed.
- Press “CTRL + D” to select the next instance, then again for the next, and so on.
- You can change all selected instances directly.
- 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:
- Click anywhere inside the word you want to mass edit.
- 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.
- Make changes as you see fit (you’re not constricted to the selected word and can add text beyond it).
- 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:
- Select the word you want to edit.
- Press “CMD + D” to select the next instance of it in the code.
- Repeat Step 2 until you reach a point in code where you don’t need to make any changes.
- Edit the selected word as needed.
- 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:
- Select the word you need anywhere in the code.
- 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.
- Enter the changes you want. The editor will work on all the selected changes simultaneously.
- 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:
- Select a word that needs to be changed.
- Press “CTRL + D” to select the next instance. Note that this opens multi-cursor editing.
- Repeat Step 2 until you reach the part of code that can stay as-is.
- Make edits to the selected words.
- Exit multi-cursor selection by clicking anywhere.
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!