12
$\begingroup$

By default code copied from the FrontEnd is formatted with backslashes added at the ends of lines. For example, evaluating in a Notebook

Range[40]

and copying the output produces the following when pasted in a text editor:

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, \
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, \
37, 38, 39, 40}

I would like to turn off addition of the backslashes at the ends of lines for the Copy command by default while preserving the wrapping with custom PageWidth.

For example with a PageWidth of 35:

SetOptions[$FrontEndSession, "ExportTypesetOptions" -> {"PageWidth" -> 35}]

copying the output of Range[40] and pasting here should yield:

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
11, 12, 13, 14, 15, 16, 17, 18, 
19, 20, 21, 22, 23, 24, 25, 26, 
27, 28, 29, 30, 31, 32, 33, 34,
35, 36, 37, 38, 39, 40}

(Custom PageWidth wrapping is applied but without backslashes at the ends of lines.)

Is it possible?


Update

Inside of the Notebook interface it is possible to achieve the desired formatting as follows:

CellPrint@TextCell[ToString[Range[40], InputForm], "Text", 
  TextJustification -> 1, PageWidth -> 330]

screenshot

But I still can't figure out how to copy such output keeping this formatting...

$\endgroup$
9
  • $\begingroup$ I was just about to close this as a duplicate of (17540) when I read again the point about backslashes. Is that a separate issue from page width? $\endgroup$
    – Mr.Wizard
    Jul 18, 2015 at 7:54
  • 3
    $\begingroup$ @Mr.Wizard Yes, but I mean manual copying via the right-click menu (or Ctrl+C), not programmatic copying via CopyToClipboard. $\endgroup$ Jul 18, 2015 at 8:30
  • 1
    $\begingroup$ @Mr.Wizard If there is no hidden option which controls this behavior, anything like a hotkey or menu item would be sufficient. A Palette solution is not very practical for such task. $\endgroup$ Jul 18, 2015 at 8:57
  • 1
    $\begingroup$ @Wouter Ctrl+Shift+C copies unformatted code without wrapping. I need formatted code but without backslashes. $\endgroup$ Jul 18, 2015 at 18:01
  • 1
    $\begingroup$ @M.R. This operation will delete comments from the code and change its appearance: for example, a // f will be converted to f[a]. I need to preserve the appearance with custom PageWidth. $\endgroup$ Jul 19, 2015 at 3:26

1 Answer 1

2
$\begingroup$

Perhaps this could work for you:

copyOutput[expr_, pageWidth_:20] := Module[{f = $TemporaryPrefix, s},
   s = OpenWrite[f, PageWidth -> pageWidth];
   Write[s, expr]; CopyToClipboard[Import[f]];
   Close[f]; Return @ expr
]

So when you run this:

In[]:= copyOutput[Range[40]]        
Out[]= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, \
        22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40}

the output you want is copied to the clipboard:

{1, 2, 3, 4, 5, 6, 
7, 8, 9, 10, 11, 
12, 13, 14, 15, 16, 
17, 18, 19, 20, 21, 
22, 23, 24, 25, 26, 
27, 28, 29, 30, 31, 
32, 33, 34, 35, 36, 
37, 38, 39, 40}
$\endgroup$

Your Answer

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

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