I copied something from Windows to Nano in WSL and it included carriage returns (CR characters). Even though I removed all carriage returns with the text editor (Notepad++) these are still added after copying, possibly by Windows itself - The more the window is narrower, the more carriage returns Windows adds.

enter image description here

My question:

How could I remove all carriage returns in a find and replace (CTRL+\) action in Nano?


If I paste the script and just saving (CTRL+O) there is no change (I made sure that when saving, dos mode is toggled off).


Executing dos2unix on the file didn't help - it still contains these Green boxes and as for now I can remove them only manually (not a solution of course). I know there are carriage returns, because if I copy the file back to Notepad++, I see these are displayed as CR chars, when I do View > Show symbol > Show all characters.

  • Why in nano? sed 's/\r//' ses.sh > ses2.sh from command line. – Rinzwind Apr 25 '17 at 14:38
  • Refer to Remove carriage return in Unix – wjandrea Apr 25 '17 at 14:38
  • 2
    nano seems to automatically remove them. If I open a file that contains them and then ^O, it's saved without the carriage returns – Zanna Apr 25 '17 at 14:39
  • can you search for \r and replace with nothing? – Zanna Apr 27 '17 at 7:16
  • Windows uses CR+LF line endings and drops CR on text mode on load and re-adds them on save. *nix has no such concept. However, Windows notepad.exe opens text files in binary mode and expects CR+LF line endings. LF line endings will be invisible on notepad.exe. – Erkin Alp Güney May 12 '17 at 18:40

This seems not to be necessary.

$ echo -e 'foo\rbar\r\n' > nano
$ cat -A nano
$ nano nano

enter image description here

I do absolutely nothing except type Ctrl+O and press enter to save the file, then Ctrl+X to exit...

$ cat -A nano

So apparently simply opening a file and saving it in nano on Ubuntu is sufficient to destroy carriage returns.

  • 2
    I don't know about WSL, but in nano 2.6.3 on my xenial box when the line endings are CRLF rather than just CR, it seems to require an extra step of hitting Ctrl-D in the save dialog to toggle DOS format off – steeldriver Apr 25 '17 at 15:02
  • Thanks Zanna! Till now the only thing I did (both in WSL and in a remote machine I tunneled into from WSL) was just CTRL+X because as far as I know, it encapsulates CTRL+O. @steeldriver do you suggest the sequence CTRL+O, CTRL+D, CTRL+X? – JohnDoea Apr 25 '17 at 18:15
  • ^X alone allows me to exit without prompting me - seems to be a special case in that respect @Benia – Zanna Apr 25 '17 at 18:19
  • If the buffer is unmodified it will, but if any change was made, usually it will prompt... – JohnDoea Apr 25 '17 at 18:23
  • yes I know, that's why I'm saying this is a special case, because ^X lets me exit and the file is unchanged, but ^O modifies the file (even though I haven't done anything) @Benia – Zanna Apr 25 '17 at 18:30

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.