Sometimes when you have long code you need to check some part of this code.

the way I am using currently is to selected the part that I want and then copy it to new notebook and then evaluate it there. this process becomes annoying when repeated several times.

is there a better way to automatically evaluate selection in new notebook without need to go through copy paste procedure?

Update :

Thanks to halirutan for his suggestions. But one of my concerns is to evaluate selection within the cell in to another window. For example: enter image description here

how to evaluate the selection without manually copy and paste. If the cell itself contain long content, then the only way to debug the cell is be copying part by part and pasting into another notebook and evaluate as a whole new cell.

  • $\begingroup$ Yes, I guess it should be fairly easy to make a button which copies the currently selected cells, inserts them into a new notebook and evaluates everything. Please look at SelectedCells, CreateDocument and NotebookEvaluate. $\endgroup$ – halirutan Jul 23 '14 at 4:44
  • $\begingroup$ related 32340 $\endgroup$ – Kuba Jul 23 '14 at 9:20

Context Menu

The content of context menus is defined in the ContextMenus.tr in the $InstallationDirectory\SystemFiles\FrontEnd\TextResources folder. It is possible to modify this file with A) a text editor (e.g. Notepad++) or B) programmatically.

In order to get a new context menu item with the functionality you described:
First copy the original ContextMenus.tr form the $InstallationDirectory to your $UserBaseDirectory

If[! DirectoryQ[
   FileNameJoin[{$UserBaseDirectory, "SystemFiles", "FrontEnd", "TextResources"}]],
  FileNameJoin[{$UserBaseDirectory, "SystemFiles", "FrontEnd", "TextResources"}]]]

contextMenusFile =    
   FileNameJoin[{$InstallationDirectory, "SystemFiles", "FrontEnd", "TextResources", 
   FileNameJoin[{$UserBaseDirectory, "SystemFiles", "FrontEnd", "TextResources",


A) open ContextMenus.tr with an editor

SystemOpen[contextMenusFile] (*to open the file with your default editor*)
SystemOpen[FileNameDrop[contextMenusFile, -1]] (*to just open the directory*)

and find the "Input" -> { section. Now insert the following part between MenuItem["Evaluate &in Place", Evaluate[All]], and MenuItem["Evaluate Initiali&zation Cells", "EvaluateInitialization"], (or wherever you prefer) and save the file.

MenuItem["Evaluate in &New Notebook",

B) programmatically insert the new MenuItem into the existing list

contextMenus = Import[contextMenusFile, "HeldExpressions"];

insertPosition = 
   MenuItem["Evaluate Initiali&zation Cells", "EvaluateInitialization"]]]

newContextMenus = 
   MenuItem["Evaluate in &New Notebook", 
    FrontEndExecute[{FrontEndToken["Copy"], FrontEndToken["New"], 
      FrontEndToken["Paste"], FrontEndToken["HandleShiftReturn"]}]], 

and export it with

Export[contextMenusFile, newContextMenus, {"Package", "HeldExpressions"}, "Comments" -> None]

After you restarted Mathematica the menu item "Evaluate in New Notebook" appears whenever you right-click on a selected input. Menu

A left-click on "Evaluate in New Notebook" will create a new notebook with your selected input as its first input. It will then automatically be evaluated. newNB

Extra tip: To close the window of that new notebook without saving and without the pop-up of the "Save changes to ..." dialog, just have Shift pressed when you click on the close window x (in Windows).

Keyboard Shortcut

In order to get the same functionality as a keyboard shortcut (Alt + n):

Item[KeyEvent["n", Modifiers->{Command}], 

to the list in KeyEventTranslations.tr, which is located in the $InstallationDirectory\SystemFiles\FrontEnd\TextResources\Windows folder.

  • $\begingroup$ Great solution, thanks a lot $\endgroup$ – Algohi Aug 16 '14 at 5:10

To give you a first idea: Try this button, which uses all currently selected cells in the selected notebook, copies them into a new document and evaluates everything

Button["Test Selected Cells",
  InsertResults -> True], Method -> "Queued"]

You can use this to create a new palette which is then always available when you want to test your code.

Regarding your updated question: What you have to do is, look at the code I have given you and use NotebookRead and Print to examine what exactly the result of the reading procedure is when you only select parts of the text. You will see that you get box expression in this case. Now you can adapt the code to produce a correctly working button.

Button["Test Selected Cells", 
   Cell[BoxData[# ], "Input"] & /@ 
  InsertResults -> True], Method -> "Queued"]

Now it should be possible to select complete cells and additionally, only parts of the text.

  • $\begingroup$ thanks. can you look at the update in the question and see if what I want can be achieved. $\endgroup$ – Algohi Jul 23 '14 at 7:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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