To test your case I'd done the following in Windows 10 with Mathematica 12.3:
Use Python
to generate 3 files, each with a different form (\n
, \r
and \r\n
):
with open('file1.txt','w') as f:
f.write('sample\n')
with open('file2.txt','w') as f:
f.write('sample\r')
with open('file3.txt','w') as f:
f.write('sample\r\n')
I used Windows notepad and Notepad++, which both copied the exact same content that was written.
Also in Mathematica, if we read those file using ReadString
, it will replace \n
with \r\n
:
ReadString["file1.txt"] // FullForm
(*Out: "sample\r\n" *)
ReadString["file2.txt"] // FullForm
(*Out: "sample\r" *)
ReadString["file3.txt"] // FullForm
(*Out: "sample\r\r\n" *)
it seems pasting in Mathematica, replace all the forms (\n
, \r
and \r\n
) as \n
:
(* copied from file1.txt content*)
file1 = "sample
";
(* copied from file2.txt content*)
file2 = "sample
";
(* copied from file3.txt content*)
file3 = "sample
";
FullForm[file1]
(*Out: "sample\n" *)
FullForm[file2]
(*Out: "sample\n" *)
FullForm[file3]
(*Out: "sample\n" *)
file1 == file2 == file3
(*Out: True *)
Note that in the Notepad++ or Sublime Text, file3.txt
has 3
lines.
Solution
We can change the paste function in the menu bar to insert the raw format like sample\r
instead of sample
with a new line. If you're on Windows, open MenuSetup.tr
in the Mathematica_Directory\12.3\SystemFiles\FrontEnd\TextResources\Windows
.
If you're using other languages in Mathematica, go into the language folder for example for Spanish the file exists in Mathematica_Directory\12.3\SystemFiles\FrontEnd\TextResources\Spanish\Windows
If you're a Mac user, we face some problems in this post. If you find a workaround, please comment it, so all of us could enjoy it.
In the file, search for paste
to reach this line (Windows and Mac seem to have little difference):
MenuItem["&Paste", FrontEnd`Paste[Automatic], MenuKey["v", Modifiers->{"Control"}]],
Add this code which was inspired by @kglr post after the above line to have two methods of pasting in Mathematica:
MenuItem["Paste 2", KernelExecute[NotebookApply[InputNotebook[],StringTake[ToString[FullForm[NotebookGet[ClipboardNotebook[]][[1, 1, 1]]]],{2,-2}]]],MenuEvaluator -> Automatic],
Save the file, restart the Mathematica and use the new Paste 2
: