Replace Conditionally
The Replace Conditionally
extension allows you to perform search-and-replace operations in Visual Studio Code with advanced conditional logic.
Features
- Conditional Replace: Replace text based on various conditions.
- Decoration Updates: Highlight text matching the search conditions in real-time.
- Sidebar: Convenient sidebar for easy access to extension features.
Options
- Search Input: The search input to search for text. See example
- Replace Input: The replace input box to replace text with the search text. See example
- Match Case: Targets only the search words that exactly match the case. See example
- Match Whole Word: Matches the whole word of the search input. See example
- Regular Expression: Execute regular expressions like
(.*)
, +
, .
etc. See example
- Skip Spaces/Lines: Skips spaces at the beginning and end of lines. See example
- Regex Multiple Selector: An extended version of regex line selection
(.*)
. When enabled, you can skip multiple lines of the element. See example
- Conditions: Select one of
contains
, not contains
, or exact
. These terms are self-explanatory and allow you to replace text if the condition text meets the specified criteria in the line. For multiple line selection, the condition applies to the selected pattern. See example
- Condition Text: The condition text that needs to be checked against the conditions.
- Add Conditions: Add another condition. You can add multiple conditions.
- Remove Conditions: Removes the below condition.
- In Between Conditions: When using multiple conditions, you can choose whether conditions are based on
and
or or
. With and
, both conditions must be met to replace the text; with or
, if one condition is met, the text is replaced.
- Clear All: Clears all input data.
- Refresh: Refreshes the extension if any unknown errors occur.
Multiple Use Case Examples
Search Example
Simply search for the word "primary" in real-time; it shows the results in the active file, similar to VS Code search.
Search Replace Example
Search for the word "primary" and replace it with "secondary," similar to VS Code search and replace.
Match Case Example
Search for the word "index," and it shows every occurrence. When you enable match case, it only shows the exact match.
Match Whole Word Example
Search for the word "i" and it shows every occurrence, even within other words. Enabling match whole word only matches the exact word.
Regular Expression Example
This is one example of using a regular expression that is commonly used. You can use any regular expression after enabling it in the search.
Skip Spaces/Lines Example
Enable the skip spaces/lines option to skip spaces at the start and end of lines, as well as line breaks. This makes it easier to search for corresponding entries. Be sure to format the document properly after replacement if needed. Or you can add spacing in the replace text.
Here is another example of Skip Spaces/Lines:
In this example, I used Skip Spaces/Lines with a regular expression. After enabling Skip Spaces/Lines, the extension skips spaces and line breaks, making it easier to match the pattern. It's particularly useful with regex multiple selectors.
Regex Multiple Selector Example
Search for "button," but the buttons are different from each other. After enabling the Regex Multiple Selector, it considers everything inside the button as one element. This finds multiple patterns in the file. When you paste the text of the search in the replace field it automatically allocates numbers for time-saving. I replaced the second group ($2
) with "Changed." Skip Spaces/Lines removed default spaces, which can be fixed by formatting the document or adding spaces in the replacer.
Conditions Examples
Examples using single and multiple line searches with regex and multiple conditions.
Single Word with Conditions
Search for "secondary" and replace it with "primary." By applying conditions, the extension checks if the lines of that word include the condition.
In the above example, I first applied the condition if it contains "Code" so it highlights the "secondary" that line contains the Code after I extended that condition and added one more condition of contains "//" with and
operation so that now it highlights the "secondary" only which line contains both "Code" word and "//", after that I changed the "//" condition to not contains so it highlights the "secondary" only that line contains "Code" and not contains "//", Its simple logical expressions. The second condition I applied is exact and copy-paste the line code so that it only replaces the secondary if its line is exactly the same as my provided line text in the condition and I replaced that secondary with primary, you can also try it by yourself and enjoy it 😊.
Multi-Line Pattern with Conditions
Search for "Button" with a regex pattern (.*)
, Skip Spaces/Lines, and Regex Multiple Selector. Copy the search expression into replace, and it automatically allocates numbers to (.*)
for time-saving. Change the second group ($2
) to "Changed" and apply the condition if the matched pattern contains id="13"
. Highlights the button that contains id="13"
, and then change it to id="14"
, and at the end, I changed the condition to not contain id="14"
and then replaced the other buttons, after replacing the only button that has id="14"
is not changed and others are changed. After replacement, format the document if Skip Spaces/Lines was used or you can also add spaces in replace text.
For any further questions, feedback, or inquiries, feel free to reach out:
Created this extended search extension for VS Code to enhance search functionality and make things easier.
Thank you for using the Replace Conditionally
extension! We hope it enhances your development experience in Visual Studio Code.