Select and replace multiple occurrences of the same word not exactly in the same area/column?

150 views (last 30 days)
Hi im still learning matlab coming from VScode.
Is there a way to select multiple similar words one by one like in VSCode's Ctrl+D?? Like this https://www.youtube.com/shorts/vJvF7Y9oEjc
Based on other questions, it seems I can use Alt+left click drag. However this is only possible if they are perfectly aligned in the same area/column. VSCode Ctrl+D works in any configuration even if in different areas and different variables, and I can also select how many variables i can change.
For example, I have different variable names or maybe file names like this. I want to replace all the (variables1/2/3) with a new variable name (sampleID1/2/3) but they aren't really in the same column so I can't use Alt +left click drag.
variable1 = 1;
% Some other code here
variable2 = 1;
% Some other code here
variable3 = 1;

Answers (2)

Sai Sumanth Korthiwada
Sai Sumanth Korthiwada on 7 Feb 2023
Hi Lance,
Please note that, as of R2021b, the MATLAB Editor has the feature where you can hold "Alt" or "Option" while clicking to make a multi-cursor block selection.
But for the use case you mentioned, there is no such shortcut. You can use "Replace" (Ctrl+H) or "Replace all" to acheive the desired result.
Hope this helps.

Dinesh
Dinesh on 8 Feb 2023
Hi Lance,
You do have an option in MATLAB editor too to select similar words and then replace them to another word. However, it is only applicable if you choose to replace all of the similar words or a single one of them. To select specific instances of a variable and rename them, you would have to manually find and replace each instance of the variable that you want to rename. The following steps are for finding and replacing text in the current file.
Firstly, enter the command:
Ctrl + F
on the keyboard and then a small bar like below appears on top of the code.
You can enter the word that you want to search for and then expand the options by clicking on the arrow button next to "Find" to see:
(You can alternatively enter Ctrl + H if you want to open "Find" and "Replace" options at once).
When "Replace All" is clicked, the editor changes all the occurences of "dirName" to "directoryName". You can also replace words one-by-one by clicking on the "Replace" button.
You can choose if the word entered may be a part of another word or not, if the case has to be matched or not, or enter a regular expression to find and replace patterns of text, by using the options above the "Replace All" button.
Please take a look at this link for more information.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!