How to convert the ^M linebreak to 'normal' linebreak in a file opened in vim?
Solution 1
This is the only thing that worked for me:
:e ++ff=dos
Found it at: http://vim.wikia.com/wiki/File_format
Solution 2
Command
:%s/<Ctrl-V><Ctrl-M>/\r/g
Where <Ctrl-V><Ctrl-M>
means type Ctrl+V then Ctrl+M.
Explanation
:%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).
Solution 3
On Linux and Mac OS, the following works,
:%s/^V^M/^V^M/g
where ^V^M
means type Ctrl+V, then Ctrl+M.
Note: on Windows you probably want to use ^Q
instead of ^V
, since by default ^V
is mapped to paste text.
Solution 4
Within vim
, look at the file format — DOS or Unix:
:set filetype=unix
:set fileformat=unix
The file will be written back without carriage return (CR, ^M) characters.
Solution 5
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.
ByteNirvana
Hi, I'm a webdeveloper with a solid background in graphic design.
Updated on December 04, 2020Comments
-
ByteNirvana over 3 years
vim shows on every line ending ^M
How I do to replace this with a 'normal' linebreak?
-
Paul Tomblin about 15 yearsWhy did this get a downvote? It works, even when your file is mashed onto one line because it's got the wrong line end.
-
Bart Schuller about 15 yearsIt really works and is exactly what you need when all your text is on one line.
-
Adnan about 15 yearsShouldn't %s/^V^M/^V^M/g be %s/^V^M//g ?
-
Paul Tomblin about 15 yearsNo. My way replaces whatever is the line end in the file with the correct line end.
-
LeopardSkinPillBoxHat almost 14 years@luckytaxi - what does it do for you?
-
sdot257 almost 14 yearsit removed the ^M characters but doesn't insert the carriage return.
-
Metagrapher almost 13 yearsIn this case, what's wrong with using *NIX's native dos2unix command? linux.die.net/man/1/dos2unix
-
derGral almost 13 yearsuse :set fileformat=unix For most configurations filetype only changes the syntax type being used.
-
Metagrapher over 12 yearspressing ctrl-v ctrl-m may work to insert the character, as well, fwiw. but the \r is what inserts the proper carriage return.
-
Thomas Hunter II about 12 yearsThis fixed the bug I was having... Vim started thinking my UNIX formatted file was windows and newly changed lines were showing ^M in the git diff.
-
jumpnett over 11 yearsThis doesn't work, at least on Ubuntu. I get the error
E486: Pattern not found: <Ctrl-V><Ctrl-M>
-
jumpnett over 11 yearsThis doesn't work, at least on Ubuntu. I get the error
E486: Pattern not found: ^V^M
. -
Paul Tomblin over 11 years@jumpnett, you didn't type it right. colon, then percent, then s, then slash, then control v, then control m, then slash, then control v, then control m, then slash, then g, then return.
-
jumpnett over 11 yearsI didn't type it right. It does replace the
^M
's, but it also adds an extra newline. What I want is:%s/^V^M//g
, but that's neither here no there, since I didn't ask the question. -
jumpnett over 11 yearsI actually typed the string '<Ctrl-V><Ctrl-M>'. Did you mean type <kbd>Ctrl</kbd>+<kbd>V</kbd>, then <kbd>Ctrl</kbd>+<kbd>M</kbd>?
-
gbarry over 11 yearsMust be system dependent. Today, this one worked. The set command is done within vim, btw.
-
LeopardSkinPillBoxHat over 11 years@jumpnett - You need to push the key chords Ctrl-V/Ctrl-M, not enter that as verbatim text.
-
gbarry over 11 yearsThere's a tendency for search functions to accept broader rules for recognizing end-of-line sequences. But \r has a specific meaning when it's being written as data.
-
carlosayam over 11 yearsThis is the RIGHT answer. As said above, use <ctrl-v><ctrl-m> to get the literal ^M inserted in the command.
-
Metagrapher over 11 yearsI agree, though I am certain that if that had worked for me at the time of original posting then I wouldn't have posted. ;)
-
Leo about 11 yearsThis was the correct solution when I had this problem. It's not quite as simple as that though. This works if the file has CRLF endings, while the :%s/^V^M/^V^M/g approach works if converting from one to the other. The settings when the file is opened and when it is updated by Git in the background can affect this, because Vim may guess the mode when opening the file, but will read in everything if the file changes while open.
-
Leo about 11 yearsThis is the correct solution if your file appears to have no newlines at all (i.e. it is one long line). If your file appears to have line endings and the ^M character, then this will add an extra line break between every line.
-
Leo about 11 yearsSmall gotcha I found: if Git updates a file in the background (due to a checkout) and you have set this to something other than what Vim guessed when opening the file, Vim does not appear to reconvert when reloading the buffer. You'll end up with a bunch of extraneous characters. You can then use one of the %s/^V^M/.../g approaches above. Which one depends on exactly what formats you're converting to and from.
-
Paul Tomblin about 11 years@Leo, the question specifically asks to convert ^M to a linebreak, which is what my answer does (unlike the accepted answer, which removes ^M).
-
Leo about 11 years@PaulTomblin yes, you're quite right. Apologies. The problem I had was subtly different - ^M appearing in
git diff
but not in Vim because of conversion when the file is openend. I had to use Jonathan Leffler's answer in combination with both yours and LeopardSkinPillBoxHat's to wrangle the various files. Perhaps I should have asked a new question, but last time I did so for a slight variation I was told it was a dupe! -
ppostma1 almost 11 yearsfail, now I have a one-line file
-
LavaSlider over 10 years@PaulTomblin -- your answer works for one long line with ^M's in it. If there are lots of lines, each ending with a ^M, then the accepted answer works since those are really carriage-return/line-feed pairs.
-
Paul Tomblin over 10 years@LavaSlider, my experience is that it works for any file with any number of lines.
-
LavaSlider over 10 yearsBut if the line ends with "<CR><LF>", ":%s/^V^M/^V^M/g" will change it to "<LF><LF>", inserting a blank line. Maybe it's different versions of
vim
but I typed in: hello^V^Mhow^V^Mare^V^Myou^V^M[return]hello^V^M[return]how^V^M[return]are^V^M[return]you^V^M[esc] (where [return] is hitting the return/enter key and [esc] is the escape key). Before executing the command the file has 5 lines, the first with "hello^Mhow^Mare^Myou^M", the next four with one word followed by "^M". After ":%s/^V^M/^V^M/g" there are 13 lines, 1 word per line except blank lines at lines 5,7,9,11 & 13 (on a mac). Try it. -
Paul Tomblin over 10 yearsThat's not my experience with real files moved from Windows to Linux or Windows to Mac.
-
Soundararajan over 10 yearsWorks for me in ArchLinux :-), Vim 7.3
-
O.O over 10 yearsThis one works for me, files from windows, edited on osx, the question is, why this works, it's a bit odd isn't it, replacing with the same character sequence.
-
dlamblin over 10 yearsThis is definitely the right answer for the question, if you have a ^M followed by a new line, you want to keep the newline but remove the ^M. Doing the other substitution below double-spaces your file.
-
dlamblin over 10 yearsThis is for a different problem when your file has NO new lines in it, which I'll admit is more common. FYI ^M can be matched by \r
-
Eric Walker over 10 yearsThis is a deeply mysterious command, but it works for me on a mac.
-
zhaozhi over 10 yearsgreat!because the fileformat is dos, set it to be unix, and it's ok
-
flash over 10 yearsThis worked for me in Windows gVim to sort out a file that had no line breaks, just lots of ^M instead.
-
William Turrell about 10 yearsIf you're still missing a carriage return (i.e. the ^M has been removed but everything is wrapped on a single line), do:
:%s/<Ctrl-V><Ctrl-M>/\r/g
(where \r replaces the ^M with a newline). -
B6431 almost 10 yearsFrom all the solutions offered on this page, this was the only pattern that worked for me in removing ^M from a csv file. Using MacVim.
-
Carl almost 10 years+1 Worked for me too while SSHing to Ubuntu Trusty from Windows using Mobaterm
-
GrandAdmiral almost 10 yearsIf you are running Vim on Windows, then you'll need to use <Ctrl-Q> instead of <Ctrl-V>.
-
b_dev almost 10 yearsFor me, using MacVim @netpoetica's solution way down below was the only one here that worked.
-
Admin over 9 yearsThe OP said replace, not remove.
-
donut over 9 yearsSo, this seemed to replace all of the
\r
characters with\n
despite specifying\r
as the replacement. Not complaining, that's exactly what I wanted. Just weird. -
Eaten by a Grue over 9 yearsdeeply mysterious indeed and works for me on every server I tried it on from redhat, to ubuntu, to os x.
-
Kedar Mhaswade almost 9 yearsYep! Works on cygwin too. I had a file created on Ubuntu/scp'ed on to Windows/opened with vim under Cygwin. It had one long line with 1660 ^M's in it. Running this command gave:
1660 substitutions on 1 line
and the file now has 1661 lines. Yay! -
Ashesh almost 9 yearsThis is the correct answer and not the one that has been accepted.
-
Posva over 8 yearsThis also works on vi BTW, and you can do
set ff=unix
as a little shortcut in the same wayft
is a shortcut forfiletype
-
Jonathan Leffler over 8 years@Posva: Thanks for the abbreviations!
-
RichVel over 8 yearsI found the equivalent
%s/\r/\r/g
worked well on MacVim for a file showing only^M
at end of lines (one long line in MacVim terms). Clearly \r means different things in the pattern vs replacement parts of this command. -
schuess over 8 yearsWorked on windows7 Vim7.4
-
a2f0 over 8 yearsMake sure you actually press Ctrl-V and Ctrl-M as opposed to just copy and pasting the string.
-
jgitter about 8 yearsI always forget how to do this, and always end up back on this question when I go looking. Thanks for the 18th time.
-
Reza S almost 8 yearsThis answer should be my homepage
-
Kuf over 7 yearsnever heard of mac2unix before, would have upvote again if I could!
-
Death Metal over 7 yearsThis works like charm! on OSX+vim. :) Thank you so much. Highly annoying to have ^M in file.
-
Private over 7 yearsHuray! this is the way to go!
-
Max over 7 yearsI had to do both.
:e ++ff=dos
then:set ff=unix
and:w
-
HoldOffHunger over 7 yearsWhen dealing with a text file that was mangled by Windows, I only needed to replace the ^M with "" (the empty string).
-
pixel 67 over 7 yearsThis is the way I do it as well. I just never remember the command
-
dcc310 over 7 yearsThis answer cleared up for me why the search and replace patterns are the same. I would think that has no effect, but they are interpreted differently in each context - I haven't seen that mentioned yet. Short answer seems to be that "\n was already taken".
-
LordWilmore over 7 yearsCan you add some explanation as to why this works, i.e. what are you actually doing with the various parts of that command?
-
Martin Tournoij over 7 yearsThis is just the same as the top answer, except that
<C-v>
is replace with<C-q>
because by default the former is mapped to paste text. -
jiangdongzi over 7 years^M means "0d0d0a",
:%s/<C-Q><C-M>/g
will change "0d0d0a" to "0d0a" (0d0a means \n\r). I dont know why, but it really works in this way (0d0d0a→0d0a i.e. ^M→\n\r) in my windows7 OS. -
roblogic about 7 yearsFurther to @GrandAdmiral : GVim on windows could not find
^M
but it found^M$
!?!? example::%s/^M$//g
worked for me -
Jeff Holt about 7 yearsI like this solution because I don't have to do what I used to do (a) leave vi, (b) vi -b <filename>, (c) :s/^M//. Thanks.
-
Sabuncu about 7 yearsYour tip on using ^Q on Windows should get an additional +1000 points! Thanks.
-
Marcello Romani about 7 yearsThis one worked for me, while the accepted answer didn't.
-
Peddipaga almost 7 yearsthank you! this fixed my issue and found it useful to use in scripts
-
dexter2305 almost 7 yearsIn one other thread, "set binary" was recommended to get rid of the auto insert of new line.
-
ZhaoGang almost 7 yearsif you only want to remove the ^M:
:%s/<Ctrl-V><Ctrl-M>/g
. Also I find out that/g
is not necessary(gvim, windows) -
Metagrapher over 6 yearsidk why this was downvoted to -1... because it uses sed? you can do the same substitution inside vim and it should work
-
Metagrapher over 6 yearsThis is the recommended way to remove these endings.
-
Metagrapher over 6 yearsThis will remove ANY character at the end of a line. If you have a line that is not, for some reason, terminated by ^M, then you will remove that character also, and would be unexpected. This should not be the case, but in the event that it is, and you don't know this, then you could sully your file in a very hard to identify way.
-
Admin over 6 yearsBang on!! Works on Mac.
-
M.D. over 6 yearsNote that this does not work if the file is loaded as a dos file.
-
Mateen Ulhaq about 6 years
:e ++ff=dos
followed by:set ff=unix
will convert the endings to a sane format. -
lcjury almost 6 yearsThis didn't worked for me, but RichVel comment %s/\r/\r/g worked for me. Would love to see an answer who explain why this answer didn't worked, but \r does (and another solutions people put). There are a lot of different solutions to this problem, and don't understand what is happening resolves to try everything until something works.
-
Armand almost 6 yearsAre you sure this works? On OSX:
sed -e 's/\r/\n/g' <<< 'over and over'
->oven and oven
-
Freedom_Ben almost 6 yearsYes it works (I use it a lot), but on OSX if you do not have the GNU version of sed installed then it may not work. The version of sed that Apple ships by default is much different (sadly) than the GNU version that ships on all Linux distros. Check out the answer to this question for more help: stackoverflow.com/q/30003570/2062384
-
Armand almost 6 yearsThanks @Freedom_Ben. So it sounds like this works for GNU
sed
but not BSDsed
then (as found on OSX). Anyone fixing linebreaks is probably working with a mix of operating systems, so it's useful to be clear about when the answer will work. -
Edenshaw almost 6 yearsSolution's response didn't work for me, only @MateenUlhaq commands helped me.
-
Sardeep Lakhera over 5 yearsonly this worked for me. If you don't want new line, don't add \r.
:%s/<Ctrl-V><Ctrl-M>//g
-
Admin over 5 yearsThis answer added extra line breaks to my file ... the accepted answer worked fine.
:e ++ff=dos
-
carlin.scott over 3 yearsThat just removed the ^M characters, rather than replace them with newlines.
-
robsch over 3 yearsOn a Mac with iTerm2 one really has to use the 'control' key, not the 'command'.
-
Enis Arik about 3 yearsThis worked perfectly for my case as well.
-
RGD2 almost 3 yearsthis failed to do both for me.
:%s/^M/\r/g
(where the^M
was CTRL-'VM') worked. -
RGD2 almost 3 yearsNice Answer. Just kills the
^M
though, doesn't replace them with newlines (vim linux, ff=dos). YMMV depending on which of [mac,windows,linux] you have and what fileformat is currently set to ( see with:set ff?
). Were you on a mac? -
RGD2 almost 3 yearsVim 8.1 gives me
E486: Pattern not found: \r$
for that. -
pacoverflow over 2 years@Adnan yes, replacing it with nothing worked for me.
-
Andy K over 2 yearsFirst command simply removes all \r, if you save it like this your file will be destroyed.