Celeb Glow
general | April 04, 2026

How to delete carriage returns from Nano?

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

2 Answers

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

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy