How to convert the ^M linebreak to 'normal' linebreak in a file opened in vim?

403,744

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.

Share:
403,744
ByteNirvana
Author by

ByteNirvana

Hi, I'm a webdeveloper with a solid background in graphic design.

Updated on December 04, 2020

Comments

  • ByteNirvana
    ByteNirvana over 3 years

    vim shows on every line ending ^M

    How I do to replace this with a 'normal' linebreak?

  • Paul Tomblin
    Paul Tomblin about 15 years
    Why 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
    Bart Schuller about 15 years
    It really works and is exactly what you need when all your text is on one line.
  • Adnan
    Adnan about 15 years
    Shouldn't %s/^V^M/^V^M/g be %s/^V^M//g ?
  • Paul Tomblin
    Paul Tomblin about 15 years
    No. My way replaces whatever is the line end in the file with the correct line end.
  • LeopardSkinPillBoxHat
    LeopardSkinPillBoxHat almost 14 years
    @luckytaxi - what does it do for you?
  • sdot257
    sdot257 almost 14 years
    it removed the ^M characters but doesn't insert the carriage return.
  • Metagrapher
    Metagrapher almost 13 years
    In this case, what's wrong with using *NIX's native dos2unix command? linux.die.net/man/1/dos2unix
  • derGral
    derGral almost 13 years
    use :set fileformat=unix For most configurations filetype only changes the syntax type being used.
  • Metagrapher
    Metagrapher over 12 years
    pressing 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
    Thomas Hunter II about 12 years
    This 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
    jumpnett over 11 years
    This doesn't work, at least on Ubuntu. I get the error E486: Pattern not found: <Ctrl-V><Ctrl-M>
  • jumpnett
    jumpnett over 11 years
    This doesn't work, at least on Ubuntu. I get the error E486: Pattern not found: ^V^M.
  • Paul Tomblin
    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
    jumpnett over 11 years
    I 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
    jumpnett over 11 years
    I 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
    gbarry over 11 years
    Must be system dependent. Today, this one worked. The set command is done within vim, btw.
  • LeopardSkinPillBoxHat
    LeopardSkinPillBoxHat over 11 years
    @jumpnett - You need to push the key chords Ctrl-V/Ctrl-M, not enter that as verbatim text.
  • gbarry
    gbarry over 11 years
    There'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
    carlosayam over 11 years
    This is the RIGHT answer. As said above, use <ctrl-v><ctrl-m> to get the literal ^M inserted in the command.
  • Metagrapher
    Metagrapher over 11 years
    I 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
    Leo about 11 years
    This 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
    Leo about 11 years
    This 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
    Leo about 11 years
    Small 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
    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
    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
    ppostma1 almost 11 years
    fail, now I have a one-line file
  • LavaSlider
    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
    Paul Tomblin over 10 years
    @LavaSlider, my experience is that it works for any file with any number of lines.
  • LavaSlider
    LavaSlider over 10 years
    But 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[re‌​turn]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
    Paul Tomblin over 10 years
    That's not my experience with real files moved from Windows to Linux or Windows to Mac.
  • Soundararajan
    Soundararajan over 10 years
    Works for me in ArchLinux :-), Vim 7.3
  • O.O
    O.O over 10 years
    This 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
    dlamblin over 10 years
    This 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
    dlamblin over 10 years
    This 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
    Eric Walker over 10 years
    This is a deeply mysterious command, but it works for me on a mac.
  • zhaozhi
    zhaozhi over 10 years
    great!because the fileformat is dos, set it to be unix, and it's ok
  • flash
    flash over 10 years
    This worked for me in Windows gVim to sort out a file that had no line breaks, just lots of ^M instead.
  • William Turrell
    William Turrell about 10 years
    If 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
    B6431 almost 10 years
    From 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
    Carl almost 10 years
    +1 Worked for me too while SSHing to Ubuntu Trusty from Windows using Mobaterm
  • GrandAdmiral
    GrandAdmiral almost 10 years
    If you are running Vim on Windows, then you'll need to use <Ctrl-Q> instead of <Ctrl-V>.
  • b_dev
    b_dev almost 10 years
    For me, using MacVim @netpoetica's solution way down below was the only one here that worked.
  • Admin
    Admin over 9 years
    The OP said replace, not remove.
  • donut
    donut over 9 years
    So, 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
    Eaten by a Grue over 9 years
    deeply mysterious indeed and works for me on every server I tried it on from redhat, to ubuntu, to os x.
  • Kedar Mhaswade
    Kedar Mhaswade almost 9 years
    Yep! 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
    Ashesh almost 9 years
    This is the correct answer and not the one that has been accepted.
  • Posva
    Posva over 8 years
    This also works on vi BTW, and you can do set ff=unix as a little shortcut in the same way ft is a shortcut for filetype
  • Jonathan Leffler
    Jonathan Leffler over 8 years
    @Posva: Thanks for the abbreviations!
  • RichVel
    RichVel over 8 years
    I 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
    schuess over 8 years
    Worked on windows7 Vim7.4
  • a2f0
    a2f0 over 8 years
    Make sure you actually press Ctrl-V and Ctrl-M as opposed to just copy and pasting the string.
  • jgitter
    jgitter about 8 years
    I 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
    Reza S almost 8 years
    This answer should be my homepage
  • Kuf
    Kuf over 7 years
    never heard of mac2unix before, would have upvote again if I could!
  • Death Metal
    Death Metal over 7 years
    This works like charm! on OSX+vim. :) Thank you so much. Highly annoying to have ^M in file.
  • Private
    Private over 7 years
    Huray! this is the way to go!
  • Max
    Max over 7 years
    I had to do both. :e ++ff=dos then :set ff=unix and :w
  • HoldOffHunger
    HoldOffHunger over 7 years
    When dealing with a text file that was mangled by Windows, I only needed to replace the ^M with "" (the empty string).
  • pixel 67
    pixel 67 over 7 years
    This is the way I do it as well. I just never remember the command
  • dcc310
    dcc310 over 7 years
    This 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
    LordWilmore over 7 years
    Can 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
    Martin Tournoij over 7 years
    This 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
    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
    roblogic about 7 years
    Further to @GrandAdmiral : GVim on windows could not find ^M but it found ^M$ !?!? example: :%s/^M$//g worked for me
  • Jeff Holt
    Jeff Holt about 7 years
    I 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
    Sabuncu about 7 years
    Your tip on using ^Q on Windows should get an additional +1000 points! Thanks.
  • Marcello Romani
    Marcello Romani about 7 years
    This one worked for me, while the accepted answer didn't.
  • Peddipaga
    Peddipaga almost 7 years
    thank you! this fixed my issue and found it useful to use in scripts
  • dexter2305
    dexter2305 almost 7 years
    In one other thread, "set binary" was recommended to get rid of the auto insert of new line.
  • ZhaoGang
    ZhaoGang almost 7 years
    if 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
    Metagrapher over 6 years
    idk why this was downvoted to -1... because it uses sed? you can do the same substitution inside vim and it should work
  • Metagrapher
    Metagrapher over 6 years
    This is the recommended way to remove these endings.
  • Metagrapher
    Metagrapher over 6 years
    This 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
    Admin over 6 years
    Bang on!! Works on Mac.
  • M.D.
    M.D. over 6 years
    Note that this does not work if the file is loaded as a dos file.
  • Mateen Ulhaq
    Mateen Ulhaq about 6 years
    :e ++ff=dos followed by :set ff=unix will convert the endings to a sane format.
  • lcjury
    lcjury almost 6 years
    This 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
    Armand almost 6 years
    Are you sure this works? On OSX: sed -e 's/\r/\n/g' <<< 'over and over' -> oven and oven
  • Freedom_Ben
    Freedom_Ben almost 6 years
    Yes 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
    Armand almost 6 years
    Thanks @Freedom_Ben. So it sounds like this works for GNU sed but not BSD sed 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
    Edenshaw almost 6 years
    Solution's response didn't work for me, only @MateenUlhaq commands helped me.
  • Sardeep Lakhera
    Sardeep Lakhera over 5 years
    only this worked for me. If you don't want new line, don't add \r. :%s/<Ctrl-V><Ctrl-M>//g
  • Admin
    Admin over 5 years
    This answer added extra line breaks to my file ... the accepted answer worked fine. :e ++ff=dos
  • carlin.scott
    carlin.scott over 3 years
    That just removed the ^M characters, rather than replace them with newlines.
  • robsch
    robsch over 3 years
    On a Mac with iTerm2 one really has to use the 'control' key, not the 'command'.
  • Enis Arik
    Enis Arik about 3 years
    This worked perfectly for my case as well.
  • RGD2
    RGD2 almost 3 years
    this failed to do both for me. :%s/^M/\r/g (where the ^M was CTRL-'VM') worked.
  • RGD2
    RGD2 almost 3 years
    Nice 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
    RGD2 almost 3 years
    Vim 8.1 gives me E486: Pattern not found: \r$ for that.
  • pacoverflow
    pacoverflow over 2 years
    @Adnan yes, replacing it with nothing worked for me.
  • Andy K
    Andy K over 2 years
    First command simply removes all \r, if you save it like this your file will be destroyed.