One of the powerful features that can save you time and effort while working with text or code in VSCode is the “Find and Replace” feature.
In this article, we will look at how to use the Find and Replace feature in VSCode.
Open Your File: Begin by opening the file where you want to find and replace text.
Shortcut for Find/Replace Dialog Box: Press Ctrl+F
(Windows/Linux). For macOS users, it’s ⌘+F
.
This keyboard shortcut will open the Find and Replace dialog box.
Now that you’ve opened the Find and Replace dialog box, let’s learn how to find text:
Type Your Query: In the “Find” field, enter the text you want to locate within the document.
Navigation: To locate the next or previous occurrence of the text, use the keyboard shortcuts Enter
or ↑/↓
respectively.
Match Case (Optional): If you need to find text that matches the capitalization of your query, enable the “Match case” option.
As you can see in this image below, “lorem ipsum” is the query that is being typed in, but only the last occurrence is highlighted.
This is because the query, typed in, is in lowercase. But if you disable the “match case” option, it will highlight every line with “lorem ipsum” irrespective of the case.
Whole Words (Optional): To find only whole words, not parts of words, enable the “Whole words” option.
Regular Expressions (Advanced): For complex pattern matching, VSCode allows you to use regular expressions in your queries.
At first, to use regular expression to find specific text, you need to enable the “Regular Expression” option.
As you can see in the image above, there are four email addresses but only two with “@gmail” are highlighted.
This is because the regex pattern “\b[A-Za-z0-9._%+-]+@gmail.com\b
” used in the find dialog box will only look for emails with “@gmail” in it.
Find in Selection (Optional): If you have a specific selection within the file, you can choose to find text only within that selection.
As you can see in the image above, there are two sections of sentences with same text.
The first section which is highlighted is the section we want to search for words in, regardless of if there are same words in other places in the document.
After highlighting the section, you need to enable the “selection” option and as you can see, it turns the selected section color gray.
The query typed in is “read” but it only selects the word “read” in the first section while ignoring the word “read” in the other section.
Find in Files (Advanced): VSCode also offers the capability to find and replace text in all open files or within a specified folder.
In order to search for files in the folder, you need to tap on any files or folder (so as to make sure that your cursor is not in the document area).
And then, hold “Ctrl+F” or ⌘+F
to launch the dialog box.
Now, let’s look at how to replace text using the Find and Replace feature:
Replace Text: In the “Replace” field, input the text you want to use as a replacement for the found text.
Replace All Occurrences: To replace all occurrences of the text at once, click the “Replace All” button.
Replace Next Occurrence: To replace only the next occurrence of the text, use the “Arrow” button and then “Replace”.
Ctrl+Z
(Windows/Linux) or ⌘+Z
(macOS) to revert your changes.