7
$\begingroup$

I want to replace a string in a selected region/cell in mathematica notebook/package. I searched but didn' t get any result. I think it' s a quite common feature for editors/programming environments. I guess workbench has this but I don' t have access to a copy.

Maybe now the best option is to use some other editor to do this, is it?

$\endgroup$
5
  • $\begingroup$ Why don't you use Find and Replace in the menu Edit, a shorthand (Ctrl+F) ? It helps you to replace whatever you'd like. $\endgroup$
    – Artes
    Commented May 27, 2013 at 12:34
  • $\begingroup$ It replaces in the whole notebook by default and doesn' t have an option to replace in the selected region/cell. $\endgroup$
    – xgdgsc
    Commented May 27, 2013 at 12:35
  • $\begingroup$ Check again, you can customize replacements, FindNext, FindPrevious, Replace, ReplaceAll, etc. Or maybe you need StringReplace ??? $\endgroup$
    – Artes
    Commented May 27, 2013 at 12:38
  • 2
    $\begingroup$ Yes, but it's different from replacing in a selected region. Say in the region you have 100 words to confirm replacing and another 100 same world outside this region in the same notebook. It's not practical to use FindNext and Replace only. $\endgroup$
    – xgdgsc
    Commented May 27, 2013 at 12:40
  • 2
    $\begingroup$ I do the same as @Artes suggested, and yet usually I come across the same problem as you @xgdgsc. What I do is copying the cells that I want to edit to another notebook and then use Find and Replace for the whole [new] notebook. $\endgroup$
    – Sos
    Commented May 27, 2013 at 13:35

1 Answer 1

4
$\begingroup$

Many (most?) code editors offer a check box labeled something like "Search in selected text only" (that's the wording used by the Find dialog of the OS X code editor BBEdit), Mathematica's Find dialog, unfortunately, does not support such an option. Until such time as this is rectified by the Mathematica developers, I think the work-around suggested by Sosi is you best opttion

  1. Bring up a new Notebook window
  2. copy the region in which you want to search into the new window
  3. carry out the search and replace there
  4. paste the results back over the selected region in the first notebook.
$\endgroup$
3
  • $\begingroup$ I also use this workaround. Perhaps we can automate it; I've never tried. $\endgroup$
    – Mr.Wizard
    Commented May 29, 2013 at 1:39
  • 1
    $\begingroup$ It is such a basic feature. We are now in version 12.1 and yet there is hardly any change in the editor interface from say version 3 (which I also used). I say this because a lot of basic features are either very hard to find or are simply absent. As a heavy user of MATHEMATICA I find that disheartening. $\endgroup$
    – Iconoclast
    Commented Aug 5, 2020 at 12:29
  • $\begingroup$ This certainly works, yet this can be a pain. Now that we are in 12.3, I think WRI can and should do better and permit find/replace within a selected block of code. $\endgroup$ Commented Oct 31, 2021 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.