# How to copy code out of notebook to an external text editor without extra line feeds being added?

I am simply trying to copy code from a cell (input cell) in a notebook, to paste the code into a plain text editor, and not have some lines wrap in the middle.

I need to copy the code, as is, to a text editor, so I show it verbatim in there.

I copy the code by selecting the cell, and then do COPY AS, and select INPUT TEXT. Then paste it into the other text editor. (if I try COPY AS PLAIN TEXT, then things are worst, I lose all formatting, so that is not an option)

For some reason, some lines that are longer than some length, will end up wrapping in the text editor when pasted. The LF are inserted in different places depending on the length of the line. I see a CRLF added where this was not in the notebook.

Here is a screen shot, where I have 2 cells, and the result of copying each cell to the text editor.

You can see that the first cell, which is the same as the second cell, except one line in there is a little tiny bit longer than what is in the second cell (I added + x to that line to make it little longer). Now that line was wrapped in the copy in 2 places. When the second cell, the line wrapping was in one place.

Here are the 2 cells shown in the image above, so you can try that yourself:

foo {z_} := Module[{x, y},
If[z < 1,
x + y/2 + Sin[3 x] - 43453488362 Cos[x]/Pi - (343^4 - Sin[34 - x]) + 34 Sin[x y]/(x y) + x,
x - y
]
]


second cell

foo {z_} := Module[{x, y},
If[z < 1,
x + y/2 + Sin[3 x] - 43453488362 Cos[x]/Pi - (343^4 - Sin[34 - x]) + 34 Sin[x y]/(x y),
x - y
]
]


Here is a screen shot showing the locations of the CRLF characters that are added during the copy of both cells to the text editor.

I would like to be able to copy the code from the cell to a text editor, without any extra LF being inserted during the copy.

I think this problem is due to the notebook itself, but not sure. I tried different text editors, and they are show the same problem. So the LF is added during the copy, and not during the paste. I am on windows 7, using version 9.

Update

I have just found a work around !! But this can be done for the whole notebook, and not individual cells. But that is ok. I could always copy the cell to its own notebook.

## Here are the steps to copy code out of a notebook without extra CRLF

1. select File->SAVE AS ->Rich Text format (.rtf)
2. Using MS WORD (or any other program that can read .RTF), open the .rtf file
3. Select and Copy the text from word using the mouse.
4. Paste, using the mouse, into the other text editor (notepad, etc...)

Now no line wrapping appear! No extra CRLF are seen.

So, I am happy now. This works for me, as a work around. It would be nice to know though if it is possible why this happens.

Update Well, the excitement did not last for long. Even though the above solution fixes the extra CRLF problem, it introduces new problems. Many of Mathematica symbols in the code do not show up correctly in .rtf (all the 2D math symbols for examples. So this solution does not work. So I still need to find a way around this. More research is needed.

-
If your target is a plain text editor, RTF isn't going to work. Why not use a simple multi-replacement on all the CR/LF in the editor after copying the text? I tried to be more specific, but I actually don't have the problem on Mac OS X with xemacs under X11 as the target. There, a regular copy and paste (not even as plain text) works for me with the line breaks intact. Caveat: I have no idea what capabilities your editor has... nor have I got that mysterious WORD program you mentioned. – Jens Jan 11 '13 at 1:42
Oh right, you only want to remove certain ones - now that I look at it more carefully, I see the same thing as you in xemacs (just didn't compare carefully enough at first). So the line break formatting must have been done by the notebook, as you mentioned. – Jens Jan 11 '13 at 3:19

SetOptions[$FrontEndSession, "ExportTypesetOptions" -> {"PageWidth" -> 500}]  You must select and copy the entire cell for this setting to have effect. Change $FrontEndSession to \$FrontEnd to make the setting persistent.
Based on the survey, for use here a good value is "PageWidth" -> 90. – Mr.Wizard Jan 11 '13 at 9:14