How to copy and paste $n$ lines from a text editor to $n$ cells?

It is not straightforward to access the clipboard contents but one way, that doesn't involve writing and reading from temporary notebooks is to use the undocumented ClipboardNotebook function:

CellPrint /@ StringSplit[NotebookGet[ClipboardNotebook[]][[1, 1, 1]], "\n"];

See also this answer which mentions some caveats. This prints strings as text cells. See the documentation for CellPrint if you require another form.


I would do it this way. I would write a function

pasteLines[txt_, styl_] := (CellPrint @ Cell[#, styl] & /@ StringSplit[txt, "\n"];)

To use it, I would first write

pasteLines["", "Input"]

I would line copy the text I wanted from the external editor onto the clipboard and paste it between the quotes in the empty string (1st argument), getting

pasteLines["line 1
line 2
 ...
line n", "Input"]

Evaluating this last expression, gives

enter image description here

These are all normal input cells that can be edited and evaluated.

This can be made more user friendly. You could merge this MikeLimaOscar's answer, eliminating the txt argument, and getting the text from the clip board programatically. You could incorporate the function into paste button which you put on a palette, available for frequent use. (I'm sorry to say I don't have the time to develop this second idea.)

Tags:

Import

Cells