Skip to main content
added 8 characters in body
Source Link
Ben Izd
  • 9.5k
  • 1
  • 15
  • 47
MenuItem["Paste 2", KernelExecute[NotebookApply[InputNotebook[],StringReplace[StringTake[RunProcess[{"powershell", "(get-clipboard -raw) -replace '\r''\\r','\r''\\r' -replace '\n''\\n','\n''\\n' -replace '\t''\\t','\t'"'\\t'"}, "StandardOutput"], {1, -3}], {RegularExpression["(?<!\n\\\\)\\n"]\n"] ->  "\\n", RegularExpression["(?<!\r\\\\)\\r"]\r"] -> "\\r", RegularExpression["(?<!\t\\\\)\\t"]\t"] -> "\\t"}]]],MenuEvaluator -> Automatic],
MenuItem["Paste 2", KernelExecute[NotebookApply[InputNotebook[],StringReplace[StringTake[RunProcess[{"powershell", "(get-clipboard -raw) -replace '\r','\r' -replace '\n','\n' -replace '\t','\t'"}, "StandardOutput"], {1, -3}], {RegularExpression["(?<!\n)\\n"] ->  "\\n", RegularExpression["(?<!\r)\\r"] -> "\\r", RegularExpression["(?<!\t)\\t"] -> "\\t"}]]],MenuEvaluator -> Automatic],
MenuItem["Paste 2", KernelExecute[NotebookApply[InputNotebook[],StringReplace[StringTake[RunProcess[{"powershell", "(get-clipboard -raw) -replace '\\r','\\r' -replace '\\n','\\n' -replace '\\t','\\t'"}, "StandardOutput"], {1, -3}], {RegularExpression["(?<!\\\\)\n"] -> "\\n", RegularExpression["(?<!\\\\)\r"] -> "\\r", RegularExpression["(?<!\\\\)\t"] -> "\\t"}]]],MenuEvaluator -> Automatic],
added 253 characters in body
Source Link
Ben Izd
  • 9.5k
  • 1
  • 15
  • 47
with open('file1.txt','w',newline='') as f:
    f.write('sample\n')

with open('file2.txt','w',newline='') as f:
    f.write('sample\r')

with open('file3.txt','w',newline='') as f:
    f.write('sample\r\n')

Also in Mathematica, if we read those file using ReadString, it will replace \n with \r\nwe'll see the original content:

ReadString["file1.txt"] // FullForm
(*Out: "sample\r\n""sample\n" *)

ReadString["file2.txt"] // FullForm
(*Out: "sample\r" *)

ReadString["file3.txt"] // FullForm
(*Out: "sample\r\r\n""sample\r\n" *)

it seemsAfter pasting in Mathematica, replace all the forms (\n, \r and \r\n) asbecomes \n:

Note that in the Notepad++ or Sublime Text, file3.txt has 3 lines.

MenuItem["Paste 2", KernelExecute[NotebookApply[InputNotebook[],StringTake[ToString[FullForm[NotebookGet[ClipboardNotebook[]][[1StringReplace[StringTake[RunProcess[{"powershell", "(get-clipboard -raw) -replace '\r','\r' -replace '\n','\n' -replace '\t','\t'"}, "StandardOutput"], {1, 1]]]]-3}], {2RegularExpression["(?<!\n)\\n"] ->  "\\n", RegularExpression["(?<!\r)\\r"] -2> "\\r", RegularExpression["(?<!\t)\\t"] -> "\\t"}]]],MenuEvaluator -> Automatic],

enter image description hereenter image description here

Because Paste 2 runs a PowerShell code to get the clipboard content, it's a little bit slow.

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')

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:

Note that in the Notepad++ or Sublime Text, file3.txt has 3 lines.

MenuItem["Paste 2", KernelExecute[NotebookApply[InputNotebook[],StringTake[ToString[FullForm[NotebookGet[ClipboardNotebook[]][[1, 1, 1]]]],{2,-2}]]],MenuEvaluator -> Automatic],

enter image description here

with open('file1.txt','w',newline='') as f:
    f.write('sample\n')

with open('file2.txt','w',newline='') as f:
    f.write('sample\r')

with open('file3.txt','w',newline='') as f:
    f.write('sample\r\n')

Also in Mathematica, if we read those file using ReadString, we'll see the original content:

ReadString["file1.txt"] // FullForm
(*Out: "sample\n" *)

ReadString["file2.txt"] // FullForm
(*Out: "sample\r" *)

ReadString["file3.txt"] // FullForm
(*Out: "sample\r\n" *)

After pasting in Mathematica, all forms (\n, \r and \r\n) becomes \n:

MenuItem["Paste 2", KernelExecute[NotebookApply[InputNotebook[],StringReplace[StringTake[RunProcess[{"powershell", "(get-clipboard -raw) -replace '\r','\r' -replace '\n','\n' -replace '\t','\t'"}, "StandardOutput"], {1, -3}], {RegularExpression["(?<!\n)\\n"] ->  "\\n", RegularExpression["(?<!\r)\\r"] -> "\\r", RegularExpression["(?<!\t)\\t"] -> "\\t"}]]],MenuEvaluator -> Automatic],

enter image description here

Because Paste 2 runs a PowerShell code to get the clipboard content, it's a little bit slow.

Source Link
Ben Izd
  • 9.5k
  • 1
  • 15
  • 47

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:

enter image description here