# Evaluate selection in new window or new notebook?

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:

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.

• 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. – halirutan Jul 23 '14 at 4:44
• related 32340 – Kuba Jul 23 '14 at 9:20

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"}]],
CreateDirectory[
FileNameJoin[{$UserBaseDirectory, "SystemFiles", "FrontEnd", "TextResources"}]]] contextMenusFile = CopyFile[ FileNameJoin[{$InstallationDirectory, "SystemFiles", "FrontEnd", "TextResources",
FileNameJoin[{$UserBaseDirectory, "SystemFiles", "FrontEnd", "TextResources", "ContextMenus.tr"}]]  then 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", FrontEndExecute[{ FrontEndToken["Copy"], FrontEndToken["New"], FrontEndToken["Paste"], FrontEndToken["HandleShiftReturn"] }]],  B) programmatically insert the new MenuItem into the existing list contextMenus = Import[contextMenusFile, "HeldExpressions"]; insertPosition = Flatten[Position[contextMenus, MenuItem["Evaluate Initiali&zation Cells", "EvaluateInitialization"]]] newContextMenus = Insert[contextMenus, MenuItem["Evaluate in &New Notebook", FrontEndExecute[{FrontEndToken["Copy"], FrontEndToken["New"], FrontEndToken["Paste"], FrontEndToken["HandleShiftReturn"]}]], insertPosition];  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. 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. 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): Add Item[KeyEvent["n", Modifiers->{Command}], FrontEndExecute[{ FrontEndToken["Copy"], FrontEndToken["New"], FrontEndToken["Paste"], FrontEndToken["HandleShiftReturn"] }]]  to the list in KeyEventTranslations.tr, which is located in the$InstallationDirectory\SystemFiles\FrontEnd\TextResources\Windows folder.

• Great solution, thanks a lot – 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",
NotebookEvaluate[
CreateDocument[
Cell[BoxData[# ], "Input"] & /@