vim shows on every line ending ^M
How I do to replace this with a 'normal' linebreak?
vim shows on every line ending ^M
How I do to replace this with a 'normal' linebreak?
This is the only thing that worked for me:
:e ++ff=dos
Found it at: http://vim.wikia.com/wiki/File_format
:%s/<Ctrl-V><Ctrl-M>/\r/g
Where <Ctrl-V><Ctrl-M>
means type Ctrl+V then Ctrl+M.
:%s
substitute, % = all lines
<Ctrl-V><Ctrl-M>
^M characters (the Ctrl-V is a Vim way of writing the Ctrl ^ character and Ctrl-M writes the M after the regular expression, resulting to ^M special character)
/\r/
with new line (\r
)
g
And do it globally (not just the first occurrence on the line).
A file I had created with BBEdit seen in MacVim was displaying a bunch of ^M
line returns instead of regular ones. The following string replace solved the issue - hope this helps:
:%s/\r/\r/g
It's interesting because I'm replacing line breaks with the same character, but I suppose Vim just needs to get a fresh \r to display correctly. I'd be interested to know the underlying mechanics of why this works.
First, use :set ff?
to figure out the file format your file is.
I guess it could be unix
, then the problem is your file was created with fileformat=dos
adding "^M^J" to the line end but read with flieformat=unix
only removing the "^J" from the line end, leaving the "^M" there.
Just input :e ++ff=dos
in Vim command line to change your file's format from unix to dos. It should solve the problem. If not, :%s/\r//g
should help you out.
in order to get the ^M character to match I had to visually select it and then use the OS copy to clipboard command to retrieve it. You can test it by doing a search for the character before trying the replace command.
/^M
should select the first bad line
:%s/^M/\r/g
will replace all the errant ^M with carriage returns.
This is as functions in MacVim, which is based on gvim 7.
EDIT:
Having this problem again on my Windows 10 machine, which has Ubuntu for Windows, and I think this is causing fileformat issues for vim. In this case changing the ff to unix, mac, or dos did nothing other than to change the ^M to ^J and back again.
The solution in this case:
:%s/\r$/ /g
:%s/ $//g
The reason I went this route is because I wanted to ensure I was being non-destructive with my file. I could have :%s/\r$//g
but that would have deleted the carriage returns right out, and could have had unexpected results. Instead we convert the singular CR character, here a ^M character, into a space, and then remove all spaces at the end of lines (which for me is a desirable result regardless)
Sorry for reviving an old question that has long since been answered, but there seemed to be some confusion afoot and I thought I'd help clear some of that up since this is coming up high in google searches.
There are many other answers to this question, but still, the following works best for me, as I needed a command line solution:
vim -u NONE -c 'e ++ff=dos' -c 'w ++ff=unix' -c q myfile
Explanation:
myfile
:e ++ff=dos
to force a reload of the entire file as dos line endings.:w ++ff=unix
to write the file using unix line endingsCtrl+M minimizes my window, but Ctrl+Enter actually inserts a ^M
character. I also had to be sure not to lift off the Ctrl key between presses.
So the solution for me was:
:%s/<Ctrl-V><Ctrl-Enter>/\r/g
Where <Ctrl-V><Ctrl-Enter>
means to press and hold Ctrl, press and release V, press and release Enter, and then release Ctrl.
The above solution will add an additional line between existing lines, because there is already an invisible \r
after the ^M
.
To prevent this, you want to delete the ^M
characters without replacing them.
:%s/<Ctrl-V><Ctrl-Enter>//g
Where %
means "in this buffer," s
means "substitute," /
means "(find) the following pattern," <Ctrl-V><Ctrl-Enter>
refers to the keys to press to get the ^M
character (see above), //
means "with nothing" (or, "with the pattern between these two slashes, which is empty"), and g
is a flag meaning "globally," as opposed to the first occurrence in a line.
In my case,
Nothing above worked, I had a CSV file copied to Linux machine from my mac and I used all the above commands but nothing helped but the below one
tr "\015" "\n" < inputfile > outputfile
I had a file in which ^M characters were sandwitched between lines something like below
Audi,A4,35 TFSi Premium,,CAAUA4TP^MB01BNKT6TG,TRO_WBFB_500,Trico,CARS,Audi,A4,35 TFSi Premium,,CAAUA4TP^MB01BNKTG0A,TRO_WB_T500,Trico,
Alternatively, there are open-source utilities called dos2unix and unix2dos available that do this very thing. On a linux system they are probably installed by default; for a windows system you can download them from http://www.bastet.com/ amongst others.
To use sed
on MacOS, do this:
sed -i.bak $'s/\r//' <filename>
Explanation: The $'STRING'
syntax here pertains to the bash shell. Macs don't treat \r
as special character. By quoting the command string in $''
you're telling the shell to replace \r
with the actual \r
character specified in the ANSI-C standard.
I've spent an afternoon struggling with \n ctrl-v 012 (both of which supply me with null). & laboured through this thread until I reached metagrapher's.
\r
worked fine for me!
/),/s/),/)\r/g
turned something like this:
blacklist-extra:i386 (0.4.1, 0.4.1+nmu1), libmount1:i386 (2.20.1-5.1, 2.20.1 -5.2), libblkid1:i386 (2.20.1-5.1, 2.20.1-5.2), libapt-pkg4.12:i386 (0.9.7.4 , 0.9.7.5), nmap:i386 (6.00-0.1, 6.00-0.2), libsane-common:i386 (1.0.22-7.3,
into something like this:
26 libwv-1.2-4:i386 (1.2.9-3, automatic)
27 openjdk-6-jre-headless:i386 (6b24-1.11.4-3, automatic)
28 jed:i386 (0.99.19-2.1)
Magic. I am profoundly grateful