2

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?

Update1:

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

Update2:

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.

5
  • Why in nano? sed 's/\r//' ses.sh > ses2.sh from command line.
    – Rinzwind
    Apr 25, 2017 at 14:38
  • Refer to Remove carriage return in Unix
    – wjandrea
    Apr 25, 2017 at 14:38
  • 3
    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, 2017 at 14:39
  • can you search for \r and replace with nothing?
    – Zanna
    Apr 27, 2017 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. May 12, 2017 at 18:40

2 Answers 2

2

This seems not to be necessary.

$ echo -e 'foo\rbar\r\n' > nano
$ cat -A nano
foo^Mbar^M$
$
$ 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
foo$
bar$
$

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

6
  • 3
    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 Apr 25, 2017 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?
    – user423047
    Apr 25, 2017 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, 2017 at 18:19
  • If the buffer is unmodified it will, but if any change was made, usually it will prompt...
    – user423047
    Apr 25, 2017 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, 2017 at 18:30
2

The green cell is not carriage return but trailing whitespace, which is defined in /usr/share/nano/sh.nanorc like this:

# Trailing whitespace.
color ,green "[[:space:]]+$"

Thus, can be removed by replacing the regex [[:space:]]+$ or \s+$ or +$ with "nothing" by doing the following inside nano:

  1. Press Ctrl+\ to open the "search and replace" prompt.
  2. Press Alt+r to enable RegEx search.
  3. Enter [[:space:]]+$ or \s+$ or +$ and press Enter to search for trailing whitespace.
  4. Press Enter to replace the found whitespace with "nothing".
  5. Enter A and press Enter to apply to all occurences of whitespace.

You must log in to answer this question.