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

92 次查看(过去 30 天)
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;

回答(2 个)

Sai Sumanth Korthiwada
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 2023-2-8
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.

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by