Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I was thinking if there is something like Find and Replace (Crtl+F) functionality adapted to work with patterns.

Probably not, but it seems possible to create this with some effort and skills in low-level-notebook-programming.

My question is: if there is not such functionality bulid in, maybe someone have worked on this?

Example of what I'm thinking it should be able to do:

Text["test string"]

enter image description here

Style["test string", Bold]
share|improve this question
NotebookRead@Cells[] and work with CellExpression is the final choice I like. –  HyperGroups Jun 14 '13 at 9:20
I suppose it would be nice if we can use regexes in the find/replace box... –  J. M. Jun 14 '13 at 14:23
It doesn't exactly answer your question, but you probably should know that such functionality is available in the Wolfram Workbench, but restricted to the package files if I remember correctly. It will also search a whole directory, if you want. As that is basically a Mathematica plugin for eclipse, you'll also have access to a search with regexes. –  Albert Retey Jun 15 '13 at 10:45

2 Answers 2

  1. Parse cell objects to box data.
  2. Use MakeExpression[boxes] to convert boxes to Unevaluated standard form expressions.
  3. Find expressions that match the pattern p_.
  4. Use ToString[expr, InputForm]] to convert the target expressions to strings in InputForm.
  5. Find those strings in notebook and replace them to what you want (r_).

     NBReplace[p_, r_] := 
       Cases[NotebookRead /@ Cells[], Cell[BoxData[content_], "Input", ___] :> 
         (Cases[MakeExpression[#, StandardForm], 
            target: p :> 
                ToString[target, InputForm]]; 
                NotebookWrite[SelectedNotebook[], ToString[r, InputForm]]; 
              target-> r), Infinity] &) /@ 
         If[ListQ[content], content, {content}]] // Flatten


Style["test string", Bold]
NBReplace[Text[x_String], Style[x, Bold]]

results: Style["test string", Bold]

Also you can do:

NBReplace[Foo[x_Integer], Bar[x + 5]]

results: Bar[6]

If you want to bind to a custom shortcut, see this: http://stackoverflow.com/questions/4209405/customizing-mathematica-shortcuts
Hope it makes sense, cheers :)

share|improve this answer
Thank You for those remarks. This link is also correct anticipation of my needs. :) –  Kuba Jun 14 '13 at 22:17

This is too long for a comment. It only deals with gettings strings that you may then use to search for your patterns.

It is a shame that ToString[boxes, InputForm] does not give you a string that you want, as the resulting expression will have a lot of braces in it. Otherwise, I'd start with

boxesToStringOrSimplyString[cell_] := 
 If[StringQ[cell // First], cell[[1]], 
  ToString[cell[[1, 1]], InputForm]]

(Composition[boxesToStringOrSimplyString, NotebookRead] /@ Cells[])

string expressions of the contents of cells.

Anyway, you can also get such string expressions by using copy to clipboard. But it seems quite silly to me :P

Cell["", "Text"] // CellPrint;
cells = Cells[];
storeCellObj = Cells[] // Last;
pasteCells = cells[[;; -3]];
nb = EvaluationNotebook[];

 SelectionMove[pasteCells[[iii]], All, CellContents];
 SelectionMove[storeCellObj, After, CellContents];
 {iii, 1, Length[pasteCells]}

To find nice FrontEndTokens, check out, from the folder, $InstallationDirectory <> "/SystemFiles/FrontEnd/TextResources/<>"My system" the file KeyEventTranslations.tr. That's where I found the "Linebreak" token.

Another alternative is to save a .m file and to import that I think. There is also a menu item Clear Formatting that does almost what we want.

share|improve this answer
I knew it would lure You :) I'm going to test some things and then I will respond to Your post. –  Kuba Jun 14 '13 at 21:45
@Kuba alright! I'm sorry I did not put much explanation in. Note in particular that the CopyToClipboard thing works (for its silly purpose) if you put it in a fresh input cell in the bottom of your notebook. To get it into a textcell is nice as textcells don't use boxes. But it is quite silly, so I didn't want to spend too much time on it :) (and neither should you ;) ). But I think getting the right strings is crucial and I'm sure if there is a convenient way to get them. We'll see if somebody knows how :). –  Jacob Akkerboom Jun 14 '13 at 22:04
You are right, I'm not going to copy anything to clipbard. :) –  Kuba Jun 14 '13 at 22:11
@Kuba I was hoping my recent answer would help with this, but on closer look there is still a big gap to fill. Oh well :) –  Jacob Akkerboom Jul 22 '13 at 9:02
@Kuba oh there is no new version :). Just this remark, I felt things were related. Hope to catch you later. –  Jacob Akkerboom Jul 22 '13 at 9:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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