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.
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.
2 Answers
This seems not to be necessary.
$ echo -e 'foo\rbar\r\n' > nano
$ cat -A nano
foo^Mbar^M$
$
$ nano nanoI 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.
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:
- Press Ctrl+\ to open the "search and replace" prompt.
- Press Alt+r to enable RegEx search.
- Enter
[[:space:]]+$or\s+$or+$and press Enter to search for trailing whitespace. - Press
Enterto replace the found whitespace with "nothing". - Enter
Aand press Enter to apply to all occurences of whitespace.