Tips・Use VS Code faster

Fix things in bulk with find and replace

Answer "where did I write that word again?" in an instant. And go further, replacing the same word across a file or a whole folder at once. Save all the time you'd spend fixing things one by one by hand.

As your code gets longer, you spend more and more time hunting for “that line I just wrote, where is it again?” And when you want to change every instance of a name to a different one, scanning from the top and fixing them one by one is a chore. That’s where Find and Replace come in.

First, “Find” — ⌘ + F / Ctrl + F

Press ⌘ + F and a small search box appears at the top right. Type the word you’re looking for and every instance lights up, along with a count of how many were found. Each time you press Enter, you jump to the next spot in order.

No more hunting with your eyes for “that class name” or “that character” in a long stretch of code.

At the far right of the search box, three small icons sit in a row. Pressing them toggles them on and off to narrow down how you search.

IconNameWhat it does
AaMatch CaseWhen on, treats Box and box as different things
ab|Match Whole WordWhen on, matches box but no longer matches inbox
.*Use Regular ExpressionSearch by pattern (e.g., \d+ to find only numbers)

Usually leaving them off is plenty, but when “I only want to find box, yet toolbox lights up too,” turning on Match Whole Word narrows it down to exactly what you’re after.

Next, “Replace” — ⌘ + ⌥ + F / Ctrl + H

Press the small > (arrow) to the left of the search box, and another input field opens below. That’s where your replacement goes.

  • Top field: the word to find (e.g., box)
  • Bottom field: the new word (e.g., card)

Replace has two buttons.

ButtonWhat it does
Replace (one at a time)Replaces just one found spot (with confirmation)
Replace AllReplaces every found spot all at once

For example, to change the name box to card everywhere, one press of “Replace All” does it.

Replacing only within a selected range

“I only want to replace this part, but the whole file becomes the target.” When that happens, first select the range you want to replace, then turn on the “Find in Selection” icon at the far right of the search box.

Now, even if you press “Replace All,” nothing outside your selection is touched. Since you can safely aim at “just inside this function” or “just this block,” it’s the most practical answer to the “Replace All is scary” problem from earlier.

Replacing while preserving case

You want to change box to card, but your code also has Box (a class name) and BOX (a constant) mixed in—a common situation. A normal replace turns them all into lowercase card.

So turn on the AB icon to the right of the replace field (Preserve Case). Now the replacement keeps the original capitalization.

Original wordAfter replace
boxcard
BoxCard
BOXCARD

Without knowing this, you’d have to go back and fix just the capitals by hand after replacing. It’s the option you’ll want to turn on first when renaming things.

“I can’t remember which file I wrote it in!” For that, use the magnifying-glass icon (Search) on the left bar. ⌘ + ⇧ + F (on Windows, Ctrl + Shift + F) also opens it.

Type a word here and it searches all the files inside your open folder at once. It even lists which file and which line number, so you won’t get lost even on a site that’s grown large.

To replace across a whole folder, use ⌘ + ⇧ + H (on Windows, Ctrl + Shift + H). Here too, you don’t have to replace everything all at once. The list of search results can be replaced per file or per single item, and clicking a result line lets you check the difference between before and after.

Advanced: use $1 in regular expressions to reuse “what you found”

Turn on .* (regular expressions) in search, and you can reuse the part enclosed in () directly in the replacement. For example, when you want to standardize strings written with single quotes to double quotes:

  • Search field: '(.+)'
  • Replace field: "$1"

Now a form like 'box' changes to "box". $1 means “the string that matched inside the parentheses.” It feels hard at first, but the day you face making the same small edit in dozens of places by hand, remembering this trick is a huge time-saver.

TipsVSCode shortcuts worth learningSaving, commenting, and other VSCode shortcuts worth learning are over here

Summary

  1. ⌘ / Ctrl + F lights up and finds words within the current file
  2. Turn on Match Whole Word to narrow down to exactly the word you want
  3. Use Replace to change the same word into another one in bulk
  4. Select a range and turn on Find in Selection to safely replace only there
  5. Turn on AB (Preserve Case) and Box becomes Card, keeping upper/lower case
  6. With ⌘ / Ctrl + ⇧ + F full-text search, find across every file in a folder at once

When finding and fixing gets faster, growing code stops being scary.

FAQ

What if search can't find anything?
The case-sensitivity or whole-word toggles may be getting in the way. Check the state of the three icons on the right of the search box. If you want to find a string that spans line breaks, either paste the text with its line break into the search field, or press Ctrl + Enter to insert a line break into the field.
What is $1 in a regular expression?
When you use parentheses () in your search term, whatever matches inside them is remembered. If you write $1 in the replace field, you can reuse that remembered text as-is. By searching including the parentheses and using $1 in the replace field, you can do things like keep only the enclosed text while rewriting the symbols around it.
I want to check each spot as I replace
Instead of "Replace All," use the "Replace" (one at a time) button so you can confirm each highlighted spot as you replace it. This is the safer choice while you're still getting used to it.