Select or Replace all occurrences of selection in VS Code

avatar
Borislav Hadzhiev

Last updated: Apr 6, 2024
7 min

banner

# Table of Contents

  1. Select all occurrences of the current selection in VS Code
  2. Select the next occurrence of the current word
  3. Using the find functionality
  4. Select all occurrences of text using regular expression
  5. Replace all occurrences of the selected text
  6. Replace all occurrences using Select All occurrences of find Match
  7. Replace all occurrences in All files
  8. Replace all occurrences in specific files
  9. Replace all occurrences using a regular expression

# Select all occurrences of the current selection in VS Code

To select all occurrences of the current selection:

  1. Highlight the word.
  2. Use the Ctrl + Shift + L keyboard shortcut on Windows and Linux.
  3. Use the Cmd + Shift + L keyboard shortcut on macOS.

select all occurrences

Once you press the keyboard shortcut, your cursor will be placed at the end of all occurrences of the selected text.

You can also use the "Select All occurrences of find Match" action using the Command Palette.

  1. Highlight the word or text.
  2. Press:
  • Ctrl + Shift + P on Windows and Linux
  • Command + Shift + P on macOS
Note: you can also press F1 to open the Command Palette.
  1. Type select all occurrences and select Select all Occurrences of Find Match.

select all occurrences of find match

# Selecting all occurrences of the current word

If you need to select all occurrences of the current word, without having to highlight it:

  1. Use the Ctrl + F2 keyboard shortcut on Windows and Linux.
  2. Use the Cmd + F2 keyboard shortcut on macOS.

select all occurrences of current word

# Select the next occurrence of the current word

If you need to select the next occurrence:

  1. Use the Ctrl + D keyboard shortcut on Windows and Linux.
  2. Use the Cmd + D keyboard shortcut on macOS.

select next occurrence

If you need to undo the latest cursor selection:

  1. Use the Ctrl + U keyboard shortcut on Windows and Linux.
  2. Use the Cmd + U keyboard shortcut on macOS.

undo latest cursor selection

If you need to skip the current occurrence and add the next occurrence:

  1. Use the Ctrl + K + Ctrl + D keyboard shortcut on Windows and Linux.
  2. Use the Cmd + K + Cmd + D keyboard shortcut on macOS.

skip current occurrence add next occurrence

You can also select the next occurrence using the Command Palette.

  1. Highlight the word or text.
  2. Press:
  • Ctrl + Shift + P on Windows and Linux
  • Command + Shift + P on macOS
Note: you can also press F1 to open the Command Palette.
  1. Type add selection next match and select Add Selection to Next Find Match.

add selection to next find match

# Using the find functionality

You can also use the find functionality by pressing Ctrl + f on Windows or Linux or Cmd + f on macOS.

using find functionality

You can press the arrows to go to the previous or the next match.

If you want to select all matches of the found text, press:

  • Alt + Enter on Windows and Linux
  • Option + Enter on macOS

select all matches of found word

If you need to find the matches in a selection of text (not all matches in the file):

  1. Select the text.
  2. Press Ctrl + f (or Cmd + f on macOS).
  3. Type the word you'd like to search for.
  4. Click on the three lines icon to the right to enable the Find in Selection feature.

find matches in selection

The find functionality will only look for the text in the selected block.

# Select all occurrences of text using regular expression

The find functionality can also be used with a regular expression.

  1. Press Ctrl + f (Windows and Linux) or Cmd + f on macOS and enable regex search.

enable regex search

  1. You can enable regex search by clicking on the . * icon.

  2. Once you type in your regular expression, the matches will get highlighted.

  3. Click on the screen to focus the main window.

  4. Press Ctrl + Shift + L (or Cmd + Shift + L on macOS) to select all matches.

select all occurrences using regex

Note that you have to click on the main code window for the selection to work.

You can also use the Select All Occurrences of find match keyboard shortcut instead of Ctrl + Shift + L.

The keyboard shortcut is:

  • Alt + Enter on Windows and Linux
  • Option + Enter on macOS

select all occurrences of find match

The keyboard shortcut selects all of the matches that were found using the Ctrl + f functionality.

You can then edit the selected occurrences, copy them, replace them etc.

If the Ctrl + Shift + L shortcut doesn't work for you, use the command palette instead.

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type select all occurrences and select Select all Occurrences of Find Match.

select all occurrences of find match

# Replace all occurrences of the selected text

You can use the Ctrl + H (Windows and Linux) or Cmd + H keyboard shortcut if you need to replace all occurrences of the selected text.

  1. Select the text that you'd like to change.
  2. Press Ctrl + H (or Cmd + H) to open the replace functionality.
  3. Enter the replacement and click on the Replace all button.

replace all occurrences

Here is a short clip that demonstrates the process.

replace all occurrences

If you need to only replace the occurrences of text in a part of a file:

  1. Highlight the block of text with your mouse.
  2. Press Ctrl + H (or Cmd + H) to open the replace functionality.
  3. Click on the three lines icon to enable the Find in Selection functionality.

find in selection

  1. Replace the matches in the selected block of text.

# Replace all occurrences using Select All occurrences of find Match

You can also use the Select All occurrences of find Match action to replace all occurrences of a selected text.

  1. Highlight the word.
  2. Use the Ctrl + Shift + L keyboard shortcut on Windows and Linux.
  3. Use the Cmd + Shift + L keyboard shortcut on macOS.
  4. Start typing to replace the selected text.

replace all occurrences using select all occurrences

If the Ctrl + Shift + L keyboard shortcut doesn't work, you can also use the Select All occurrences of find Match action via the Command Palette.

  1. Highlight the word or text.
  2. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type select all occurrences and select Select all Occurrences of Find Match.

select all occurrences of find match

# Replace all occurrences in All files

You can use the Ctrl + Shift + H (Windows and Linux) or Cmd + Shift + H (macOS) keyboard shortcut to replace all occurrences in all files.

replace all occurrences in all files

You can also open the replace functionality by clicking on the magnifying glass in the left Activity Bar and expanding the arrow to show the field where the replacement text is entered.

expand replacement field

The icon right next to the replacement field is used to replace all matches of the text in all files.

Note that the changes are not made until you click on the icon and save your files.

# Replace all occurrences in specific files

If you click on the three dots below the icon, you can select to only replace the text in specific files.

only replace in specific files

For example, if you enter *.ts in the include field, the matches are only replaced in files that have a .ts extension.

If you enter src/**/tasks, the matches are only replaced in the src/<anything>/tasks directory.

The same approach can be used to exclude specific files or folders, you just add the pattern to the exclude field.

You can also replace all occurrences in specific files by hovering over the file in the results and clicking on the Replace all icon.

replace all occurrences in specific file

Here is a short clip that shows how to replace all occurrences in a specific file.

replace all occurrences in specific file

The same approach can be used to replace a single occurrence in a single file.

Hover over the line that you'd like to replace and click on the Replace button.

replace single occurrence

Make sure to save your changes after replacing the text.

# Replace all occurrences using a regular expression

If you need to replace all occurrences using a regular expression:

  • Use the Ctrl + h keyboard shortcut on Windows and Linux
  • Use the Cmd + h keyboard shortcut on macOS

find replace using regex

  1. Press on the . * icon to enable the Use Regular expression functionality.

enable use regex functionality

  1. Click on the Replace all button and save the changes.

click replace all button

The same approach can be used to replace all occurrences in all files using a regular expression:

  • Use the Ctrl + Shift + H keyboard shortcut on Windows and Linux
  • Use the Cmd + Shift + H keyboard shortcut on macOS
  1. Press on the . * icon to enable the Use Regular expression functionality.

enable regex functionality

  1. Enter your regex and the replacement and click on the Replace all button and save your changes.

click replace all

I've also written an article on how to rename/refactor a variable name in VS Code.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.