# How to Copy formatted code without backslashes added?

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]  But I still can't figure out how to copy such output keeping this formatting... • 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? – Mr.Wizard Jul 18 '15 at 7:54 • @Mr.Wizard Yes, but I mean manual copying via the right-click menu (or Ctrl+C), not programmatic copying via CopyToClipboard. – Alexey Popkov Jul 18 '15 at 8:30 • @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. – Alexey Popkov Jul 18 '15 at 8:57 • @Wouter Ctrl+Shift+C copies unformatted code without wrapping. I need formatted code but without backslashes. – Alexey Popkov Jul 18 '15 at 18:01 • @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. – Alexey Popkov Jul 19 '15 at 3:26 ## 1 Answer 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}